public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Roy Eldar <royeldar0@gmail.com>
To: fweimer@redhat.com
Cc: Roy Eldar <royeldar0@gmail.com>,
	libc-alpha@sourceware.org, carlos@redhat.com
Subject: [PATCH v2] elf: fix handling of negative numbers in dl-printf
Date: Thu, 25 May 2023 17:41:58 +0300	[thread overview]
Message-ID: <20230525144159.2935-1-royeldar0@gmail.com> (raw)
In-Reply-To: <87fs7kq2ey.fsf@oldenburg.str.redhat.com>

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

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
 		  }
 
-- 
2.30.2


  reply	other threads:[~2023-05-25 14:42 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-03 21:16 [PATCH] " Roy Eldar
2023-05-08 13:42 ` Florian Weimer
2023-05-08 18:08   ` Roy E
2023-05-24 17:41     ` Roy E
2023-05-15 20:29   ` [PATCH] elf: add test for dl-printf Roy Eldar
2023-05-25  7:44     ` Florian Weimer
2023-05-25 14:41       ` [PATCH v2] " Roy Eldar
2023-05-25 17:10         ` Florian Weimer
2023-05-25  7:46 ` [PATCH] elf: fix handling of negative numbers in dl-printf Florian Weimer
2023-05-25 14:41   ` Roy Eldar [this message]
2023-05-25 17:10     ` [PATCH v2] " Florian Weimer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230525144159.2935-1-royeldar0@gmail.com \
    --to=royeldar0@gmail.com \
    --cc=carlos@redhat.com \
    --cc=fweimer@redhat.com \
    --cc=libc-alpha@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).