public inbox for libc-hacker@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] Fix _itoa (0LL, ...) for bases other than 8 and 16 [BZ #3902]
@ 2007-01-22 16:06 Jakub Jelinek
  0 siblings, 0 replies; only message in thread
From: Jakub Jelinek @ 2007-01-22 16:06 UTC (permalink / raw)
  To: Ulrich Drepper; +Cc: Glibc hackers

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

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2007-01-22 16:06 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-01-22 16:06 [PATCH] Fix _itoa (0LL, ...) for bases other than 8 and 16 [BZ #3902] Jakub Jelinek

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).