[glibc/azanella/ubsan-undef] stdio: Fix UB on snprintf

Adhemerval Zanella azanella@sourceware.org
Fri Apr 25 19:51:45 GMT 2025


https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=7274dd723816e8e9735641e7e02a8efaaaa9749c

commit 7274dd723816e8e9735641e7e02a8efaaaa9749c
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Date:   Fri Apr 18 10:03:56 2025 -0300

    stdio: Fix UB on snprintf
    
    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'

Diff:
---
 stdio-common/vfprintf-process-arg.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/stdio-common/vfprintf-process-arg.c b/stdio-common/vfprintf-process-arg.c
index 90b5e61ceb..a41feb2c47 100644
--- a/stdio-common/vfprintf-process-arg.c
+++ b/stdio-common/vfprintf-process-arg.c
@@ -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);
     }


More information about the Glibc-cvs mailing list