Skip to content

Commit 883a651

Browse files
committed
Fix static analyzer bugs
1 parent 0525ee9 commit 883a651

File tree

7 files changed

+59
-37
lines changed

7 files changed

+59
-37
lines changed

icu4c/source/common/cstring.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,12 +329,14 @@ uprv_strndup(const char *src, int32_t n) {
329329

330330
if(n < 0) {
331331
dup = uprv_strdup(src);
332-
} else {
332+
} else if (n < INT32_MAX) {
333333
dup = (char*)uprv_malloc(n+1);
334334
if (dup) {
335335
uprv_memcpy(dup, src, n);
336336
dup[n] = 0;
337337
}
338+
} else {
339+
dup = nullptr;
338340
}
339341

340342
return dup;

icu4c/source/common/messagepattern.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1164,7 +1164,7 @@ MessagePattern::setParseError(UParseError *parseError, int32_t index) {
11641164
parseError->preContext[length]=0;
11651165

11661166
// Set postContext to some of msg starting at index.
1167-
length=msg.length()-index;
1167+
length=msg.length()>index ? msg.length()-index : 0;
11681168
if(length>=U_PARSE_CONTEXT_LEN) {
11691169
length=U_PARSE_CONTEXT_LEN-1;
11701170
if(length>0 && U16_IS_LEAD(msg[index+length-1])) {

icu4c/source/common/ubidiln.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ ubidi_getRuns(UBiDi *pBiDi, UErrorCode*) {
620620

621621
/* search for the run limits and initialize visualLimit values with the run lengths */
622622
i=0;
623-
do {
623+
while(i<limit) {
624624
/* prepare this run */
625625
start=i;
626626
level=levels[i];
@@ -639,7 +639,7 @@ ubidi_getRuns(UBiDi *pBiDi, UErrorCode*) {
639639
runs[runIndex].visualLimit=i-start;
640640
runs[runIndex].insertRemove=0;
641641
++runIndex;
642-
} while(i<limit);
642+
}
643643

644644
if(limit<length) {
645645
/* there is a separate WS run */

icu4c/source/i18n/number_longnames.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1688,8 +1688,9 @@ void MixedUnitLongNameHandler::processQuantity(DecimalQuantity &quantity, MicroP
16881688
const Modifier *MixedUnitLongNameHandler::getMixedUnitModifier(DecimalQuantity &quantity,
16891689
MicroProps &micros,
16901690
UErrorCode &status) const {
1691-
if (micros.mixedMeasuresCount == 0) {
1691+
if (micros.mixedMeasuresCount == 0 || micros.mixedMeasuresCount > fMixedUnitCount) {
16921692
U_ASSERT(micros.mixedMeasuresCount > 0); // Mixed unit: we must have more than one unit value
1693+
U_ASSERT(micros.mixedMeasuresCount > fMixedUnitCount);
16931694
status = U_UNSUPPORTED_ERROR;
16941695
return &micros.helpers.emptyWeakModifier;
16951696
}

icu4c/source/i18n/rbt_pars.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1496,6 +1496,12 @@ int32_t TransliteratorParser::syntaxError(UErrorCode parseErrorCode,
14961496
int32_t pos,
14971497
UErrorCode& status)
14981498
{
1499+
if (pos < 0) {
1500+
pos = 0;
1501+
}
1502+
if (pos > rule.length()) {
1503+
pos = rule.length();
1504+
}
14991505
parseError.offset = pos;
15001506
parseError.line = 0 ; /* we are not using line numbers */
15011507

icu4c/source/i18n/rematch.cpp

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4783,15 +4783,17 @@ void RegexMatcher::MatchChunkAt(int32_t startIdx, UBool toEnd, UErrorCode &statu
47834783

47844784
UChar32 c;
47854785
U16_NEXT(inputBuf, fp->fInputIdx, fActiveLimit, c);
4786-
if (c < 256) {
4787-
Regex8BitSet &s8 = RegexStaticSets::gStaticSets->fPropSets8[opValue];
4788-
if (s8.contains(c)) {
4789-
success = !success;
4790-
}
4791-
} else {
4792-
const UnicodeSet &s = RegexStaticSets::gStaticSets->fPropSets[opValue];
4793-
if (s.contains(c)) {
4794-
success = !success;
4786+
if (c >= 0) {
4787+
if (c < 256) {
4788+
Regex8BitSet &s8 = RegexStaticSets::gStaticSets->fPropSets8[opValue];
4789+
if (s8.contains(c)) {
4790+
success = !success;
4791+
}
4792+
} else {
4793+
const UnicodeSet &s = RegexStaticSets::gStaticSets->fPropSets[opValue];
4794+
if (s.contains(c)) {
4795+
success = !success;
4796+
}
47954797
}
47964798
}
47974799
if (!success) {
@@ -4815,15 +4817,17 @@ void RegexMatcher::MatchChunkAt(int32_t startIdx, UBool toEnd, UErrorCode &statu
48154817

48164818
UChar32 c;
48174819
U16_NEXT(inputBuf, fp->fInputIdx, fActiveLimit, c);
4818-
if (c < 256) {
4819-
Regex8BitSet &s8 = RegexStaticSets::gStaticSets->fPropSets8[opValue];
4820-
if (s8.contains(c) == false) {
4821-
break;
4822-
}
4823-
} else {
4824-
const UnicodeSet &s = RegexStaticSets::gStaticSets->fPropSets[opValue];
4825-
if (s.contains(c) == false) {
4826-
break;
4820+
if (c >= 0) {
4821+
if (c < 256) {
4822+
Regex8BitSet &s8 = RegexStaticSets::gStaticSets->fPropSets8[opValue];
4823+
if (s8.contains(c) == false) {
4824+
break;
4825+
}
4826+
} else {
4827+
const UnicodeSet &s = RegexStaticSets::gStaticSets->fPropSets[opValue];
4828+
if (s.contains(c) == false) {
4829+
break;
4830+
}
48274831
}
48284832
}
48294833
fp = reinterpret_cast<REStackFrame*>(fStack->popFrame(fFrameSize));
@@ -4844,20 +4848,21 @@ void RegexMatcher::MatchChunkAt(int32_t startIdx, UBool toEnd, UErrorCode &statu
48444848
// There is input left. Pick up one char and test it for set membership.
48454849
UChar32 c;
48464850
U16_NEXT(inputBuf, fp->fInputIdx, fActiveLimit, c);
4847-
if (c<256) {
4848-
Regex8BitSet *s8 = &fPattern->fSets8[opValue];
4849-
if (s8->contains(c)) {
4850-
// The character is in the set. A Match.
4851-
break;
4852-
}
4853-
} else {
4854-
UnicodeSet* s = static_cast<UnicodeSet*>(fSets->elementAt(opValue));
4855-
if (s->contains(c)) {
4856-
// The character is in the set. A Match.
4857-
break;
4851+
if (c >= 0) {
4852+
if (c<256) {
4853+
Regex8BitSet *s8 = &fPattern->fSets8[opValue];
4854+
if (s8->contains(c)) {
4855+
// The character is in the set. A Match.
4856+
break;
4857+
}
4858+
} else {
4859+
UnicodeSet* s = static_cast<UnicodeSet*>(fSets->elementAt(opValue));
4860+
if (s->contains(c)) {
4861+
// The character is in the set. A Match.
4862+
break;
4863+
}
48584864
}
48594865
}
4860-
48614866
// the character wasn't in the set.
48624867
fp = reinterpret_cast<REStackFrame*>(fStack->popFrame(fFrameSize));
48634868
}

icu4c/source/i18n/simpletz.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,7 +1140,11 @@ SimpleTimeZone::initTransitionRules(UErrorCode& status) {
11401140
}
11411141

11421142
// Calculate the first DST start time
1143-
dstRule->getFirstStart(getRawOffset(), 0, firstDstStart);
1143+
if (!dstRule->getFirstStart(getRawOffset(), 0, firstDstStart)) {
1144+
status = U_ILLEGAL_ARGUMENT_ERROR;
1145+
deleteTransitionRules();
1146+
return;
1147+
}
11441148

11451149
// Create a TimeZoneRule for standard time
11461150
timeRuleType = (endTimeMode == STANDARD_TIME) ? DateTimeRule::STANDARD_TIME :
@@ -1178,7 +1182,11 @@ SimpleTimeZone::initTransitionRules(UErrorCode& status) {
11781182
}
11791183

11801184
// Calculate the first STD start time
1181-
stdRule->getFirstStart(getRawOffset(), dstRule->getDSTSavings(), firstStdStart);
1185+
if (!stdRule->getFirstStart(getRawOffset(), dstRule->getDSTSavings(), firstStdStart)) {
1186+
status = U_ILLEGAL_ARGUMENT_ERROR;
1187+
deleteTransitionRules();
1188+
return;
1189+
}
11821190

11831191
// Create a TimeZoneRule for initial time
11841192
if (firstStdStart < firstDstStart) {

0 commit comments

Comments
 (0)