Skip to content

Commit 464e9f1

Browse files
committed
ICU-23267 Fix static analyzer errors
1 parent 0525ee9 commit 464e9f1

18 files changed

+67
-17
lines changed

icu4c/source/common/putil.cpp

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -915,8 +915,15 @@ static UBool compareBinaryFiles(const char* defaultTZFileName, const char* TZFil
915915
fseek(tzInfo->defaultTZFilePtr, 0, SEEK_END);
916916
tzInfo->defaultTZFileSize = ftell(tzInfo->defaultTZFilePtr);
917917
}
918-
fseek(file, 0, SEEK_END);
918+
if (fseek(file, 0, SEEK_END) != 0) {
919+
fclose(file);
920+
return false;
921+
}
919922
sizeFile = ftell(file);
923+
if (sizeFile == -1) {
924+
fclose(file);
925+
return false;
926+
}
920927
sizeFileLeft = sizeFile;
921928

922929
if (sizeFile != tzInfo->defaultTZFileSize) {
@@ -926,16 +933,26 @@ static UBool compareBinaryFiles(const char* defaultTZFileName, const char* TZFil
926933
* compare each byte to determine equality.
927934
*/
928935
if (tzInfo->defaultTZBuffer == nullptr) {
929-
rewind(tzInfo->defaultTZFilePtr);
936+
if (fseek(tzInfo->defaultTZFilePtr, 0, SEEK_SET) != 0) {
937+
fclose(file);
938+
return false;
939+
}
930940
tzInfo->defaultTZBuffer = static_cast<char*>(uprv_malloc(sizeof(char) * tzInfo->defaultTZFileSize));
931941
sizeFileRead = fread(tzInfo->defaultTZBuffer, 1, tzInfo->defaultTZFileSize, tzInfo->defaultTZFilePtr);
932942
}
933-
rewind(file);
943+
if (fseek(file, 0, SEEK_SET) != 0) {
944+
fclose(file);
945+
return false;
946+
}
934947
while(sizeFileLeft > 0) {
935948
uprv_memset(bufferFile, 0, MAX_READ_SIZE);
936949
sizeFileToRead = sizeFileLeft < MAX_READ_SIZE ? sizeFileLeft : MAX_READ_SIZE;
937950

938951
sizeFileRead = fread(bufferFile, 1, sizeFileToRead, file);
952+
if (sizeFileRead == -1) {
953+
fclose(file);
954+
return false;
955+
}
939956
if (memcmp(tzInfo->defaultTZBuffer + tzInfo->defaultTZPosition, bufferFile, sizeFileRead) != 0) {
940957
result = false;
941958
break;

icu4c/source/common/ucnv.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,10 @@ _fromUnicodeWithCallback(UConverterFromUnicodeArgs *pArgs, UErrorCode *err) {
877877
* and failed to consume all of them.
878878
* We need to "replay" them from a temporary buffer and convert them first.
879879
*/
880+
if (!pArgs->source) {
881+
*err=U_ILLEGAL_ARGUMENT_ERROR;
882+
return;
883+
}
880884
realSource=pArgs->source;
881885
realSourceLimit=pArgs->sourceLimit;
882886
realFlush=pArgs->flush;
@@ -1322,6 +1326,10 @@ _toUnicodeWithCallback(UConverterToUnicodeArgs *pArgs, UErrorCode *err) {
13221326
* and failed to consume all of them.
13231327
* We need to "replay" them from a temporary buffer and convert them first.
13241328
*/
1329+
if (!pArgs->source) {
1330+
*err=U_ILLEGAL_ARGUMENT_ERROR;
1331+
return;
1332+
}
13251333
realSource=pArgs->source;
13261334
realSourceLimit=pArgs->sourceLimit;
13271335
realFlush=pArgs->flush;

icu4c/source/common/udata.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1291,7 +1291,7 @@ doOpenChoice(const char *path, const char *type, const char *name,
12911291
dataPath = u_getDataDirectory();
12921292

12931293
/**** Time zone individual files override */
1294-
if (isICUData && isTimeZoneFile(name, type)) {
1294+
if (isICUData && type != nullptr && isTimeZoneFile(name, type)) {
12951295
const char *tzFilesDir = u_getTimeZoneFilesDirectory(pErrorCode);
12961296
if (tzFilesDir[0] != 0) {
12971297
#ifdef UDATA_DEBUG

icu4c/source/common/uenum.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ uenum_unextDefault(UEnumeration* en,
9999
if (ustr == nullptr) {
100100
*status = U_MEMORY_ALLOCATION_ERROR;
101101
} else {
102-
u_charsToUChars(cstr, ustr, len+1);
102+
u_charsToUChars(cstr, ustr, len);
103+
ustr[len] = 0;
103104
}
104105
}
105106
} else {
@@ -129,7 +130,8 @@ uenum_nextDefault(UEnumeration* en,
129130
*status = U_MEMORY_ALLOCATION_ERROR;
130131
return nullptr;
131132
}
132-
u_UCharsToChars(tempUCharVal, tempCharVal, *resultLength + 1);
133+
u_UCharsToChars(tempUCharVal, tempCharVal, *resultLength);
134+
tempCharVal[*resultLength] = 0;
133135
return tempCharVal;
134136
} else {
135137
*status = U_UNSUPPORTED_ERROR;

icu4c/source/common/uidna.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,7 @@ uidna_compare( const char16_t *s1, int32_t length1,
915915

916916
if (U_FAILURE(bufferStatus)) {
917917
*status = bufferStatus;
918+
goto CLEANUP;
918919
}
919920

920921
// when toASCII is applied all label separators are replaced with FULL_STOP

icu4c/source/common/uniset.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ UnicodeSet& UnicodeSet::copyFrom(const UnicodeSet& o, UBool asThawed) {
241241
} else if (hasStrings()) {
242242
strings_->removeAllElements();
243243
}
244-
if (o.stringSpan != nullptr && !asThawed) {
244+
if (o.stringSpan != nullptr && strings_ != nullptr && !asThawed) {
245245
stringSpan = new UnicodeSetStringSpan(*o.stringSpan, *strings_);
246246
if (stringSpan == nullptr) { // Check for memory allocation error.
247247
setToBogus();

icu4c/source/common/ushape.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -982,7 +982,7 @@ expandCompositCharAtEnd(char16_t *dest, int32_t sourceLength, int32_t destSize,U
982982

983983
uprv_memset(tempbuffer, 0, (sourceLength+1)*U_SIZEOF_UCHAR);
984984

985-
while(dest[inpsize-1] == SPACE_CHAR) {
985+
while(inpsize>0 && dest[inpsize-1] == SPACE_CHAR) {
986986
countr++;
987987
inpsize--;
988988
}
@@ -1525,6 +1525,11 @@ u_shapeArabic(const char16_t *source, int32_t sourceLength,
15251525
prevLink = currLink;
15261526
currLink = getLink(source[i]);
15271527
if (aggregate_tashkeel && ((prevLink|currLink)&COMBINE) == COMBINE && aggregation_possible) {
1528+
if (j<0 || j>=2*sourceLength) {
1529+
*pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
1530+
if (tempsource != nullptr) uprv_free(tempsource);
1531+
return 0;
1532+
}
15281533
aggregation_possible = 0;
15291534
tempsource[j] = (prev<source[i]?prev:source[i])-0x064C+0xFC5E;
15301535
currLink = getLink(tempsource[j]);

icu4c/source/i18n/anytrans.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,9 @@ void AnyTransliterator::registerIDs() {
359359

360360
UErrorCode ec = U_ZERO_ERROR;
361361
Hashtable seen(true, ec);
362-
362+
if (U_FAILURE(ec)) {
363+
return;
364+
}
363365
int32_t sourceCount = Transliterator::_countAvailableSources();
364366
for (int32_t s=0; s<sourceCount; ++s) {
365367
UnicodeString source;

icu4c/source/i18n/collationsettings.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,10 @@ CollationSettings::setReorderArrays(const int32_t *codes, int32_t codesLength,
195195
int32_t *ownedCodes;
196196
int32_t totalLength = codesLength + rangesLength;
197197
U_ASSERT(totalLength > 0);
198+
if (totalLength <= 0) {
199+
errorCode = U_ILLEGAL_ARGUMENT_ERROR;
200+
return;
201+
}
198202
if(totalLength <= reorderCodesCapacity) {
199203
ownedCodes = const_cast<int32_t *>(reorderCodes);
200204
} else {

icu4c/source/i18n/nfrs.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,9 @@ NFRuleSet::findFractionRuleSetRule(double number) const
602602
// all the precision we need, and we can do all of the rest
603603
// of the math using integer arithmetic
604604
int64_t leastCommonMultiple = rules[0]->getBaseValue();
605+
if (leastCommonMultiple == 0) {
606+
return nullptr;
607+
}
605608
int64_t numerator;
606609
{
607610
for (uint32_t i = 1; i < rules.size(); ++i) {

0 commit comments

Comments
 (0)