Bug Summary

File:root/firefox-clang/intl/icu/source/i18n/formattedvalue.cpp
Warning:line 219, column 5
Address of stack memory associated with local variable 'readOnlyAlias' returned to caller

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 formattedvalue.cpp -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 -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -fhalf-no-semantic-interposition -mframe-pointer=all -relaxed-aliasing -ffp-contract=off -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/root/firefox-clang/obj-x86_64-pc-linux-gnu/config/external/icu/i18n -fcoverage-compilation-dir=/root/firefox-clang/obj-x86_64-pc-linux-gnu/config/external/icu/i18n -resource-dir /usr/lib/llvm-21/lib/clang/21 -include /root/firefox-clang/config/gcc_hidden.h -include /root/firefox-clang/obj-x86_64-pc-linux-gnu/mozilla-config.h -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/system_wrappers -U _FORTIFY_SOURCE -D _FORTIFY_SOURCE=2 -D _GLIBCXX_ASSERTIONS -D DEBUG=1 -D U_I18N_IMPLEMENTATION -D _LIBCPP_DISABLE_DEPRECATION_WARNINGS -D U_USING_ICU_NAMESPACE=0 -D U_NO_DEFAULT_INCLUDE_UTF_HEADERS=1 -D U_HIDE_OBSOLETE_UTF_OLD_H=1 -D UCONFIG_NO_LEGACY_CONVERSION -D UCONFIG_NO_TRANSLITERATION -D UCONFIG_NO_REGULAR_EXPRESSIONS -D UCONFIG_NO_BREAK_ITERATION -D UCONFIG_NO_IDNA -D UCONFIG_NO_MF2 -D U_CHARSET_IS_UTF8 -D UNISTR_FROM_CHAR_EXPLICIT=explicit -D UNISTR_FROM_STRING_EXPLICIT=explicit -D U_ENABLE_DYLOAD=0 -D U_DEBUG=1 -I /root/firefox-clang/config/external/icu/i18n -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/config/external/icu/i18n -I /root/firefox-clang/intl/icu/source/common -I /root/firefox-clang/mfbt/double-conversion -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/nspr -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/nss -D MOZILLA_CLIENT -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/14/../../../../include/c++/14 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/14/../../../../include/x86_64-linux-gnu/c++/14 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/14/../../../../include/c++/14/backward -internal-isystem /usr/lib/llvm-21/lib/clang/21/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/14/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-error=pessimizing-move -Wno-error=large-by-value-copy=128 -Wno-error=implicit-int-float-conversion -Wno-error=thread-safety-analysis -Wno-error=tautological-type-limit-compare -Wno-invalid-offsetof -Wno-range-loop-analysis -Wno-deprecated-anon-enum-enum-conversion -Wno-deprecated-enum-enum-conversion -Wno-deprecated-this-capture -Wno-inline-new-delete -Wno-error=deprecated-declarations -Wno-error=array-bounds -Wno-error=free-nonheap-object -Wno-error=atomic-alignment -Wno-error=deprecated-builtins -Wno-psabi -Wno-error=builtin-macro-redefined -Wno-vla-cxx-extension -Wno-unknown-warning-option -Wno-comma -Wno-implicit-const-int-float-conversion -Wno-macro-redefined -Wno-microsoft-include -Wno-tautological-unsigned-enum-zero-compare -Wno-unreachable-code-loop-increment -Wno-unreachable-code-return -fdeprecated-macro -ferror-limit 19 -fstrict-flex-arrays=1 -stack-protector 2 -fstack-clash-protection -ftrivial-auto-var-init=pattern -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fno-sized-deallocation -fno-aligned-allocation -vectorize-loops -vectorize-slp -analyzer-checker optin.performance.Padding -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2025-06-27-100320-3286336-1 -x c++ /root/firefox-clang/intl/icu/source/i18n/formattedvalue.cpp
1// © 2018 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html
3
4#include "unicode/utypes.h"
5
6#if !UCONFIG_NO_FORMATTING0
7
8#include "unicode/formattedvalue.h"
9#include "formattedval_impl.h"
10#include "capi_helper.h"
11
12U_NAMESPACE_BEGINnamespace icu_77 {
13
14
15ConstrainedFieldPosition::ConstrainedFieldPosition() {}
16
17ConstrainedFieldPosition::~ConstrainedFieldPosition() {}
18
19void ConstrainedFieldPosition::reset() {
20 fContext = 0LL;
21 fField = 0;
22 fStart = 0;
23 fLimit = 0;
24 fConstraint = UCFPOS_CONSTRAINT_NONE;
25 fCategory = UFIELD_CATEGORY_UNDEFINED;
26}
27
28void ConstrainedFieldPosition::constrainCategory(int32_t category) {
29 fConstraint = UCFPOS_CONSTRAINT_CATEGORY;
30 fCategory = category;
31}
32
33void ConstrainedFieldPosition::constrainField(int32_t category, int32_t field) {
34 fConstraint = UCFPOS_CONSTRAINT_FIELD;
35 fCategory = category;
36 fField = field;
37}
38
39void ConstrainedFieldPosition::setInt64IterationContext(int64_t context) {
40 fContext = context;
41}
42
43UBool ConstrainedFieldPosition::matchesField(int32_t category, int32_t field) const {
44 switch (fConstraint) {
45 case UCFPOS_CONSTRAINT_NONE:
46 return true;
47 case UCFPOS_CONSTRAINT_CATEGORY:
48 return fCategory == category;
49 case UCFPOS_CONSTRAINT_FIELD:
50 return fCategory == category && fField == field;
51 default:
52 UPRV_UNREACHABLE_EXITabort();
53 }
54}
55
56void ConstrainedFieldPosition::setState(
57 int32_t category,
58 int32_t field,
59 int32_t start,
60 int32_t limit) {
61 fCategory = category;
62 fField = field;
63 fStart = start;
64 fLimit = limit;
65}
66
67
68FormattedValue::~FormattedValue() = default;
69
70
71///////////////////////
72/// C API FUNCTIONS ///
73///////////////////////
74
75struct UConstrainedFieldPositionImpl : public UMemory,
76 // Magic number as ASCII == "UCF"
77 public IcuCApiHelper<UConstrainedFieldPosition, UConstrainedFieldPositionImpl, 0x55434600> {
78 ConstrainedFieldPosition fImpl;
79};
80
81U_CAPIextern "C" UConstrainedFieldPosition* U_EXPORT2
82ucfpos_openucfpos_open_77(UErrorCode* ec) {
83 auto* impl = new UConstrainedFieldPositionImpl();
84 if (impl == nullptr) {
85 *ec = U_MEMORY_ALLOCATION_ERROR;
86 return nullptr;
87 }
88 return impl->exportForC();
89}
90
91U_CAPIextern "C" void U_EXPORT2
92ucfpos_resetucfpos_reset_77(UConstrainedFieldPosition* ptr, UErrorCode* ec) {
93 auto* impl = UConstrainedFieldPositionImpl::validate(ptr, *ec);
94 if (U_FAILURE(*ec)) {
95 return;
96 }
97 impl->fImpl.reset();
98}
99
100U_CAPIextern "C" void U_EXPORT2
101ucfpos_constrainCategoryucfpos_constrainCategory_77(UConstrainedFieldPosition* ptr, int32_t category, UErrorCode* ec) {
102 auto* impl = UConstrainedFieldPositionImpl::validate(ptr, *ec);
103 if (U_FAILURE(*ec)) {
104 return;
105 }
106 impl->fImpl.constrainCategory(category);
107}
108
109U_CAPIextern "C" void U_EXPORT2
110ucfpos_constrainFielducfpos_constrainField_77(UConstrainedFieldPosition* ptr, int32_t category, int32_t field, UErrorCode* ec) {
111 auto* impl = UConstrainedFieldPositionImpl::validate(ptr, *ec);
112 if (U_FAILURE(*ec)) {
113 return;
114 }
115 impl->fImpl.constrainField(category, field);
116}
117
118U_CAPIextern "C" int32_t U_EXPORT2
119ucfpos_getCategoryucfpos_getCategory_77(const UConstrainedFieldPosition* ptr, UErrorCode* ec) {
120 const auto* impl = UConstrainedFieldPositionImpl::validate(ptr, *ec);
121 if (U_FAILURE(*ec)) {
122 return UFIELD_CATEGORY_UNDEFINED;
123 }
124 return impl->fImpl.getCategory();
125}
126
127U_CAPIextern "C" int32_t U_EXPORT2
128ucfpos_getFielducfpos_getField_77(const UConstrainedFieldPosition* ptr, UErrorCode* ec) {
129 const auto* impl = UConstrainedFieldPositionImpl::validate(ptr, *ec);
130 if (U_FAILURE(*ec)) {
131 return 0;
132 }
133 return impl->fImpl.getField();
134}
135
136U_CAPIextern "C" void U_EXPORT2
137ucfpos_getIndexesucfpos_getIndexes_77(const UConstrainedFieldPosition* ptr, int32_t* pStart, int32_t* pLimit, UErrorCode* ec) {
138 const auto* impl = UConstrainedFieldPositionImpl::validate(ptr, *ec);
139 if (U_FAILURE(*ec)) {
140 return;
141 }
142 *pStart = impl->fImpl.getStart();
143 *pLimit = impl->fImpl.getLimit();
144}
145
146U_CAPIextern "C" int64_t U_EXPORT2
147ucfpos_getInt64IterationContextucfpos_getInt64IterationContext_77(const UConstrainedFieldPosition* ptr, UErrorCode* ec) {
148 const auto* impl = UConstrainedFieldPositionImpl::validate(ptr, *ec);
149 if (U_FAILURE(*ec)) {
150 return 0;
151 }
152 return impl->fImpl.getInt64IterationContext();
153}
154
155U_CAPIextern "C" void U_EXPORT2
156ucfpos_setInt64IterationContextucfpos_setInt64IterationContext_77(UConstrainedFieldPosition* ptr, int64_t context, UErrorCode* ec) {
157 auto* impl = UConstrainedFieldPositionImpl::validate(ptr, *ec);
158 if (U_FAILURE(*ec)) {
159 return;
160 }
161 impl->fImpl.setInt64IterationContext(context);
162}
163
164U_CAPIextern "C" UBool U_EXPORT2
165ucfpos_matchesFielducfpos_matchesField_77(const UConstrainedFieldPosition* ptr, int32_t category, int32_t field, UErrorCode* ec) {
166 const auto* impl = UConstrainedFieldPositionImpl::validate(ptr, *ec);
167 if (U_FAILURE(*ec)) {
168 return 0;
169 }
170 return impl->fImpl.matchesField(category, field);
171}
172
173U_CAPIextern "C" void U_EXPORT2
174ucfpos_setStateucfpos_setState_77(
175 UConstrainedFieldPosition* ptr,
176 int32_t category,
177 int32_t field,
178 int32_t start,
179 int32_t limit,
180 UErrorCode* ec) {
181 auto* impl = UConstrainedFieldPositionImpl::validate(ptr, *ec);
182 if (U_FAILURE(*ec)) {
183 return;
184 }
185 impl->fImpl.setState(category, field, start, limit);
186}
187
188U_CAPIextern "C" void U_EXPORT2
189ucfpos_closeucfpos_close_77(UConstrainedFieldPosition* ptr) {
190 UErrorCode localStatus = U_ZERO_ERROR;
191 auto* impl = UConstrainedFieldPositionImpl::validate(ptr, localStatus);
192 delete impl;
193}
194
195
196// -Wreturn-local-addr first found in https://gcc.gnu.org/onlinedocs/gcc-4.8.5/gcc/Warning-Options.html#Warning-Options
197#if U_GCC_MAJOR_MINOR(4 * 100 + 2) >= 409
198#pragma GCC diagnostic push
199#pragma GCC diagnostic ignored "-Wreturn-local-addr"
200#endif
201U_CAPIextern "C" const char16_t* U_EXPORT2
202ufmtval_getStringufmtval_getString_77(
203 const UFormattedValue* ufmtval,
204 int32_t* pLength,
205 UErrorCode* ec) {
206 const auto* impl = UFormattedValueApiHelper::validate(ufmtval, *ec);
207 if (U_FAILURE(*ec)) {
1
Taking false branch
208 return nullptr;
209 }
210 UnicodeString readOnlyAlias = impl->fFormattedValue->toTempString(*ec);
211 if (U_FAILURE(*ec)) {
2
Taking false branch
212 return nullptr;
213 }
214 if (pLength != nullptr) {
3
Assuming the condition is false
4
Taking false branch
215 *pLength = readOnlyAlias.length();
216 }
217 // Note: this line triggers -Wreturn-local-addr, but it is safe because toTempString is
218 // defined to return memory owned by the ufmtval argument.
219 return readOnlyAlias.getBuffer();
5
Address of stack memory associated with local variable 'readOnlyAlias' returned to caller
220}
221#if U_GCC_MAJOR_MINOR(4 * 100 + 2) >= 409
222#pragma GCC diagnostic pop
223#endif
224
225
226U_CAPIextern "C" UBool U_EXPORT2
227ufmtval_nextPositionufmtval_nextPosition_77(
228 const UFormattedValue* ufmtval,
229 UConstrainedFieldPosition* ucfpos,
230 UErrorCode* ec) {
231 const auto* fmtval = UFormattedValueApiHelper::validate(ufmtval, *ec);
232 auto* cfpos = UConstrainedFieldPositionImpl::validate(ucfpos, *ec);
233 if (U_FAILURE(*ec)) {
234 return false;
235 }
236 return fmtval->fFormattedValue->nextPosition(cfpos->fImpl, *ec);
237}
238
239
240U_NAMESPACE_END}
241
242#endif /* #if !UCONFIG_NO_FORMATTING */