Skip to content

Commit 8437d1d

Browse files
committed
ICU-22767 Fix GCC warning and turn warning to errors
See unicode-org#3129
1 parent 3cd97ad commit 8437d1d

File tree

19 files changed

+87
-61
lines changed

19 files changed

+87
-61
lines changed

.github/workflows/icu4c.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ jobs:
7171

7272
- name: ICU4C with gcc
7373
env:
74+
CXXFLAGS: -Wextra -Werror -Wno-error=return-local-addr
75+
CFLAGS: -Werror
7476
PREFIX: /tmp/icu-prefix
7577
run: |
7678
mkdir build;

icu4c/source/common/ucnvmbcs.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3146,11 +3146,8 @@ ucnv_MBCSGetNextUChar(UConverterToUnicodeArgs *pArgs,
31463146
if(c<0) {
31473147
if(U_SUCCESS(*pErrorCode) && source==sourceLimit && lastSource<source) {
31483148
/* incomplete character byte sequence */
3149-
uint8_t *bytes=cnv->toUBytes;
31503149
cnv->toULength = static_cast<int8_t>(source - lastSource);
3151-
do {
3152-
*bytes++=*lastSource++;
3153-
} while(lastSource<source);
3150+
uprv_memcpy(cnv->toUBytes, lastSource, cnv->toULength);
31543151
*pErrorCode=U_TRUNCATED_CHAR_FOUND;
31553152
} else if(U_FAILURE(*pErrorCode)) {
31563153
/* callback(illegal) */

icu4c/source/common/ucurr.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -372,12 +372,8 @@ struct CReg : public icu::UMemory {
372372
CReg(const char16_t* _iso, const char* _id)
373373
: next(nullptr)
374374
{
375-
int32_t len = static_cast<int32_t>(uprv_strlen(_id));
376-
if (len > static_cast<int32_t>(sizeof(id) - 1)) {
377-
len = (sizeof(id)-1);
378-
}
379-
uprv_strncpy(id, _id, len);
380-
id[len] = 0;
375+
uprv_strncpy(id, _id, sizeof(id)-1);
376+
id[sizeof(id)-1] = 0;
381377
u_memcpy(iso, _iso, ISO_CURRENCY_CODE_LENGTH);
382378
iso[ISO_CURRENCY_CODE_LENGTH] = 0;
383379
}

icu4c/source/common/ushape.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "ubidi_props.h"
2929
#include "uassert.h"
3030

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

751+
if (static_cast<size_t>(sourceLength) + 1 > std::numeric_limits<size_t>::max() / U_SIZEOF_UCHAR) {
752+
*pErrorCode = U_INDEX_OUTOFBOUNDS_ERROR;
753+
return 0;
754+
}
750755
tempbuffer = static_cast<char16_t*>(uprv_malloc((sourceLength + 1) * U_SIZEOF_UCHAR));
751756
/* Test for nullptr */
752757
if(tempbuffer == nullptr) {

icu4c/source/i18n/calendar.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -828,9 +828,9 @@ Calendar::operator=(const Calendar &right)
828828
fWeekendCease = right.fWeekendCease;
829829
fWeekendCeaseMillis = right.fWeekendCeaseMillis;
830830
fNextStamp = right.fNextStamp;
831-
uprv_strncpy(validLocale, right.validLocale, sizeof(validLocale));
832-
uprv_strncpy(actualLocale, right.actualLocale, sizeof(actualLocale));
831+
uprv_strncpy(validLocale, right.validLocale, sizeof(validLocale)-1);
833832
validLocale[sizeof(validLocale)-1] = 0;
833+
uprv_strncpy(actualLocale, right.actualLocale, sizeof(actualLocale)-1);
834834
actualLocale[sizeof(validLocale)-1] = 0;
835835
}
836836

icu4c/source/i18n/formattedvalue.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,11 @@ ucfpos_close(UConstrainedFieldPosition* ptr) {
193193
}
194194

195195

196+
// -Wreturn-local-addr first found in https://gcc.gnu.org/onlinedocs/gcc-4.8.5/gcc/Warning-Options.html#Warning-Options
197+
#if U_GCC_MAJOR_MINOR >= 409
198+
#pragma GCC diagnostic push
199+
#pragma GCC diagnostic ignored "-Wreturn-local-addr"
200+
#endif
196201
U_CAPI const char16_t* U_EXPORT2
197202
ufmtval_getString(
198203
const UFormattedValue* ufmtval,
@@ -213,6 +218,9 @@ ufmtval_getString(
213218
// defined to return memory owned by the ufmtval argument.
214219
return readOnlyAlias.getBuffer();
215220
}
221+
#if U_GCC_MAJOR_MINOR >= 409
222+
#pragma GCC diagnostic pop
223+
#endif
216224

217225

218226
U_CAPI UBool U_EXPORT2

icu4c/source/i18n/number_rounding.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -278,23 +278,23 @@ Precision IncrementPrecision::withMinFraction(int32_t minFrac) const {
278278
}
279279

280280
FractionPrecision Precision::constructFraction(int32_t minFrac, int32_t maxFrac) {
281-
FractionSignificantSettings settings;
281+
FractionSignificantSettings settings{};
282282
settings.fMinFrac = static_cast<digits_t>(minFrac);
283283
settings.fMaxFrac = static_cast<digits_t>(maxFrac);
284284
settings.fMinSig = -1;
285285
settings.fMaxSig = -1;
286-
PrecisionUnion union_;
286+
PrecisionUnion union_{};
287287
union_.fracSig = settings;
288288
return {RND_FRACTION, union_};
289289
}
290290

291291
Precision Precision::constructSignificant(int32_t minSig, int32_t maxSig) {
292-
FractionSignificantSettings settings;
292+
FractionSignificantSettings settings{};
293293
settings.fMinFrac = -1;
294294
settings.fMaxFrac = -1;
295295
settings.fMinSig = static_cast<digits_t>(minSig);
296296
settings.fMaxSig = static_cast<digits_t>(maxSig);
297-
PrecisionUnion union_;
297+
PrecisionUnion union_{};
298298
union_.fracSig = settings;
299299
return {RND_SIGNIFICANT, union_};
300300
}
@@ -311,20 +311,20 @@ Precision::constructFractionSignificant(
311311
settings.fMaxSig = static_cast<digits_t>(maxSig);
312312
settings.fPriority = priority;
313313
settings.fRetain = retain;
314-
PrecisionUnion union_;
314+
PrecisionUnion union_{};
315315
union_.fracSig = settings;
316316
return {RND_FRACTION_SIGNIFICANT, union_};
317317
}
318318

319319
IncrementPrecision Precision::constructIncrement(uint64_t increment, digits_t magnitude) {
320-
IncrementSettings settings;
320+
IncrementSettings settings{};
321321
// Note: For number formatting, fIncrement is used for RND_INCREMENT but not
322322
// RND_INCREMENT_ONE or RND_INCREMENT_FIVE. However, fIncrement is used in all
323323
// three when constructing a skeleton.
324324
settings.fIncrement = increment;
325325
settings.fIncrementMagnitude = magnitude;
326326
settings.fMinFrac = magnitude > 0 ? 0 : -magnitude;
327-
PrecisionUnion union_;
327+
PrecisionUnion union_{};
328328
union_.increment = settings;
329329
if (increment == 1) {
330330
// NOTE: In C++, we must return the correct value type with the correct union.
@@ -339,7 +339,7 @@ IncrementPrecision Precision::constructIncrement(uint64_t increment, digits_t ma
339339
}
340340

341341
CurrencyPrecision Precision::constructCurrency(UCurrencyUsage usage) {
342-
PrecisionUnion union_;
342+
PrecisionUnion union_{};
343343
union_.currencyUsage = usage;
344344
return {RND_CURRENCY, union_};
345345
}

icu4c/source/i18n/number_skeletons.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,6 +1015,12 @@ blueprint_helpers::parseExponentSignOption(const StringSegment& segment, MacroPr
10151015
return true;
10161016
}
10171017

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

10381047
void
10391048
blueprint_helpers::generateCurrencyOption(const CurrencyUnit& currency, UnicodeString& sb, UErrorCode&) {

icu4c/source/test/cintltst/cbiditst.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4730,6 +4730,7 @@ checkMaps(UBiDi *pBiDi, int32_t stringIndex, const char *src, const char *dest,
47304730
);
47314731
testOK = false;
47324732
}
4733+
memset(getIndexMap, 0, sizeof(getIndexMap));
47334734
for (i = 0; i < srcLen; i++) {
47344735
idx = ubidi_getVisualIndex(pBiDi, i, &rc);
47354736
assertSuccessful("ubidi_getVisualIndex", &rc);

icu4c/source/test/cintltst/custrtrn.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1527,7 +1527,7 @@ static void Test_strToJavaModifiedUTF8(void) {
15271527
0xee, 0x80, 0x81, 0xee, 0x80, 0x82, 0xee, 0x80, 0x83,
15281528
0xed, 0xa0, 0x80, 0xed, 0xb0, 0x80, 0xed, 0xb0, 0x80, 0xed, 0xa0, 0x80, 0xc0, 0x80,
15291529
0xed, 0xaf, 0xbf, 0xed, 0xbf, 0xbf,
1530-
0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0xc3, 0xad, 0xe0, 0xb8, 0x8e, 0x6f
1530+
0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0xc3, 0xad, 0xe0, 0xb8, 0x8e, 0x6f, 0
15311531
};
15321532
static const UChar shortSrc[]={
15331533
0xe01, 0xe1, 0x61
@@ -1554,7 +1554,7 @@ static void Test_strToJavaModifiedUTF8(void) {
15541554
p=u_strToJavaModifiedUTF8(dest, (int32_t)sizeof(dest), &length,
15551555
src, UPRV_LENGTHOF(src), &errorCode);
15561556
if( U_FAILURE(errorCode) || p!=dest ||
1557-
length!=UPRV_LENGTHOF(expected) || 0!=memcmp(dest, expected, length) ||
1557+
length!=(UPRV_LENGTHOF(expected)-1) || 0!=memcmp(dest, expected, length) ||
15581558
dest[length]!=0
15591559
) {
15601560
log_err("u_strToJavaModifiedUTF8(normal) failed - %s\n", u_errorName(errorCode));
@@ -1565,18 +1565,18 @@ static void Test_strToJavaModifiedUTF8(void) {
15651565
p=u_strToJavaModifiedUTF8(dest, (int32_t)sizeof(dest), NULL,
15661566
src, UPRV_LENGTHOF(src), &errorCode);
15671567
if( U_FAILURE(errorCode) || p!=dest ||
1568-
0!=memcmp(dest, expected, UPRV_LENGTHOF(expected)) ||
1569-
dest[UPRV_LENGTHOF(expected)]!=0
1568+
0!=memcmp(dest, expected, (UPRV_LENGTHOF(expected)-1)) ||
1569+
dest[(UPRV_LENGTHOF(expected)-1)]!=0
15701570
) {
15711571
log_err("u_strToJavaModifiedUTF8(normal, pLength=NULL) failed - %s\n", u_errorName(errorCode));
15721572
}
15731573
memset(dest, 0xff, sizeof(dest));
15741574
errorCode=U_ZERO_ERROR;
15751575
length=-5;
1576-
p=u_strToJavaModifiedUTF8(dest, UPRV_LENGTHOF(expected), &length,
1576+
p=u_strToJavaModifiedUTF8(dest, (UPRV_LENGTHOF(expected)-1), &length,
15771577
src, UPRV_LENGTHOF(src), &errorCode);
15781578
if( errorCode!=U_STRING_NOT_TERMINATED_WARNING || p!=dest ||
1579-
length!=UPRV_LENGTHOF(expected) || 0!=memcmp(dest, expected, length) ||
1579+
length!=(UPRV_LENGTHOF(expected)-1) || 0!=memcmp(dest, expected, length) ||
15801580
dest[length]!=(char)0xff
15811581
) {
15821582
log_err("u_strToJavaModifiedUTF8(tight) failed - %s\n", u_errorName(errorCode));
@@ -1604,10 +1604,10 @@ static void Test_strToJavaModifiedUTF8(void) {
16041604
memset(dest, 0xff, sizeof(dest));
16051605
errorCode=U_ZERO_ERROR;
16061606
length=-5;
1607-
p=u_strToJavaModifiedUTF8(dest, UPRV_LENGTHOF(expected)/2, &length,
1607+
p=u_strToJavaModifiedUTF8(dest, (UPRV_LENGTHOF(expected)-1)/2, &length,
16081608
src, UPRV_LENGTHOF(src), &errorCode);
16091609
if( errorCode!=U_BUFFER_OVERFLOW_ERROR ||
1610-
length!=UPRV_LENGTHOF(expected) || dest[UPRV_LENGTHOF(expected)/2]!=(char)0xff
1610+
length!=(UPRV_LENGTHOF(expected)-1) || dest[(UPRV_LENGTHOF(expected)-1)/2]!=(char)0xff
16111611
) {
16121612
log_err("u_strToJavaModifiedUTF8(overflow) failed - %s\n", u_errorName(errorCode));
16131613
}
@@ -1617,7 +1617,7 @@ static void Test_strToJavaModifiedUTF8(void) {
16171617
p=u_strToJavaModifiedUTF8(NULL, 0, &length,
16181618
src, UPRV_LENGTHOF(src), &errorCode);
16191619
if( errorCode!=U_BUFFER_OVERFLOW_ERROR ||
1620-
length!=UPRV_LENGTHOF(expected) || dest[0]!=(char)0xff
1620+
length!=(UPRV_LENGTHOF(expected)-1) || dest[0]!=(char)0xff
16211621
) {
16221622
log_err("u_strToJavaModifiedUTF8(pure preflighting) failed - %s\n", u_errorName(errorCode));
16231623
}

0 commit comments

Comments
 (0)