Building libunistring with clang on 64-bit Windows, a couple of warnings show up. In particular, clang's -Wshorten-64-to-32 warning is useful, because it highlights places where 64-bit sizes are implicitly truncated to 32-bit. Which means that strings longer than 4 GiB likely trigger bugs.
Here are patches to fix some of these. 2020-09-05 Bruno Haible <[email protected]> uniname/uniname: Fix -Wshadow warning. * lib/uniname/uniname.c (unicode_name_word): Don't declare i upfront. * modules/uniname/uniname (Depends-on): Add c99. 2020-09-05 Bruno Haible <[email protected]> uniname/uniname: Fix -Wshorten-64-to-32 warnings. * lib/uniname/uniname.c (unicode_name_word_lookup): Change type of last argument to size_t. (unicode_name_character): Change type of len, n1, n2, n3, words_length, n to size_t. 2020-09-05 Bruno Haible <[email protected]> unistr/u{8,16,32}-uctomb: Avoid possible trouble with huge strings. * lib/unistr.in.h (u8_uctomb_aux, u8_uctomb, u16_uctomb_aux, u16_uctomb, u32_uctomb): Change type of last argument to ptrdiff_t. * lib/unistr/u8-uctomb.c (u8_uctomb): Likewise. * lib/unistr/u8-uctomb-aux.c (u8_uctomb_aux): Likewise. * lib/unistr/u16-uctomb.c (u16_uctomb): Likewise. * lib/unistr/u16-uctomb-aux.c (u16_uctomb_aux): Likewise. * lib/unistr/u32-uctomb.c (u32_uctomb): Likewise.
>From 13457cbc5281c271fa198c4302badca33931268a Mon Sep 17 00:00:00 2001 From: Bruno Haible <[email protected]> Date: Sat, 5 Sep 2020 15:24:53 +0200 Subject: [PATCH 1/3] unistr/u{8,16,32}-uctomb: Avoid possible trouble with huge strings. * lib/unistr.in.h (u8_uctomb_aux, u8_uctomb, u16_uctomb_aux, u16_uctomb, u32_uctomb): Change type of last argument to ptrdiff_t. * lib/unistr/u8-uctomb.c (u8_uctomb): Likewise. * lib/unistr/u8-uctomb-aux.c (u8_uctomb_aux): Likewise. * lib/unistr/u16-uctomb.c (u16_uctomb): Likewise. * lib/unistr/u16-uctomb-aux.c (u16_uctomb_aux): Likewise. * lib/unistr/u32-uctomb.c (u32_uctomb): Likewise. --- ChangeLog | 11 +++++++++++ lib/unistr.in.h | 18 +++++++++--------- lib/unistr/u16-uctomb-aux.c | 2 +- lib/unistr/u16-uctomb.c | 2 +- lib/unistr/u32-uctomb.c | 2 +- lib/unistr/u8-uctomb-aux.c | 2 +- lib/unistr/u8-uctomb.c | 2 +- 7 files changed, 25 insertions(+), 14 deletions(-) diff --git a/ChangeLog b/ChangeLog index 92320c5..0d50b68 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +2020-09-05 Bruno Haible <[email protected]> + + unistr/u{8,16,32}-uctomb: Avoid possible trouble with huge strings. + * lib/unistr.in.h (u8_uctomb_aux, u8_uctomb, u16_uctomb_aux, u16_uctomb, + u32_uctomb): Change type of last argument to ptrdiff_t. + * lib/unistr/u8-uctomb.c (u8_uctomb): Likewise. + * lib/unistr/u8-uctomb-aux.c (u8_uctomb_aux): Likewise. + * lib/unistr/u16-uctomb.c (u16_uctomb): Likewise. + * lib/unistr/u16-uctomb-aux.c (u16_uctomb_aux): Likewise. + * lib/unistr/u32-uctomb.c (u32_uctomb): Likewise. + 2020-09-01 Paul Eggert <[email protected]> manywarnings: remove -Wchkp and -Wabi from C++ too diff --git a/lib/unistr.in.h b/lib/unistr.in.h index 0acfae6..a0fe3db 100644 --- a/lib/unistr.in.h +++ b/lib/unistr.in.h @@ -25,7 +25,7 @@ /* Get bool. */ #include <stdbool.h> -/* Get size_t. */ +/* Get size_t, ptrdiff_t. */ #include <stddef.h> #ifdef __cplusplus @@ -305,13 +305,13 @@ extern int #if GNULIB_UNISTR_U8_UCTOMB || HAVE_LIBUNISTRING /* Auxiliary function, also used by u8_chr, u8_strchr, u8_strrchr. */ extern int - u8_uctomb_aux (uint8_t *s, ucs4_t uc, int n); + u8_uctomb_aux (uint8_t *s, ucs4_t uc, ptrdiff_t n); # if !HAVE_INLINE extern int - u8_uctomb (uint8_t *s, ucs4_t uc, int n); + u8_uctomb (uint8_t *s, ucs4_t uc, ptrdiff_t n); # else static inline int -u8_uctomb (uint8_t *s, ucs4_t uc, int n) +u8_uctomb (uint8_t *s, ucs4_t uc, ptrdiff_t n) { if (uc < 0x80 && n > 0) { @@ -327,13 +327,13 @@ u8_uctomb (uint8_t *s, ucs4_t uc, int n) #if GNULIB_UNISTR_U16_UCTOMB || HAVE_LIBUNISTRING /* Auxiliary function, also used by u16_chr, u16_strchr, u16_strrchr. */ extern int - u16_uctomb_aux (uint16_t *s, ucs4_t uc, int n); + u16_uctomb_aux (uint16_t *s, ucs4_t uc, ptrdiff_t n); # if !HAVE_INLINE extern int - u16_uctomb (uint16_t *s, ucs4_t uc, int n); + u16_uctomb (uint16_t *s, ucs4_t uc, ptrdiff_t n); # else static inline int -u16_uctomb (uint16_t *s, ucs4_t uc, int n) +u16_uctomb (uint16_t *s, ucs4_t uc, ptrdiff_t n) { if (uc < 0xd800 && n > 0) { @@ -349,10 +349,10 @@ u16_uctomb (uint16_t *s, ucs4_t uc, int n) #if GNULIB_UNISTR_U32_UCTOMB || HAVE_LIBUNISTRING # if !HAVE_INLINE extern int - u32_uctomb (uint32_t *s, ucs4_t uc, int n); + u32_uctomb (uint32_t *s, ucs4_t uc, ptrdiff_t n); # else static inline int -u32_uctomb (uint32_t *s, ucs4_t uc, int n) +u32_uctomb (uint32_t *s, ucs4_t uc, ptrdiff_t n) { if (uc < 0xd800 || (uc >= 0xe000 && uc < 0x110000)) { diff --git a/lib/unistr/u16-uctomb-aux.c b/lib/unistr/u16-uctomb-aux.c index a136b0a..44fbdd5 100644 --- a/lib/unistr/u16-uctomb-aux.c +++ b/lib/unistr/u16-uctomb-aux.c @@ -21,7 +21,7 @@ #include "unistr.h" int -u16_uctomb_aux (uint16_t *s, ucs4_t uc, int n) +u16_uctomb_aux (uint16_t *s, ucs4_t uc, ptrdiff_t n) { if (uc < 0xd800) { diff --git a/lib/unistr/u16-uctomb.c b/lib/unistr/u16-uctomb.c index b4f2b88..77e52e5 100644 --- a/lib/unistr/u16-uctomb.c +++ b/lib/unistr/u16-uctomb.c @@ -28,7 +28,7 @@ #if !HAVE_INLINE int -u16_uctomb (uint16_t *s, ucs4_t uc, int n) +u16_uctomb (uint16_t *s, ucs4_t uc, ptrdiff_t n) { if (uc < 0xd800) { diff --git a/lib/unistr/u32-uctomb.c b/lib/unistr/u32-uctomb.c index 53e835d..73554f1 100644 --- a/lib/unistr/u32-uctomb.c +++ b/lib/unistr/u32-uctomb.c @@ -28,7 +28,7 @@ #if !HAVE_INLINE int -u32_uctomb (uint32_t *s, ucs4_t uc, int n) +u32_uctomb (uint32_t *s, ucs4_t uc, ptrdiff_t n) { if (uc < 0xd800 || (uc >= 0xe000 && uc < 0x110000)) { diff --git a/lib/unistr/u8-uctomb-aux.c b/lib/unistr/u8-uctomb-aux.c index 0b5ec90..4e66c95 100644 --- a/lib/unistr/u8-uctomb-aux.c +++ b/lib/unistr/u8-uctomb-aux.c @@ -23,7 +23,7 @@ #include "attribute.h" int -u8_uctomb_aux (uint8_t *s, ucs4_t uc, int n) +u8_uctomb_aux (uint8_t *s, ucs4_t uc, ptrdiff_t n) { int count; diff --git a/lib/unistr/u8-uctomb.c b/lib/unistr/u8-uctomb.c index 7cf3389..395be77 100644 --- a/lib/unistr/u8-uctomb.c +++ b/lib/unistr/u8-uctomb.c @@ -30,7 +30,7 @@ #if !HAVE_INLINE int -u8_uctomb (uint8_t *s, ucs4_t uc, int n) +u8_uctomb (uint8_t *s, ucs4_t uc, ptrdiff_t n) { if (uc < 0x80) { -- 2.7.4
>From 09af21003026a61e6f1d51f0a2d6d71f04834a78 Mon Sep 17 00:00:00 2001 From: Bruno Haible <[email protected]> Date: Sat, 5 Sep 2020 15:44:29 +0200 Subject: [PATCH 2/3] uniname/uniname: Fix -Wshorten-64-to-32 warnings. * lib/uniname/uniname.c (unicode_name_word_lookup): Change type of last argument to size_t. (unicode_name_character): Change type of len, n1, n2, n3, words_length, n to size_t. --- ChangeLog | 8 ++++++++ lib/uniname/uniname.c | 23 +++++++++++++---------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/ChangeLog b/ChangeLog index 0d50b68..205d98a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,13 @@ 2020-09-05 Bruno Haible <[email protected]> + uniname/uniname: Fix -Wshorten-64-to-32 warnings. + * lib/uniname/uniname.c (unicode_name_word_lookup): Change type of last + argument to size_t. + (unicode_name_character): Change type of len, n1, n2, n3, words_length, + n to size_t. + +2020-09-05 Bruno Haible <[email protected]> + unistr/u{8,16,32}-uctomb: Avoid possible trouble with huge strings. * lib/unistr.in.h (u8_uctomb_aux, u8_uctomb, u16_uctomb_aux, u16_uctomb, u32_uctomb): Change type of last argument to ptrdiff_t. diff --git a/lib/uniname/uniname.c b/lib/uniname/uniname.c index 7cf1e2e..db8399d 100644 --- a/lib/uniname/uniname.c +++ b/lib/uniname/uniname.c @@ -90,7 +90,7 @@ unicode_name_word (unsigned int index, unsigned int *lengthp) /* Looks up the index of a word. */ static int -unicode_name_word_lookup (const char *word, unsigned int length) +unicode_name_word_lookup (const char *word, size_t length) { if (length > 0 && length < SIZEOF (unicode_name_by_length) - 1) { @@ -359,7 +359,7 @@ unicode_character_name (ucs4_t c, char *buf) ucs4_t unicode_name_character (const char *name) { - unsigned int len = strlen (name); + size_t len = strlen (name); if (len > 1 && len <= UNICODE_CHARNAME_MAX_LENGTH) { /* Test for "word1 word2 ..." syntax. */ @@ -469,9 +469,9 @@ unicode_name_character (const char *name) p4++; if (p4 == ptr) { - unsigned int n1 = p2 - p1; - unsigned int n2 = p3 - p2; - unsigned int n3 = p4 - p3; + size_t n1 = p2 - p1; + size_t n2 = p3 - p2; + size_t n3 = p4 - p3; if (n1 <= 2 && (n2 >= 1 && n2 <= 3) && n3 <= 2) { @@ -578,12 +578,15 @@ unicode_name_character (const char *name) filled_words: { /* Multiply by 2, to simplify later comparisons. */ - unsigned int words_length = wordptr - words; + size_t words_length = wordptr - words; { - int i = words_length - 1; + size_t i = words_length - 1; words[i] = 2 * words[i]; - for (; --i >= 0; ) - words[i] = 2 * words[i] + 1; + for (; i > 0; ) + { + --i; + words[i] = 2 * words[i] + 1; + } } /* Binary search in unicode_name_to_index. */ { @@ -594,7 +597,7 @@ unicode_name_character (const char *name) unsigned int i = (i1 + i2) >> 1; const uint16_t *w = words; const uint16_t *p = &unicode_names[unicode_name_to_index[i].name]; - unsigned int n = words_length; + size_t n = words_length; for (;;) { if (*p < *w) -- 2.7.4
>From 8fba1d09bd8e99404cd82670138a98dad44aad94 Mon Sep 17 00:00:00 2001 From: Bruno Haible <[email protected]> Date: Sat, 5 Sep 2020 14:26:49 +0200 Subject: [PATCH 3/3] uniname/uniname: Fix -Wshadow warning. * lib/uniname/uniname.c (unicode_name_word): Don't declare i upfront. * modules/uniname/uniname (Depends-on): Add c99. --- ChangeLog | 6 ++++++ lib/uniname/uniname.c | 3 +-- modules/uniname/uniname | 1 + 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index 205d98a..052d965 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,11 @@ 2020-09-05 Bruno Haible <[email protected]> + uniname/uniname: Fix -Wshadow warning. + * lib/uniname/uniname.c (unicode_name_word): Don't declare i upfront. + * modules/uniname/uniname (Depends-on): Add c99. + +2020-09-05 Bruno Haible <[email protected]> + uniname/uniname: Fix -Wshorten-64-to-32 warnings. * lib/uniname/uniname.c (unicode_name_word_lookup): Change type of last argument to size_t. diff --git a/lib/uniname/uniname.c b/lib/uniname/uniname.c index db8399d..adefa43 100644 --- a/lib/uniname/uniname.c +++ b/lib/uniname/uniname.c @@ -60,7 +60,6 @@ unicode_name_word (unsigned int index, unsigned int *lengthp) { unsigned int i1; unsigned int i2; - unsigned int i; assert (index < UNICODE_CHARNAME_NUM_WORDS); @@ -80,7 +79,7 @@ unicode_name_word (unsigned int index, unsigned int *lengthp) else i2 = i; } - i = i1; + unsigned int i = i1; assert (unicode_name_by_length[i].ind_offset <= index && index < unicode_name_by_length[i+1].ind_offset); *lengthp = i; diff --git a/modules/uniname/uniname b/modules/uniname/uniname index bbd8a99..0f2ffa9 100644 --- a/modules/uniname/uniname +++ b/modules/uniname/uniname @@ -10,6 +10,7 @@ Depends-on: uniname/base attribute memcmp +c99 configure.ac: gl_LIBUNISTRING_MODULE([0.9.8], [uniname/uniname]) -- 2.7.4
