bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

mbrtoc16: Fix undefined behaviour


From: Bruno Haible
Subject: mbrtoc16: Fix undefined behaviour
Date: Tue, 10 Sep 2024 13:56:30 +0200

Compiling a gnulib testdir with clang's UBSAN, I see two more errors:
in test-mbrtoc16-3.sh.log and test-mbrtoc16-5.sh.log:

../../gllib/mbrtoc16.c:205:11: runtime error: left shift of 56843 by 16 places 
cannot be represented in type 'int'
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior 
../../gllib/mbrtoc16.c:205:11 
PASS test-mbrtoc16-3.sh (exit status: 0)

This patch fixes it.


2024-09-10  Bruno Haible  <bruno@clisp.org>

        mbrtoc16: Fix undefined behaviour.
        * lib/mbrtoc16.c (SET_EXTRA_STATE): Cast to 'unsigned int' before
        shifting the bits to the positions 31..16.

diff --git a/lib/mbrtoc16.c b/lib/mbrtoc16.c
index 1fd0fbf242..96a33fa157 100644
--- a/lib/mbrtoc16.c
+++ b/lib/mbrtoc16.c
@@ -45,7 +45,7 @@ static_assert (sizeof (mbstate_t) >= 4);
 /* mbstate_t is defined in <bits/types/__mbstate_t.h>.
    For more details, see glibc/iconv/skeleton.c.  */
 # define SET_EXTRA_STATE(ps, c16) \
-    ((ps)->__count |= (c16 << 16))
+    ((ps)->__count |= ((unsigned int) (c16) << 16))
 # define GET_EXTRA_STATE(ps) \
     (((unsigned int) (ps)->__count) >> 16)
 # define RESET_EXTRA_STATE(ps) \






reply via email to

[Prev in Thread] Current Thread [Next in Thread]