[v2] elf: fix handling of negative numbers in dl-printf

Message ID 20230525144159.2935-1-royeldar0@gmail.com
State Committed
Commit dae801527386f94e9d2fabf23c37863d1b599153
Headers
Series [v2] elf: fix handling of negative numbers in dl-printf |

Checks

Context Check Description
redhat-pt-bot/TryBot-apply_patch success Patch applied to master at the time it was sent
redhat-pt-bot/TryBot-32bit success Build for i686
linaro-tcwg-bot/tcwg_glibc_build--master-aarch64 success Testing passed
linaro-tcwg-bot/tcwg_glibc_build--master-arm success Testing passed
linaro-tcwg-bot/tcwg_glibc_check--master-arm success Testing passed
linaro-tcwg-bot/tcwg_glibc_check--master-aarch64 success Testing passed

Commit Message

Roy Eldar May 25, 2023, 2:41 p.m. UTC
  _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>
---
 elf/dl-printf.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
  

Comments

Florian Weimer May 25, 2023, 5:10 p.m. UTC | #1
* Roy Eldar:

> _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>

And pushed.

Thanks,
Florian
  

Patch

diff --git a/elf/dl-printf.c b/elf/dl-printf.c
index e8b9900370..6efb4c019a 100644
--- a/elf/dl-printf.c
+++ b/elf/dl-printf.c
@@ -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
 		  }