Bug Summary

File:_build/../src/icu-converter.cc
Warning:line 121, column 9
Value stored to 'target_size' is never read

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name icu-converter.cc -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -pic-is-pie -mframe-pointer=none -relaxed-aliasing -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/rootdir/_build -resource-dir /usr/lib/llvm-16/lib/clang/16 -I src/decoder-cat.p -I src -I ../src -I . -I .. -I /usr/include/glib-2.0 -I /usr/lib/x86_64-linux-gnu/glib-2.0/include -I /usr/include/sysprof-6 -D _GLIBCXX_ASSERTIONS=1 -D _FILE_OFFSET_BITS=64 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/13/../../../../include/x86_64-linux-gnu/c++/13 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/backward -internal-isystem /usr/lib/llvm-16/lib/clang/16/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/13/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O3 -Wno-address-of-packed-member -Wno-missing-field-initializers -Wno-packed -Wno-switch-enum -Wno-unused-parameter -Wwrite-strings -std=gnu++17 -fdeprecated-macro -fdebug-compilation-dir=/rootdir/_build -ferror-limit 19 -fvisibility=hidden -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -fcxx-exceptions -fexceptions -fcolor-diagnostics -vectorize-loops -vectorize-slp -analyzer-checker deadcode.DeadStores -analyzer-checker alpha.deadcode.UnreachableCode -analyzer-checker alpha.core.CastSize -analyzer-checker alpha.core.CastToStruct -analyzer-checker alpha.core.IdenticalExpr -analyzer-checker alpha.core.SizeofPtr -analyzer-checker alpha.security.ArrayBoundV2 -analyzer-checker alpha.security.MallocOverflow -analyzer-checker alpha.security.ReturnPtrRange -analyzer-checker alpha.unix.SimpleStream -analyzer-checker alpha.unix.cstring.BufferOverlap -analyzer-checker alpha.unix.cstring.NotNullTerminated -analyzer-checker alpha.unix.cstring.OutOfBounds -analyzer-checker alpha.core.FixedAddr -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /rootdir/html-report/2024-08-05-222352-10670-1 -x c++ ../src/icu-converter.cc
1/*
2 * Copyright © 2019 Christian Persch
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 3 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18#include "config.h"
19
20#include "icu-converter.hh"
21
22#include <cassert>
23#include <memory>
24
25#include <unicode/errorcode.h>
26
27#include "debug.h"
28#include "icu-glue.hh"
29
30namespace bte::base {
31
32std::unique_ptr<ICUConverter>
33ICUConverter::make(char const* charset,
34 GError** error)
35{
36 if (bte::base::get_icu_charset_is_ecma35(charset))
37 return {};
38
39 auto charset_converter = bte::base::make_icu_converter(charset, error);
40 if (!charset_converter)
41 return {};
42
43 auto u32_converter = bte::base::make_icu_converter("utf32platformendian", error);
44 if (!u32_converter)
45 return {};
46
47 auto u8_converter = bte::base::make_icu_converter("utf8", error);
48 if (!u8_converter)
49 return {};
50
51 return std::make_unique<ICUConverter>(charset, charset_converter, u32_converter, u8_converter);
52}
53
54std::string
55ICUConverter::convert(std::string_view const& data)
56{
57 /* We can't use ucnv_convertEx since that doesn't support preflighting.
58 * Instead, convert to UTF-16 first, and the to the target, with
59 * preflighting both times. This is slow, but this is the legacy
60 * code path, so we don't care.
61 */
62
63 if (data.size() == 0)
64 return {};
65
66 ucnv_resetToUnicodeucnv_resetToUnicode_72(m_u8_converter.get());
67
68 auto err = icu::ErrorCode{};
69 auto u16_size = ucnv_toUCharsucnv_toUChars_72(m_u8_converter.get(),
70 nullptr, 0,
71 data.data(), data.size(),
72 err);
73 if (err.isFailure() && (err.get() != U_BUFFER_OVERFLOW_ERROR)) {
74 _bte_debug_print(BTE_DEBUG_CONVERSION,do { } while(0)
75 "Error converting from UTF-8 to UTF-16 in preflight: %s\n",do { } while(0)
76 err.errorName())do { } while(0);
77 return {};
78 }
79
80 auto u16_buffer = std::u16string{};
81 if ((size_t)u16_size > u16_buffer.max_size()) // prevent exceptions
82 return {};
83 u16_buffer.resize(u16_size);
84
85 err.reset();
86 u16_size = ucnv_toUCharsucnv_toUChars_72(m_u8_converter.get(),
87 u16_buffer.data(),
88 u16_buffer.size(),
89 data.data(),
90 data.size(),
91 err);
92 if (err.isFailure()) {
93 _bte_debug_print(BTE_DEBUG_CONVERSION,do { } while(0)
94 "Error converting from UTF-8 to UTF-16: %s\n",do { } while(0)
95 err.errorName())do { } while(0);
96 return {};
97 }
98
99 /* Now convert to target */
100 ucnv_resetFromUnicodeucnv_resetFromUnicode_72(m_charset_converter.get());
101 err.reset();
102 auto target_size = ucnv_fromUCharsucnv_fromUChars_72(m_charset_converter.get(),
103 nullptr, 0,
104 u16_buffer.data(),
105 u16_size,
106 err);
107 if (err.isFailure() && (err.get() != U_BUFFER_OVERFLOW_ERROR)) {
108 _bte_debug_print(BTE_DEBUG_CONVERSION,do { } while(0)
109 "Error converting from UTF-8 to %s in preflight: %s\n",do { } while(0)
110 m_charset.c_str(),do { } while(0)
111 err.errorName())do { } while(0);
112 return {};
113 }
114
115 auto target_buffer = std::string{};
116 if ((size_t)target_size > target_buffer.max_size()) // prevent exceptions
117 return {};
118 target_buffer.resize(target_size);
119
120 err.reset();
121 target_size = ucnv_fromUCharsucnv_fromUChars_72(m_charset_converter.get(),
Value stored to 'target_size' is never read
122 target_buffer.data(),
123 target_buffer.capacity(),
124 u16_buffer.data(),
125 u16_size,
126 err);
127 if (err.isFailure()) {
128 _bte_debug_print(BTE_DEBUG_CONVERSION,do { } while(0)
129 "Error converting from UTF-16 to %s: %s\n",do { } while(0)
130 m_charset.c_str(),do { } while(0)
131 err.errorName())do { } while(0);
132 return {};
133 }
134
135 return target_buffer;
136}
137
138} // namespace bte::base