]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
stdio: Fix UB on snprintf
authorAdhemerval Zanella <adhemerval.zanella@linaro.org>
Fri, 18 Apr 2025 13:03:56 +0000 (10:03 -0300)
committerAdhemerval Zanella <adhemerval.zanella@linaro.org>
Wed, 7 May 2025 17:21:21 +0000 (14:21 -0300)
The elf/tst-dl-printf-static test when built with ubsan triggers:

UBSAN: Undefined behaviour in vfprintf-process-arg.c:58:36 negation of 9223372036854775808 cannot be represented in type 'long int'

stdio-common/vfprintf-process-arg.c

index 90b5e61ceb8fc689edf784ab349e28048f4d8ae4..a41feb2c472a53b5795444c33a6b77dda584c485 100644 (file)
@@ -55,7 +55,8 @@ LABEL (form_integer):
         signed_number = (short int) process_arg_unsigned_int ();
 
       is_negative = signed_number < 0;
-      number.word = is_negative ? (- signed_number) : signed_number;
+      number.word = is_negative ? (- (unsigned long) signed_number)
+                               : signed_number;
 
       goto LABEL (number);
     }