diff options
author | Jan Dubiec <jdx@o2.pl> | 2025-03-02 18:20:13 +0100 |
---|---|---|
committer | Corinna Vinschen <corinna@vinschen.de> | 2025-03-03 11:45:36 +0100 |
commit | 7962e3b26e5d6e8a1228baa63350321c8d9f31de (patch) | |
tree | a3c0491421dde3b9b8b565f01ef5063263adc365 /newlib/libc | |
parent | 627785b540bd9e119ad76cdac2dbe39221fdd2f2 (diff) | |
download | newlib-7962e3b26e5d6e8a1228baa63350321c8d9f31de.zip newlib-7962e3b26e5d6e8a1228baa63350321c8d9f31de.tar.gz newlib-7962e3b26e5d6e8a1228baa63350321c8d9f31de.tar.bz2 |
Silence -Wshift-count-overflow warnings
This patch fixes a few "left shift count >= width of type
[-Wshift-count-overflow]" warnings. Before shifting a char 16 (or more)
bits left first it explicitly casts the char to uint32_t. The existing
code relies on implicit casts to int and assumes that ints are 32-bit.
This is not always true because the C standard does not require int to
be 32-bit and there are targets (e.g. H8/300) where by default int is
indeed 16-bit.
2025-03-02 Jan Dubiec <jdx@o2.pl>
newlib/ChangeLog:
* libc/stdlib/gdtoa-gdtoa.c (gdtoa): Cast to __ULong before left shift.
* libc/string/memmem.c (memmem): Cast to uint32_t before left shift.
* libc/string/strstr.c (strstr2): Ditto.
(strstr3): Ditto.
(strstr4): Ditto.
newlib/libc/stdlib/gdtoa-gdtoa.c | 2 +-
newlib/libc/string/memmem.c | 3 ++-
newlib/libc/string/strstr.c | 6 +++---
3 files changed, 6 insertions(+), 5 deletions(-)
Diffstat (limited to 'newlib/libc')
-rw-r--r-- | newlib/libc/stdlib/gdtoa-gdtoa.c | 2 | ||||
-rw-r--r-- | newlib/libc/string/memmem.c | 3 | ||||
-rw-r--r-- | newlib/libc/string/strstr.c | 6 |
3 files changed, 6 insertions, 5 deletions
diff --git a/newlib/libc/stdlib/gdtoa-gdtoa.c b/newlib/libc/stdlib/gdtoa-gdtoa.c index da2338c..d29a81e 100644 --- a/newlib/libc/stdlib/gdtoa-gdtoa.c +++ b/newlib/libc/stdlib/gdtoa-gdtoa.c @@ -261,7 +261,7 @@ gdtoa dval(d) *= 1 << j1; word0(d) += j << Exp_shift - 2 & Exp_mask; #else - word0(d) += (be + bbits - 1) << Exp_shift; + word0(d) += (__ULong)(be + bbits - 1) << Exp_shift; #endif if (k >= 0 && k <= Ten_pmax) { if (dval(d) < tens[k]) diff --git a/newlib/libc/string/memmem.c b/newlib/libc/string/memmem.c index 65267b9..13d8c0a 100644 --- a/newlib/libc/string/memmem.c +++ b/newlib/libc/string/memmem.c @@ -129,7 +129,8 @@ memmem (const void *haystack, size_t hs_len, const void *needle, size_t ne_len) if (ne_len == 2) { - uint32_t nw = ne[0] << 16 | ne[1], hw = hs[0] << 16 | hs[1]; + uint32_t nw = ((uint32_t)ne[0] << 16) | ne[1], + hw = ((uint32_t)hs[0] << 16) | hs[1]; for (hs++; hs <= end && hw != nw; ) hw = hw << 16 | *++hs; return hw == nw ? (void *)(hs - 1) : NULL; diff --git a/newlib/libc/string/strstr.c b/newlib/libc/string/strstr.c index 84e4632..50dbbec 100644 --- a/newlib/libc/string/strstr.c +++ b/newlib/libc/string/strstr.c @@ -100,7 +100,7 @@ strstr (const char *hs, const char *ne) static inline char * strstr2 (const unsigned char *hs, const unsigned char *ne) { - uint32_t h1 = (ne[0] << 16) | ne[1]; + uint32_t h1 = ((uint32_t)ne[0] << 16) | ne[1]; uint32_t h2 = 0; int c; for (c = hs[0]; h1 != h2 && c != 0; c = *++hs) @@ -111,7 +111,7 @@ strstr2 (const unsigned char *hs, const unsigned char *ne) static inline char * strstr3 (const unsigned char *hs, const unsigned char *ne) { - uint32_t h1 = (ne[0] << 24) | (ne[1] << 16) | (ne[2] << 8); + uint32_t h1 = ((uint32_t)ne[0] << 24) | ((uint32_t)ne[1] << 16) | (ne[2] << 8); uint32_t h2 = 0; int c; for (c = hs[0]; h1 != h2 && c != 0; c = *++hs) @@ -122,7 +122,7 @@ strstr3 (const unsigned char *hs, const unsigned char *ne) static inline char * strstr4 (const unsigned char *hs, const unsigned char *ne) { - uint32_t h1 = (ne[0] << 24) | (ne[1] << 16) | (ne[2] << 8) | ne[3]; + uint32_t h1 = ((uint32_t)ne[0] << 24) | ((uint32_t)ne[1] << 16) | (ne[2] << 8) | ne[3]; uint32_t h2 = 0; int c; for (c = hs[0]; c != 0 && h1 != h2; c = *++hs) |