]> Savannah Git Hosting - gnulib.git/commitdiff
iconv_open: Fix undefined behaviour.
authorBruno Haible <bruno@clisp.org>
Sat, 5 Oct 2024 00:45:49 +0000 (02:45 +0200)
committerBruno Haible <bruno@clisp.org>
Wed, 16 Oct 2024 12:03:25 +0000 (14:03 +0200)
Reported by Tim Sweet <tsweet64@protonmail.com>
at <https://savannah.gnu.org/bugs/?66289>.

* lib/iconv.c (utf32be_mbtowc, utf32le_mbtowc): Cast 'unsigned char'
values to ucs4_t before shifting them to the left.

ChangeLog
lib/iconv.c

index 8b822becc451880ca2a9a422d4616ec4bac85fc2..cc09d8145974622deabe4cedefc91b9ea755ec2d 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2024-10-04  Bruno Haible  <bruno@clisp.org>
+
+       iconv_open: Fix undefined behaviour.
+       Reported by Tim Sweet <tsweet64@protonmail.com>
+       at <https://savannah.gnu.org/bugs/?66289>.
+       * lib/iconv.c (utf32be_mbtowc, utf32le_mbtowc): Cast 'unsigned char'
+       values to ucs4_t before shifting them to the left.
+
 2024-09-23  Bruno Haible  <bruno@clisp.org>
 
        getopt-posix: Fix compilation error in C++ mode (regression 2024-09-21).
index 310f4043eb4586b101c0cf6a5a6dac8df7f0a0df..f7a67798fb030b5950f737da611e61f612a0caa2 100644 (file)
@@ -195,7 +195,10 @@ utf32be_mbtowc (ucs4_t *pwc, const unsigned char *s, size_t n)
 {
   if (n >= 4)
     {
-      ucs4_t wc = (s[0] << 24) + (s[1] << 16) + (s[2] << 8) + s[3];
+      ucs4_t wc =   ((ucs4_t) s[0] << 24)
+                  + ((ucs4_t) s[1] << 16)
+                  + ((ucs4_t) s[2] << 8)
+                  +  (ucs4_t) s[3];
       if (wc < 0x110000 && !(wc >= 0xd800 && wc < 0xe000))
         {
           *pwc = wc;
@@ -237,7 +240,10 @@ utf32le_mbtowc (ucs4_t *pwc, const unsigned char *s, size_t n)
 {
   if (n >= 4)
     {
-      ucs4_t wc = s[0] + (s[1] << 8) + (s[2] << 16) + (s[3] << 24);
+      ucs4_t wc =    (ucs4_t) s[0]
+                  + ((ucs4_t) s[1] << 8)
+                  + ((ucs4_t) s[2] << 16)
+                  + ((ucs4_t) s[3] << 24);
       if (wc < 0x110000 && !(wc >= 0xd800 && wc < 0xe000))
         {
           *pwc = wc;