Skip to content

Commit f7b9a7b

Browse files
committed
Fix static analyzer errors
1 parent 4c1e137 commit f7b9a7b

File tree

14 files changed

+83
-21
lines changed

14 files changed

+83
-21
lines changed

icu4c/source/common/filteredbrk.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,11 @@ SimpleFilteredBreakIteratorBuilder::build(BreakIterator* adoptBreakIterator, UEr
603603
LocalArray<UnicodeString> ustrs(ustrs_ptr);
604604

605605
LocalMemory<int> partials;
606-
partials.allocateInsteadAndReset(subCount);
606+
// Check subCount > 0 to distinguish allocation failure from empty set (both return nullptr)
607+
if (subCount > 0 && partials.allocateInsteadAndReset(subCount) == nullptr) {
608+
status = U_MEMORY_ALLOCATION_ERROR;
609+
return nullptr;
610+
}
607611

608612
LocalPointer<UCharsTrie> backwardsTrie; // i.e. ".srM" for Mrs.
609613
LocalPointer<UCharsTrie> forwardsPartialTrie; // Has ".a" for "a.M."

icu4c/source/common/loclikelysubtags.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,10 @@ LSR LikelySubtags::makeMaximizedLsrFrom(const Locale &locale,
534534
}
535535
LSR max = makeMaximizedLsr(locale.getLanguage(), locale.getScript(), locale.getCountry(),
536536
locale.getVariant(), returnInputIfUnmatch, errorCode);
537-
537+
if (!U_SUCCESS(errorCode)) {
538+
return LSR(name, "", "", LSR::EXPLICIT_LSR);
539+
}
540+
538541
if (uprv_strlen(max.language) == 0 &&
539542
uprv_strlen(max.script) == 0 &&
540543
uprv_strlen(max.region) == 0) {

icu4c/source/common/normalizer2impl.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,10 @@ Normalizer2Impl::copyLowPrefixFromNulTerminated(const char16_t *src,
533533
// data and check the first part of the string.
534534
// After this prefix, determine the string length to simplify the rest
535535
// of the code.
536+
if(src==nullptr) {
537+
errorCode=U_ILLEGAL_ARGUMENT_ERROR;
538+
return src;
539+
}
536540
const char16_t *prevSrc=src;
537541
char16_t c;
538542
while((c=*src++)<minNeedDataCP && c!=0) {}
@@ -1731,6 +1735,9 @@ Normalizer2Impl::composeQuickCheck(const char16_t *src, const char16_t *limit,
17311735
if(limit==nullptr) {
17321736
UErrorCode errorCode=U_ZERO_ERROR;
17331737
src=copyLowPrefixFromNulTerminated(src, minNoMaybeCP, nullptr, errorCode);
1738+
if (U_FAILURE(errorCode)) {
1739+
return src;
1740+
}
17341741
limit=u_strchr(src, 0);
17351742
if (prevBoundary != src) {
17361743
if (hasCompBoundaryAfter(*(src-1), onlyContiguous)) {

icu4c/source/common/ubidiln.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,12 @@ ubidi_getVisualRun(UBiDi *pBiDi, int32_t runIndex,
364364
{
365365
int32_t start;
366366
UErrorCode errorCode = U_ZERO_ERROR;
367+
if(pLogicalStart!=nullptr) {
368+
*pLogicalStart=0;
369+
}
370+
if(pLength!=nullptr) {
371+
*pLength=0;
372+
}
367373
RETURN_IF_NOT_VALID_PARA_OR_LINE(pBiDi, errorCode, UBIDI_LTR);
368374
ubidi_getRuns(pBiDi, &errorCode);
369375
if(U_FAILURE(errorCode)) {
@@ -585,6 +591,10 @@ ubidi_getRuns(UBiDi *pBiDi, UErrorCode*) {
585591
}
586592
}
587593

594+
if (runCount==0) {
595+
return false;
596+
}
597+
588598
/*
589599
* We don't need to see if the last run can be merged with a trailing
590600
* WS run because setTrailingWSStart() would have done that.
@@ -645,6 +655,7 @@ ubidi_getRuns(UBiDi *pBiDi, UErrorCode*) {
645655
/* there is a separate WS run */
646656
runs[runIndex].logicalStart=limit;
647657
runs[runIndex].visualLimit=length-limit;
658+
runs[runIndex].insertRemove=0;
648659
/* For the trailing WS run, pBiDi->paraLevel is ok even
649660
if contextual multiple paragraphs. */
650661
if(pBiDi->paraLevel<minLevel) {

icu4c/source/common/ubidiwrt.cpp

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,12 @@ doWriteForward(const char16_t *src, int32_t srcLength,
6060
char16_t *dest, int32_t destSize,
6161
uint16_t options,
6262
UErrorCode *pErrorCode) {
63-
if (srcLength<=0) {
64-
*pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
65-
return 0;
66-
}
6763
/* optimize for several combinations of options */
6864
switch(options&(UBIDI_REMOVE_BIDI_CONTROLS|UBIDI_DO_MIRRORING)) {
6965
case 0: {
7066
/* simply copy the LTR run to the destination */
7167
int32_t length=srcLength;
72-
if(destSize<length) {
68+
if(!destSize || destSize<length) {
7369
*pErrorCode=U_BUFFER_OVERFLOW_ERROR;
7470
return srcLength;
7571
}
@@ -83,7 +79,7 @@ doWriteForward(const char16_t *src, int32_t srcLength,
8379
int32_t i=0, j=0;
8480
UChar32 c;
8581

86-
if(destSize<srcLength) {
82+
if(!destSize || destSize<srcLength) {
8783
*pErrorCode=U_BUFFER_OVERFLOW_ERROR;
8884
return srcLength;
8985
}
@@ -178,11 +174,6 @@ doWriteReverse(const char16_t *src, int32_t srcLength,
178174
int32_t i, j;
179175
UChar32 c;
180176

181-
if(srcLength<=0) {
182-
*pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
183-
return 0;
184-
}
185-
186177
/* optimize for several combinations of options */
187178
switch(options&(UBIDI_REMOVE_BIDI_CONTROLS|UBIDI_DO_MIRRORING|UBIDI_KEEP_BASE_COMBINING)) {
188179
case 0:
@@ -192,7 +183,7 @@ doWriteReverse(const char16_t *src, int32_t srcLength,
192183
* and there is no mirroring and no keeping combining characters
193184
* with their base characters.
194185
*/
195-
if(destSize<srcLength) {
186+
if(!destSize || destSize<srcLength) {
196187
*pErrorCode=U_BUFFER_OVERFLOW_ERROR;
197188
return srcLength;
198189
}
@@ -220,7 +211,7 @@ doWriteReverse(const char16_t *src, int32_t srcLength,
220211
* and there is no mirroring.
221212
* We do need to keep combining characters with their base characters.
222213
*/
223-
if(destSize<srcLength) {
214+
if(!destSize || destSize<srcLength) {
224215
*pErrorCode=U_BUFFER_OVERFLOW_ERROR;
225216
return srcLength;
226217
}
@@ -269,7 +260,7 @@ doWriteReverse(const char16_t *src, int32_t srcLength,
269260
src-=srcLength;
270261
}
271262

272-
if(destSize<i) {
263+
if(!destSize || destSize<i) {
273264
*pErrorCode=U_BUFFER_OVERFLOW_ERROR;
274265
return i;
275266
}

icu4c/source/common/ucnv.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,7 @@ _fromUnicodeWithCallback(UConverterFromUnicodeArgs *pArgs, UErrorCode *err) {
843843
const char16_t *realSource, *realSourceLimit;
844844
int32_t realSourceIndex;
845845
UBool realFlush;
846+
UBool needRestore=false;
846847

847848
cnv=pArgs->converter;
848849
s=pArgs->source;
@@ -891,6 +892,7 @@ _fromUnicodeWithCallback(UConverterFromUnicodeArgs *pArgs, UErrorCode *err) {
891892
pArgs->sourceLimit=replay-cnv->preFromULength;
892893
pArgs->flush=false;
893894
sourceIndex=-1;
895+
needRestore=true;
894896

895897
cnv->preFromULength=0;
896898
}
@@ -982,6 +984,7 @@ _fromUnicodeWithCallback(UConverterFromUnicodeArgs *pArgs, UErrorCode *err) {
982984
if((sourceIndex+=cnv->preFromULength)<0) {
983985
sourceIndex=-1;
984986
}
987+
needRestore=true;
985988

986989
cnv->preFromULength=0;
987990
} else {
@@ -1002,14 +1005,15 @@ _fromUnicodeWithCallback(UConverterFromUnicodeArgs *pArgs, UErrorCode *err) {
10021005
* (continue converting by breaking out of only the inner loop)
10031006
*/
10041007
break;
1005-
} else if(realSource!=nullptr) {
1008+
} else if(needRestore) {
10061009
/* switch back from replaying to the real source and continue */
10071010
pArgs->source=realSource;
10081011
pArgs->sourceLimit=realSourceLimit;
10091012
pArgs->flush=realFlush;
10101013
sourceIndex=realSourceIndex;
10111014

10121015
realSource=nullptr;
1016+
needRestore=false;
10131017
break;
10141018
} else if(pArgs->flush && cnv->fromUChar32!=0) {
10151019
/*
@@ -1065,7 +1069,7 @@ _fromUnicodeWithCallback(UConverterFromUnicodeArgs *pArgs, UErrorCode *err) {
10651069
* copied back into the UConverter
10661070
* and the real arguments must be restored
10671071
*/
1068-
if(realSource!=nullptr) {
1072+
if(needRestore) {
10691073
int32_t length;
10701074

10711075
U_ASSERT(cnv->preFromULength==0);
@@ -1079,6 +1083,7 @@ _fromUnicodeWithCallback(UConverterFromUnicodeArgs *pArgs, UErrorCode *err) {
10791083
pArgs->source=realSource;
10801084
pArgs->sourceLimit=realSourceLimit;
10811085
pArgs->flush=realFlush;
1086+
needRestore=false;
10821087
}
10831088

10841089
return;

icu4c/source/common/ucnv2022.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,13 +178,18 @@ enum { MAX_JA_VERSION=0 };
178178
#else
179179
enum { MAX_JA_VERSION=4 };
180180
#endif
181-
static const uint16_t jpCharsetMasks[MAX_JA_VERSION+1]={
181+
static const uint16_t jpCharsetMasks[5]={
182182
CSM(ASCII)|CSM(JISX201)|CSM(JISX208)|CSM(HWKANA_7BIT),
183183
#if !UCONFIG_ONLY_HTML_CONVERSION
184184
CSM(ASCII)|CSM(JISX201)|CSM(JISX208)|CSM(HWKANA_7BIT)|CSM(JISX212),
185185
CSM(ASCII)|CSM(JISX201)|CSM(JISX208)|CSM(HWKANA_7BIT)|CSM(JISX212)|CSM(GB2312)|CSM(KSC5601)|CSM(ISO8859_1)|CSM(ISO8859_7),
186186
CSM(ASCII)|CSM(JISX201)|CSM(JISX208)|CSM(HWKANA_7BIT)|CSM(JISX212)|CSM(GB2312)|CSM(KSC5601)|CSM(ISO8859_1)|CSM(ISO8859_7),
187187
CSM(ASCII)|CSM(JISX201)|CSM(JISX208)|CSM(HWKANA_7BIT)|CSM(JISX212)|CSM(GB2312)|CSM(KSC5601)|CSM(ISO8859_1)|CSM(ISO8859_7)
188+
#else
189+
CSM(ASCII)|CSM(JISX201)|CSM(JISX208)|CSM(HWKANA_7BIT),
190+
CSM(ASCII)|CSM(JISX201)|CSM(JISX208)|CSM(HWKANA_7BIT),
191+
CSM(ASCII)|CSM(JISX201)|CSM(JISX208)|CSM(HWKANA_7BIT),
192+
CSM(ASCII)|CSM(JISX201)|CSM(JISX208)|CSM(HWKANA_7BIT)
188193
#endif
189194
};
190195

icu4c/source/common/unames.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -999,6 +999,11 @@ enumAlgNames(AlgorithmicRange *range,
999999
uint16_t prefixLength, i, idx;
10001000

10011001
char c;
1002+
1003+
/* guard against invalid data */
1004+
if(count==0 || count>8) {
1005+
return true;
1006+
}
10021007

10031008
/* name = prefix factorized-elements */
10041009

@@ -1025,7 +1030,7 @@ enumAlgNames(AlgorithmicRange *range,
10251030
while(++start<limit) {
10261031
/* increment the indexes in lexical order bound by the factors */
10271032
i=count;
1028-
for (;;) {
1033+
while (i>0) {
10291034
idx = static_cast<uint16_t>(indexes[--i] + 1);
10301035
if(idx<factors[i]) {
10311036
/* skip one index and its element string */
@@ -1129,6 +1134,11 @@ findAlgName(AlgorithmicRange *range, UCharNameChoice nameChoice, const char *oth
11291134

11301135
char c;
11311136

1137+
/* guard against invalid data */
1138+
if(count==0 || count>8) {
1139+
return true;
1140+
}
1141+
11321142
/* name = prefix factorized-elements */
11331143

11341144
/* compare prefix */
@@ -1154,7 +1164,7 @@ findAlgName(AlgorithmicRange *range, UCharNameChoice nameChoice, const char *oth
11541164
while(++start<limit) {
11551165
/* increment the indexes in lexical order bound by the factors */
11561166
i=count;
1157-
for (;;) {
1167+
while (i>0) {
11581168
idx = static_cast<uint16_t>(indexes[--i] + 1);
11591169
if(idx<factors[i]) {
11601170
/* skip one index and its element string */

icu4c/source/common/unormcmp.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,9 @@ unorm_cmpEquivFold(const char16_t *s1, int32_t length1,
329329
* the decomposition would replace the entire code point
330330
*/
331331
--s2;
332+
if (s2==fold2) {
333+
return c1-c2;
334+
}
332335
c2=*(s2-1);
333336
}
334337
}

icu4c/source/common/ustrcase.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1714,6 +1714,10 @@ static int32_t _cmpFold(
17141714
* the decomposition would replace the entire code point
17151715
*/
17161716
--s2;
1717+
if (s2==fold2) {
1718+
cmpRes=c1-c2;
1719+
break;
1720+
}
17171721
--m2;
17181722
c2=*(s2-1);
17191723
}

0 commit comments

Comments
 (0)