Skip to content

Commit

Permalink
ICU-22767 Fix GCC warning and turn warning to errors
Browse files Browse the repository at this point in the history
  • Loading branch information
FrankYFTang committed Sep 9, 2024
1 parent 74d5e6d commit 99d9fb0
Show file tree
Hide file tree
Showing 22 changed files with 136 additions and 52 deletions.
21 changes: 20 additions & 1 deletion .github/workflows/icu4c.yml
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,25 @@ jobs:
cd $PREFIX/bin;
LD_LIBRARY_PATH=../lib ./icuinfo
gcc-warnings-as-errors:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1

- name: ICU4C with gcc and treat warnings as errors.
env:
PREFIX: /tmp/icu-prefix
CXXFLAGS: -Wextra -Werror -Wno-return-local-addr
CFLAGS: -Werror
run: |
mkdir build;
cd build;
../icu4c/source/runConfigureICU Linux/gcc --disable-layout --disable-layoutex --prefix=$PREFIX;
make -j -l4.5 check;
make -j -l4.5 install;
cd $PREFIX/bin;
LD_LIBRARY_PATH=../lib ./icuinfo
# Clang Linux with address sanitizer.
clang-asan:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -915,4 +934,4 @@ jobs:
uses: actions/upload-artifact@v4
with:
name: icuexportdata_output
path: icuexportdata_tag-goes-here.zip
path: icuexportdata_tag-goes-here.zip
5 changes: 3 additions & 2 deletions icu4c/source/common/ucnvmbcs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3144,13 +3144,14 @@ ucnv_MBCSGetNextUChar(UConverterToUnicodeArgs *pArgs,
}

