elf: fix handling of negative numbers in dl-printf
authorRoy Eldar <royeldar0@gmail.com>
Thu, 25 May 2023 14:41:58 +0000 (17:41 +0300)
committerFlorian Weimer <fweimer@redhat.com>
Thu, 25 May 2023 16:50:59 +0000 (18:50 +0200)
_dl_debug_vdprintf is a bare-bones printf implementation; currently
printing a signed integer (using "%d" format specifier) behaves
incorrectly when the number is negative, as it just prints the
corresponding unsigned integer, preceeded by a minus sign.

For example, _dl_printf("%d", -1) would print '-4294967295'.

Signed-off-by: Roy Eldar <royeldar0@gmail.com>
Reviewed-by: Florian Weimer <fweimer@redhat.com>
elf/dl-printf.c

index e8b9900370b1b8f88ac986bc41c2f867c8ef91b0..6efb4c019a792817e986a0fafa1d5d8dfca03825 100644 (file)
@@ -1,5 +1,6 @@
 /* printf implementation for the dynamic loader.
    Copyright (C) 1997-2023 Free Software Foundation, Inc.
+   Copyright The GNU Toolchain Authors.
    This file is part of the GNU C Library.
 
    The GNU C Library is free software; you can redistribute it and/or
@@ -150,19 +151,25 @@ _dl_debug_vdprintf (int fd, int tag_p, const char *fmt, va_list arg)
                    if (long_mod)
                      {
                        if ((long int) num < 0)
-                         negative = true;
+                         {
+                           num = -num;
+                           negative = true;
+                         }
                      }
                    else
                      {
                        if ((int) num < 0)
                          {
-                           num = (unsigned int) num;
+                           num = -(unsigned int) num;
                            negative = true;
                          }
                      }
 #else
                    if ((int) num < 0)
-                     negative = true;
+                     {
+                       num = -num;
+                       negative = true;
+                     }
 #endif
                  }