Skip to content
Tcl Source Code View Ticket
Login

View Ticket

Avoid signed integer overflow in Utf32ToUtfProc()

Closed / Fixed203792a48c2fbb32f822d8b0a66f7d8fac3e7981Created 4 years agoUpdated 4 years ago
Descriptionwiki2023-04-22 08:05:33
Encountered during cmdAH.test and encoding.test for Tcl built with -fsanitize=shift-base:
generic/tclEncoding.c:2779:27: runtime error: left shift of 255 by 24 places cannot be represented in type 'int' 

Avoid by casting to unsigned before shifting:

--- generic/tclEncoding.c.orig
+++ generic/tclEncoding.c
@@ -2774,9 +2774,9 @@ Utf32ToUtfProc(
 
        int prev = ch;
        if (flags & TCL_ENCODING_LE) {
-           ch = (src[3] & 0xFF) << 24 | (src[2] & 0xFF) << 16 | (src[1] & 0xFF) << 8 | (src[0] & 0xFF);
+           ch = (unsigned int)(src[3] & 0xFF) << 24 | (src[2] & 0xFF) << 16 | (src[1] & 0xFF) << 8 | (src[0] & 0xFF);
        } else {
-           ch = (src[0] & 0xFF) << 24 | (src[1] & 0xFF) << 16 | (src[2] & 0xFF) << 8 | (src[3] & 0xFF);
+           ch = (unsigned int)(src[0] & 0xFF) << 24 | (src[1] & 0xFF) << 16 | (src[2] & 0xFF) << 8 | (src[3] & 0xFF);
        }
        if (HIGH_SURROGATE(prev) && !LOW_SURROGATE(ch)) {
            /* Bug [10c2c17c32]. If Hi surrogate not followed by Lo surrogate, finish 3-byte UTF-8 */
Referenced by2
jan.nijtmanswiki2023-04-22 18:10:27

Thanks for the fix!. Closing

Log in (anonymous is fine) to add a comment.