public inbox for libc-hacker@sourceware.org
 help / color / mirror / Atom feed
From: Jakub Jelinek <jakub@redhat.com>
To: Ulrich Drepper <drepper@redhat.com>
Cc: Glibc hackers <libc-hacker@sources.redhat.com>
Subject: [PATCH] Fix _itoa (0LL, ...) for bases other than 8 and 16 [BZ #3902]
Date: Mon, 22 Jan 2007 16:06:00 -0000	[thread overview]
Message-ID: <20070122160948.GL3819@sunsite.mff.cuni.cz> (raw)

Hi!

While _itoa_word for 0L as first argument always adds 0 into the
buffer for all bases (all the loops are do ... while (value != 0);)
and so does the RUN_2N macro in _itoa as well (i.e. _itoa (0LL, bufend, 8,
0) or _itoa (0LL, bufend, 16, 0) both return "0"), _itoa (0LL, bufend, x, 0)
for x != 8 && x != 16 returns "".  stdio-common/vfprintf.c IMHO correctly
relies on the 0 being added, as shown on the following testcase.
Fixed by making sure 0 is added for value == 0 for all bases.

2007-01-22  Jakub Jelinek  <jakub@redhat.com>

	[BZ #3902]
	* stdio-common/_itoa.c (_itoa): If value is 0, always add 0 into
	the buffer.
	* stdio-common/tst-sprintf.c (main): Add test for %0lld with 0LL.

--- libc/stdio-common/_itoa.c.jj	2004-06-06 08:02:14.000000000 +0200
+++ libc/stdio-common/_itoa.c	2007-01-22 16:46:39.000000000 +0100
@@ -1,5 +1,5 @@
 /* Internal function for converting integers to ASCII.
-   Copyright (C) 1994, 1995, 1996, 1999, 2000, 2002, 2003, 2004
+   Copyright (C) 1994, 1995, 1996, 1999, 2000, 2002, 2003, 2004, 2007
    Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Contributed by Torbjorn Granlund <tege@matematik.su.se>
@@ -272,7 +272,7 @@ _itoa (value, buflim, base, upper_case)
 #if BITS_PER_MP_LIMB == 64
 	mp_limb_t base_multiplier = brec->base_multiplier;
 	if (brec->flag)
-	  while (value != 0)
+	  do
 	    {
 	      mp_limb_t quo, rem, x, dummy;
 
@@ -282,8 +282,9 @@ _itoa (value, buflim, base, upper_case)
 	      *--buflim = digits[rem];
 	      value = quo;
 	    }
+	  while (value != 0);
 	else
-	  while (value != 0)
+	  do
 	    {
 	      mp_limb_t quo, rem, x, dummy;
 
@@ -293,6 +294,7 @@ _itoa (value, buflim, base, upper_case)
 	      *--buflim = digits[rem];
 	      value = quo;
 	    }
+	  while (value != 0);
 #endif
 #if BITS_PER_MP_LIMB == 32
 	mp_limb_t t[3];
@@ -394,6 +396,11 @@ _itoa (value, buflim, base, upper_case)
 		n = 2;
 	      }
 	  }
+	else if (value == 0)
+	  {
+	    *--buflim = '0';
+	    return buflim;
+	  }
 	else
 	  {
 	    t[0] = value;
--- libc/stdio-common/tst-sprintf.c.jj	2003-06-25 13:04:49.000000000 +0200
+++ libc/stdio-common/tst-sprintf.c	2007-01-22 16:51:33.000000000 +0100
@@ -37,5 +37,13 @@ main (void)
       free (dst);
     }
 
+  if (sprintf (buf, "%0d %0ld %0lld", 0, 0L, 0LL) != 5
+      || strcmp (buf, "0 0 0") != 0)
+    {
+      printf ("sprintf (buf, \"%%0d %%0ld %%0lld\", 0, 0L, 0LL) produced \"%s\"\n",
+	      buf);
+      result = 1;
+    }
+  
   return result;
 }

	Jakub

                 reply	other threads:[~2007-01-22 16:06 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20070122160948.GL3819@sunsite.mff.cuni.cz \
    --to=jakub@redhat.com \
    --cc=drepper@redhat.com \
    --cc=libc-hacker@sources.redhat.com \
    /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).