if(c<0) {
if(U_SUCCESS(*pErrorCode) && source==sourceLimit && lastSource<source) {
if(U_SUCCESS(*pErrorCode) && source==sourceLimit && lastSource<source &&
(size_t)(source-lastSource) <= sizeof(cnv->toUBytes)) {
/* incomplete character byte sequence */
uint8_t *bytes=cnv->toUBytes;
cnv->toULength = static_cast<int8_t>(source - lastSource);
do {
*bytes++=*lastSource++;
} while(lastSource<source);
} while(lastSource<source && bytes <= &(cnv->toUBytes[sizeof(cnv->toUBytes)-1]));
*pErrorCode=U_TRUNCATED_CHAR_FOUND;
} else if(U_FAILURE(*pErrorCode)) {
/* callback(illegal) */
Expand Down
8 changes: 2 additions & 6 deletions icu4c/source/common/ucurr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,12 +372,8 @@ struct CReg : public icu::UMemory {
CReg(const char16_t* _iso, const char* _id)
: next(nullptr)
{
int32_t len = static_cast<int32_t>(uprv_strlen(_id));
if (len > static_cast<int32_t>(sizeof(id) - 1)) {
len = (sizeof(id)-1);
}
uprv_strncpy(id, _id, len);
id[len] = 0;
uprv_strncpy(id, _id, sizeof(id)-1);
id[sizeof(id)-1] = 0;
u_memcpy(iso, _iso, ISO_CURRENCY_CODE_LENGTH);
iso[ISO_CURRENCY_CODE_LENGTH] = 0;
}
Expand Down
5 changes: 5 additions & 0 deletions icu4c/source/common/ushape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "ubidi_props.h"
#include "uassert.h"

#include <limits>
/*
* This implementation is designed for 16-bit Unicode strings.
* The main assumption is that the Arabic characters and their
Expand Down Expand Up @@ -747,6 +748,10 @@ handleGeneratedSpaces(char16_t *dest, int32_t sourceLength,
}
}

if (static_cast<size_t>((sourceLength + 1)) > (std::numeric_limits<size_t>::max() / U_SIZEOF_UCHAR)) {
*pErrorCode = U_INDEX_OUTOFBOUNDS_ERROR;
return 0;
}
tempbuffer = static_cast<char16_t*>(uprv_malloc((sourceLength + 1) * U_SIZEOF_UCHAR));
/* Test for nullptr */
if(tempbuffer == nullptr) {
Expand Down
4 changes: 2 additions & 2 deletions icu4c/source/i18n/calendar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -828,9 +828,9 @@ Calendar::operator=(const Calendar &right)
fWeekendCease = right.fWeekendCease;
fWeekendCeaseMillis = right.fWeekendCeaseMillis;
fNextStamp = right.fNextStamp;
uprv_strncpy(validLocale, right.validLocale, sizeof(validLocale));
uprv_strncpy(actualLocale, right.actualLocale, sizeof(actualLocale));
uprv_strncpy(validLocale, right.validLocale, sizeof(validLocale)-1);
validLocale[sizeof(validLocale)-1] = 0;
uprv_strncpy(actualLocale, right.actualLocale, sizeof(actualLocale)-1);
actualLocale[sizeof(validLocale)-1] = 0;
}

Expand Down
14 changes: 14 additions & 0 deletions icu4c/source/i18n/decNumber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,10 @@ static decNumber * decExpOp(decNumber *, const decNumber *,
static void decFinalize(decNumber *, decContext *, Int *, uInt *);
static Int decGetDigits(Unit *, Int);
static Int decGetInt(const decNumber *);
#ifdef UNUSED_AND_WARNING_FUNCTIONS
static decNumber * decLnOp(decNumber *, const decNumber *,
decContext *, uInt *);
#endif // #ifdef UNUSED_AND_WARNING_FUNCTIONS
static decNumber * decMultiplyOp(decNumber *, const decNumber *,
const decNumber *, decContext *,
uInt *);
Expand Down Expand Up @@ -1289,6 +1291,7 @@ U_CAPI decNumber * U_EXPORT2 uprv_decNumberInvert(decNumber *res, const decNumbe
/* (+11) range needed by Ln, Log10, etc. (which may have to be able */
/* to calculate at p+e+2). */
/* ------------------------------------------------------------------ */
#ifdef UNUSED_AND_WARNING_FUNCTIONS
U_CAPI decNumber * U_EXPORT2 uprv_decNumberLn(decNumber *res, const decNumber *rhs,
decContext *set) {
uInt status=0; /* accumulator */
Expand Down Expand Up @@ -1330,6 +1333,7 @@ U_CAPI decNumber * U_EXPORT2 uprv_decNumberLn(decNumber *res, const decNumber *r
#endif
return res;
} /* decNumberLn */
#endif // #ifdef UNUSED_AND_WARNING_FUNCTIONS

/* ------------------------------------------------------------------ */
/* decNumberLogB - get adjusted exponent, by 754 rules */
Expand Down Expand Up @@ -1411,6 +1415,7 @@ U_CAPI decNumber * U_EXPORT2 uprv_decNumberLogB(decNumber *res, const decNumber
/* fastpath in decLnOp. The final division is done to the requested */
/* precision. */
/* ------------------------------------------------------------------ */
#ifdef UNUSED_AND_WARNING_FUNCTIONS
#if defined(__clang__) || U_GCC_MAJOR_MINOR >= 406
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Warray-bounds"
Expand Down Expand Up @@ -1553,6 +1558,7 @@ U_CAPI decNumber * U_EXPORT2 uprv_decNumberLog10(decNumber *res, const decNumber
#if defined(__clang__) || U_GCC_MAJOR_MINOR >= 406
#pragma GCC diagnostic pop
#endif
#endif // #ifdef UNUSED_AND_WARNING_FUNCTIONS

/* ------------------------------------------------------------------ */
/* decNumberMax -- compare two Numbers and return the maximum */
Expand Down Expand Up @@ -1971,6 +1977,7 @@ U_CAPI decNumber * U_EXPORT2 uprv_decNumberMultiply(decNumber *res, const decNum
/* almost always be correctly rounded, but may be up to 1 ulp in */
/* error in rare cases. */
/* ------------------------------------------------------------------ */
#ifdef UNUSED_AND_WARNING_FUNCTIONS
U_CAPI decNumber * U_EXPORT2 uprv_decNumberPower(decNumber *res, const decNumber *lhs,
const decNumber *rhs, decContext *set) {
#if DECSUBSET
Expand Down Expand Up @@ -2297,6 +2304,7 @@ U_CAPI decNumber * U_EXPORT2 uprv_decNumberPower(decNumber *res, const decNumber
#endif
return res;
} /* decNumberPower */
#endif // #ifdef UNUSED_AND_WARNING_FUNCTIONS

/* ------------------------------------------------------------------ */
/* decNumberQuantize -- force exponent to requested value */
Expand Down Expand Up @@ -2826,6 +2834,7 @@ U_CAPI decNumber * U_EXPORT2 uprv_decNumberShift(decNumber *res, const decNumber
/* result setexp(approx, e div 2) % fix exponent */
/* end sqrt */
/* ------------------------------------------------------------------ */
#ifdef UNUSED_AND_WARNING_FUNCTIONS
#if defined(__clang__) || U_GCC_MAJOR_MINOR >= 406
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Warray-bounds"
Expand Down Expand Up @@ -3162,6 +3171,7 @@ U_CAPI decNumber * U_EXPORT2 uprv_decNumberSquareRoot(decNumber *res, const decN
#if defined(__clang__) || U_GCC_MAJOR_MINOR >= 406
#pragma GCC diagnostic pop
#endif
#endif // #ifdef UNUSED_AND_WARNING_FUNCTIONS

/* ------------------------------------------------------------------ */
/* decNumberSubtract -- subtract two Numbers */
Expand Down Expand Up @@ -5550,6 +5560,7 @@ decNumber * decExpOp(decNumber *res, const decNumber *rhs,
/* where x is truncated (NB) into the range 10 through 99, */
/* and then c = k>>2 and e = k&3. */
/* ------------------------------------------------------------------ */
#ifdef UNUSED_AND_WARNING_FUNCTIONS
static const uShort LNnn[90]={9016, 8652, 8316, 8008, 7724, 7456, 7208,
6972, 6748, 6540, 6340, 6148, 5968, 5792, 5628, 5464, 5312,
5164, 5020, 4884, 4748, 4620, 4496, 4376, 4256, 4144, 4032,
Expand All @@ -5560,6 +5571,7 @@ static const uShort LNnn[90]={9016, 8652, 8316, 8008, 7724, 7456, 7208,
10197, 9685, 9177, 8677, 8185, 7697, 7213, 6737, 6269, 5801,
5341, 4889, 4437, 39930, 35534, 31186, 26886, 22630, 18418, 14254,
10130, 6046, 20055};
#endif // #ifdef UNUSED_AND_WARNING_FUNCTIONS

/* ------------------------------------------------------------------ */
/* decLnOp -- effect natural logarithm */
Expand Down Expand Up @@ -5622,6 +5634,7 @@ static const uShort LNnn[90]={9016, 8652, 8316, 8008, 7724, 7456, 7208,
/* 5. The static buffers are larger than might be expected to allow */
/* for calls from decNumberPower. */
/* ------------------------------------------------------------------ */
#ifdef UNUSED_AND_WARNING_FUNCTIONS
#if defined(__clang__) || U_GCC_MAJOR_MINOR >= 406
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Warray-bounds"
Expand Down Expand Up @@ -5850,6 +5863,7 @@ decNumber * decLnOp(decNumber *res, const decNumber *rhs,
#if defined(__clang__) || U_GCC_MAJOR_MINOR >= 406
#pragma GCC diagnostic pop
#endif
#endif // #ifdef UNUSED_AND_WARNING_FUNCTIONS

/* ------------------------------------------------------------------ */
/* decQuantizeOp -- force exponent to requested value */
Expand Down
9 changes: 9 additions & 0 deletions icu4c/source/i18n/formattedvalue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,12 @@ ucfpos_close(UConstrainedFieldPosition* ptr) {
}


// -Wreturn-local-addr first found in https://gcc.gnu.org/onlinedocs/gcc-4.8.5/gcc/Warning-Options.html#Warning-Options
#if U_GCC_MAJOR_MINOR >= 409
#pragma GCC diagnostic push
#pragma GCC diagnostic warning "-Wreturn-local-addr"
#pragma GCC diagnostic ignored "-Wreturn-local-addr"
#endif
U_CAPI const char16_t* U_EXPORT2
ufmtval_getString(
const UFormattedValue* ufmtval,
Expand All @@ -213,6 +219,9 @@ ufmtval_getString(
// defined to return memory owned by the ufmtval argument.
return readOnlyAlias.getBuffer();
}
#if U_GCC_MAJOR_MINOR >= 409
#pragma GCC diagnostic pop
#endif


U_CAPI UBool U_EXPORT2
Expand Down
8 changes: 8 additions & 0 deletions icu4c/source/i18n/number_rounding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,10 @@ FractionPrecision Precision::constructFraction(int32_t minFrac, int32_t maxFrac)
settings.fMaxFrac = static_cast<digits_t>(maxFrac);
settings.fMinSig = -1;
settings.fMaxSig = -1;
// The following are not used under RND_FRACTION but initialize to avoid
// warning.
settings.fPriority = UNUM_ROUNDING_PRIORITY_RELAXED;
settings.fRetain = false;
PrecisionUnion union_;
union_.fracSig = settings;
return {RND_FRACTION, union_};
Expand All @@ -294,6 +298,10 @@ Precision Precision::constructSignificant(int32_t minSig, int32_t maxSig) {
settings.fMaxFrac = -1;
settings.fMinSig = static_cast<digits_t>(minSig);
settings.fMaxSig = static_cast<digits_t>(maxSig);
// The following are not used under RND_SIGNIFICANT, but initialize to avoid
// warning.
settings.fPriority = UNUM_ROUNDING_PRIORITY_RELAXED;
settings.fRetain = false;
PrecisionUnion union_;
union_.fracSig = settings;
return {RND_SIGNIFICANT, union_};
Expand Down
9 changes: 9 additions & 0 deletions icu4c/source/i18n/number_skeletons.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1015,6 +1015,12 @@ blueprint_helpers::parseExponentSignOption(const StringSegment& segment, MacroPr
return true;
}

// The function is called by skeleton::parseOption which called by skeleton::parseSkeleton
// the data pointed in the return macros.unit is stack allocated in the parseSkeleton function.
#if U_GCC_MAJOR_MINOR >= 1204
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdangling-pointer"
#endif
void blueprint_helpers::parseCurrencyOption(const StringSegment& segment, MacroProps& macros,
UErrorCode& status) {
// Unlike ICU4J, have to check length manually because ICU4C CurrencyUnit does not check it for us
Expand All @@ -1034,6 +1040,9 @@ void blueprint_helpers::parseCurrencyOption(const StringSegment& segment, MacroP
// Slicing is OK
macros.unit = currency; // NOLINT
}
#if U_GCC_MAJOR_MINOR >= 1204
#pragma GCC diagnostic pop
#endif

void
blueprint_helpers::generateCurrencyOption(const CurrencyUnit& currency, UnicodeString& sb, UErrorCode&) {
Expand Down
1 change: 1 addition & 0 deletions icu4c/source/test/cintltst/cbiditst.c
Original file line number Diff line number Diff line change
Expand Up @@ -4730,6 +4730,7 @@ checkMaps(UBiDi *pBiDi, int32_t stringIndex, const char *src, const char *dest,
);
testOK = false;
}
memset(getIndexMap, 0, sizeof(getIndexMap));
for (i = 0; i < srcLen; i++) {
idx = ubidi_getVisualIndex(pBiDi, i, &rc);
assertSuccessful("ubidi_getVisualIndex", &rc);
Expand Down
18 changes: 9 additions & 9 deletions icu4c/source/test/cintltst/custrtrn.c
Original file line number Diff line number Diff line change
Expand Up @@ -1527,7 +1527,7 @@ static void Test_strToJavaModifiedUTF8(void) {
0xee, 0x80, 0x81, 0xee, 0x80, 0x82, 0xee, 0x80, 0x83,
0xed, 0xa0, 0x80, 0xed, 0xb0, 0x80, 0xed, 0xb0, 0x80, 0xed, 0xa0, 0x80, 0xc0, 0x80,
0xed, 0xaf, 0xbf, 0xed, 0xbf, 0xbf,
0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0xc3, 0xad, 0xe0, 0xb8, 0x8e, 0x6f
0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0xc3, 0xad, 0xe0, 0xb8, 0x8e, 0x6f, 0
};
static const UChar shortSrc[]={
0xe01, 0xe1, 0x61
Expand All @@ -1554,7 +1554,7 @@ static void Test_strToJavaModifiedUTF8(void) {
p=u_strToJavaModifiedUTF8(dest, (int32_t)sizeof(dest), &length,
src, UPRV_LENGTHOF(src), &errorCode);
if( U_FAILURE(errorCode) || p!=dest ||
length!=UPRV_LENGTHOF(expected) || 0!=memcmp(dest, expected, length) ||
length!=(UPRV_LENGTHOF(expected)-1) || 0!=memcmp(dest, expected, length) ||
dest[length]!=0
) {
log_err("u_strToJavaModifiedUTF8(normal) failed - %s\n", u_errorName(errorCode));
Expand All @@ -1565,18 +1565,18 @@ static void Test_strToJavaModifiedUTF8(void) {
p=u_strToJavaModifiedUTF8(dest, (int32_t)sizeof(dest), NULL,
src, UPRV_LENGTHOF(src), &errorCode);
if( U_FAILURE(errorCode) || p!=dest ||
0!=memcmp(dest, expected, UPRV_LENGTHOF(expected)) ||
dest[UPRV_LENGTHOF(expected)]!=0
0!=memcmp(dest, expected, (UPRV_LENGTHOF(expected)-1)) ||
dest[(UPRV_LENGTHOF(expected)-1)]!=0
) {
log_err("u_strToJavaModifiedUTF8(normal, pLength=NULL) failed - %s\n", u_errorName(errorCode));
}
memset(dest, 0xff, sizeof(dest));
errorCode=U_ZERO_ERROR;
length=-5;
p=u_strToJavaModifiedUTF8(dest, UPRV_LENGTHOF(expected), &length,
p=u_strToJavaModifiedUTF8(dest, (UPRV_LENGTHOF(expected)-1), &length,
src, UPRV_LENGTHOF(src), &errorCode);
if( errorCode!=U_STRING_NOT_TERMINATED_WARNING || p!=dest ||
length!=UPRV_LENGTHOF(expected) || 0!=memcmp(dest, expected, length) ||
length!=(UPRV_LENGTHOF(expected)-1) || 0!=memcmp(dest, expected, length) ||
dest[length]!=(char)0xff
) {
log_err("u_strToJavaModifiedUTF8(tight) failed - %s\n", u_errorName(errorCode));
Expand Down Expand Up @@ -1604,10 +1604,10 @@ static void Test_strToJavaModifiedUTF8(void) {
memset(dest, 0xff, sizeof(dest));
errorCode=U_ZERO_ERROR;
length=-5;
p=u_strToJavaModifiedUTF8(dest, UPRV_LENGTHOF(expected)/2, &length,
p=u_strToJavaModifiedUTF8(dest, (UPRV_LENGTHOF(expected)-1)/2, &length,
src, UPRV_LENGTHOF(src), &errorCode);
if( errorCode!=U_BUFFER_OVERFLOW_ERROR ||
length!=UPRV_LENGTHOF(expected) || dest[UPRV_LENGTHOF(expected)/2]!=(char)0xff
length!=(UPRV_LENGTHOF(expected)-1) || dest[(UPRV_LENGTHOF(expected)-1)/2]!=(char)0xff
) {
log_err("u_strToJavaModifiedUTF8(overflow) failed - %s\n", u_errorName(errorCode));
}
Expand All @@ -1617,7 +1617,7 @@ static void Test_strToJavaModifiedUTF8(void) {
p=u_strToJavaModifiedUTF8(NULL, 0, &length,
src, UPRV_LENGTHOF(src), &errorCode);
if( errorCode!=U_BUFFER_OVERFLOW_ERROR ||
length!=UPRV_LENGTHOF(expected) || dest[0]!=(char)0xff
length!=(UPRV_LENGTHOF(expected)-1) || dest[0]!=(char)0xff
) {
log_err("u_strToJavaModifiedUTF8(pure preflighting) failed - %s\n", u_errorName(errorCode));
}
Expand Down
2 changes: 1 addition & 1 deletion icu4c/source/test/cintltst/hpmufn.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ static void * U_CALLCONV myMemRealloc(const void *context, void *mem, size_t siz
}
retPtr = realloc(p, size+sizeof(ctest_AlignedMemory));
if (retPtr != NULL) {
p += sizeof(ctest_AlignedMemory);
retPtr += sizeof(ctest_AlignedMemory);
}
return retPtr;
}
Expand Down
8 changes: 7 additions & 1 deletion icu4c/source/test/cintltst/ucptrietest.c
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,13 @@ trieTestGolden(const char *testName,
goto cleanup;
}
fseek(stream, 0, SEEK_SET);
fread(memoryBuffer, 1, fsize, stream);
long rsize = fread(memoryBuffer, 1, fsize, stream);
if (rsize != fsize) {
log_err(
"Golden files for '%s' fread %d bytes fail. Only get %d",
testName, fsize, rsize);
}


int32_t testResult = uprv_compareGoldenFiles(
memoryBuffer, fsize,
Expand Down
2 changes: 1 addition & 1 deletion icu4c/source/test/cintltst/uformattedvaluetst.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ static void AssertAllPartsEqual(

UErrorCode status = U_ZERO_ERROR;

char message[256];
char message[257];
uprv_strncpy(message, messagePrefix, 256);
int32_t prefixEnd = (int32_t)uprv_strlen(messagePrefix);
message[prefixEnd++] = ':';
Expand Down
7 changes: 7 additions & 0 deletions icu4c/source/test/cintltst/utf16tst.c
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,10 @@ static void TestNextPrevChar(void){

}

#if U_GCC_MAJOR_MINOR >= 406
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Warray-bounds"
#endif
/* keep this in sync with utf8tst.c's TestNulTerminated() */
static void TestNulTerminated(void) {
static const UChar input[]={
Expand Down Expand Up @@ -477,6 +481,9 @@ static void TestNulTerminated(void) {
} while(++j<i);
} while(c!=0);
}
#if U_GCC_MAJOR_MINOR >= 406
#pragma GCC diagnostic pop
#endif

static void TestFwdBack(void){
static UChar input[]={0x0061, 0xd800, 0xdc00, 0xdbff, 0xdfff, 0x0062, 0xd841, 0xd7ff, 0xd841, 0xdc41, 0xdc00, 0x0000};
Expand Down
Loading

0 comments on commit 99d9fb0

Please sign in to comment.