Bug Summary

File:out/../deps/icu-small/source/i18n/units_router.cpp
Warning:line 129, column 13
Called C++ object pointer is null

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-unknown-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name units_router.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 -mrelocation-model pic -pic-level 2 -pic-is-pie -mframe-pointer=all -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=/home/maurizio/node-v18.6.0/out -resource-dir /usr/local/lib/clang/16.0.0 -D V8_DEPRECATION_WARNINGS -D V8_IMMINENT_DEPRECATION_WARNINGS -D _GLIBCXX_USE_CXX11_ABI=1 -D NODE_OPENSSL_CONF_NAME=nodejs_conf -D NODE_OPENSSL_HAS_QUIC -D __STDC_FORMAT_MACROS -D OPENSSL_NO_PINSHARED -D OPENSSL_THREADS -D U_COMMON_IMPLEMENTATION=1 -D U_I18N_IMPLEMENTATION=1 -D U_IO_IMPLEMENTATION=1 -D U_TOOLUTIL_IMPLEMENTATION=1 -D U_ATTRIBUTE_DEPRECATED= -D _CRT_SECURE_NO_DEPRECATE= -D U_STATIC_IMPLEMENTATION=1 -D UCONFIG_NO_SERVICE=1 -D U_ENABLE_DYLOAD=0 -D U_HAVE_STD_STRING=1 -D UCONFIG_NO_BREAK_ITERATION=0 -I ../deps/icu-small/source/common -I ../deps/icu-small/source/i18n -I ../deps/icu-small/source/tools/toolutil -internal-isystem /usr/lib/gcc/x86_64-redhat-linux/8/../../../../include/c++/8 -internal-isystem /usr/lib/gcc/x86_64-redhat-linux/8/../../../../include/c++/8/x86_64-redhat-linux -internal-isystem /usr/lib/gcc/x86_64-redhat-linux/8/../../../../include/c++/8/backward -internal-isystem /usr/local/lib/clang/16.0.0/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-redhat-linux/8/../../../../x86_64-redhat-linux/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -O3 -Wno-unused-parameter -Wno-deprecated-declarations -Wno-strict-aliasing -std=gnu++17 -fdeprecated-macro -fdebug-compilation-dir=/home/maurizio/node-v18.6.0/out -ferror-limit 19 -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2022-08-22-142216-507842-1 -x c++ ../deps/icu-small/source/i18n/units_router.cpp
1// © 2020 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 "charstr.h"
9#include "cmemory.h"
10#include "cstring.h"
11#include "measunit_impl.h"
12#include "number_decimalquantity.h"
13#include "number_roundingutils.h"
14#include "resource.h"
15#include "unicode/measure.h"
16#include "units_data.h"
17#include "units_router.h"
18#include <cmath>
19
20U_NAMESPACE_BEGINnamespace icu_71 {
21namespace units {
22
23using number::Precision;
24using number::impl::parseIncrementOption;
25
26Precision UnitsRouter::parseSkeletonToPrecision(icu::UnicodeString precisionSkeleton,
27 UErrorCode &status) {
28 if (U_FAILURE(status)) {
29 // As a member of UsagePrefsHandler, which is a friend of Precision, we
30 // get access to the default constructor.
31 return {};
32 }
33 constexpr int32_t kSkelPrefixLen = 20;
34 if (!precisionSkeleton.startsWith(UNICODE_STRING_SIMPLE("precision-increment/")icu::UnicodeString(true, u"precision-increment/", -1))) {
35 status = U_INVALID_FORMAT_ERROR;
36 return {};
37 }
38 U_ASSERT(precisionSkeleton[kSkelPrefixLen - 1] == u'/')(void)0;
39 StringSegment segment(precisionSkeleton, false);
40 segment.adjustOffset(kSkelPrefixLen);
41 Precision result;
42 parseIncrementOption(segment, result, status);
43 return result;
44}
45
46UnitsRouter::UnitsRouter(StringPiece inputUnitIdentifier, StringPiece region, StringPiece usage,
47 UErrorCode &status) {
48 this->init(MeasureUnit::forIdentifier(inputUnitIdentifier, status), region, usage, status);
49}
50
51UnitsRouter::UnitsRouter(const MeasureUnit &inputUnit, StringPiece region, StringPiece usage,
52 UErrorCode &status) {
53 this->init(std::move(inputUnit), region, usage, status);
54}
55
56void UnitsRouter::init(const MeasureUnit &inputUnit, StringPiece region, StringPiece usage,
57 UErrorCode &status) {
58
59 if (U_FAILURE(status)) {
60 return;
61 }
62
63 // TODO: do we want to pass in ConversionRates and UnitPreferences instead
64 // of loading in each UnitsRouter instance? (Or make global?)
65 ConversionRates conversionRates(status);
66 UnitPreferences prefs(status);
67
68 MeasureUnitImpl inputUnitImpl = MeasureUnitImpl::forMeasureUnitMaybeCopy(inputUnit, status);
69 MeasureUnitImpl baseUnitImpl =
70 (extractCompoundBaseUnit(inputUnitImpl, conversionRates, status));
71 CharString category = getUnitQuantity(baseUnitImpl, status);
72 if (U_FAILURE(status)) {
73 return;
74 }
75
76 const UnitPreference *const *unitPreferences;
77 int32_t preferencesCount = 0;
78 prefs.getPreferencesFor(category.toStringPiece(), usage, region, unitPreferences, preferencesCount,
79 status);
80
81 for (int i = 0; i < preferencesCount; ++i) {
82 U_ASSERT(unitPreferences[i] != nullptr)(void)0;
83 const auto &preference = *unitPreferences[i];
84
85 MeasureUnitImpl complexTargetUnitImpl =
86 MeasureUnitImpl::forIdentifier(preference.unit.data(), status);
87 if (U_FAILURE(status)) {
88 return;
89 }
90
91 UnicodeString precision = preference.skeleton;
92
93 // For now, we only have "precision-increment" in Units Preferences skeleton.
94 // Therefore, we check if the skeleton starts with "precision-increment" and force the program to
95 // fail otherwise.
96 // NOTE:
97 // It is allowed to have an empty precision.
98 if (!precision.isEmpty() && !precision.startsWith(u"precision-increment", 19)) {
99 status = U_INTERNAL_PROGRAM_ERROR;
100 return;
101 }
102
103 outputUnits_.emplaceBackAndCheckErrorCode(status,
104 complexTargetUnitImpl.copy(status).build(status));
105 converterPreferences_.emplaceBackAndCheckErrorCode(status, inputUnitImpl, complexTargetUnitImpl,
106 preference.geq, std::move(precision),
107 conversionRates, status);
108
109 if (U_FAILURE(status)) {
110 return;
111 }
112 }
113}
114
115RouteResult UnitsRouter::route(double quantity, icu::number::impl::RoundingImpl *rounder, UErrorCode &status) const {
116 // Find the matching preference
117 const ConverterPreference *converterPreference = nullptr;
1
'converterPreference' initialized to a null pointer value
118 for (int32_t i = 0, n = converterPreferences_.length(); i < n; i++) {
2
Assuming 'i' is >= 'n'
3
Loop condition is false. Execution continues on line 125
119 converterPreference = converterPreferences_[i];
120 if (converterPreference->converter.greaterThanOrEqual(std::abs(quantity) * (1 + DBL_EPSILON2.2204460492503131e-16),
121 converterPreference->limit)) {
122 break;
123 }
124 }
125 U_ASSERT(converterPreference != nullptr)(void)0;
126
127 // Set up the rounder for this preference's precision
128 if (rounder != nullptr && rounder->fPrecision.isBogus()) {
4
Assuming the condition is true
5
Taking true branch
129 if (converterPreference->precision.length() > 0) {
6
Called C++ object pointer is null
130 rounder->fPrecision = parseSkeletonToPrecision(converterPreference->precision, status);
131 } else {
132 // We use the same rounding mode as COMPACT notation: known to be a
133 // human-friendly rounding mode: integers, but add a decimal digit
134 // as needed to ensure we have at least 2 significant digits.
135 rounder->fPrecision = Precision::integer().withMinDigits(2);
136 }
137 }
138
139 return RouteResult(converterPreference->converter.convert(quantity, rounder, status),
140 converterPreference->targetUnit.copy(status));
141}
142
143const MaybeStackVector<MeasureUnit> *UnitsRouter::getOutputUnits() const {
144 // TODO: consider pulling this from converterPreferences_ and dropping
145 // outputUnits_?
146 return &outputUnits_;
147}
148
149} // namespace units
150U_NAMESPACE_END}
151
152#endif /* #if !UCONFIG_NO_FORMATTING */