public inbox for libc-hacker@sourceware.org
 help / color / mirror / Atom feed
* vfprintf with positional arg and 'h' flag
@ 2002-03-13  4:36 Andreas Schwab
  2002-03-13 15:21 ` Ulrich Drepper
  0 siblings, 1 reply; 3+ messages in thread
From: Andreas Schwab @ 2002-03-13  4:36 UTC (permalink / raw)
  To: libc-hacker

This fixes the use of the 'h' flag together with positional arguments,
giving wrong output on big-endian, and reading uninitialized memory on
little-endian.

Andreas.

2002-03-02  Andreas Schwab  <schwab@suse.de>

	* stdio-common/vfprintf.c (process_arg): Fix decimal format with
	'h' flag and positional arg.
        * tst-printf.c (rfg3): New function to test positional arguments.
        (main): Fix some warnings.

--- stdio-common/tst-printf.c.~1.14.~	2001-07-16 10:43:53.000000000 +0200
+++ stdio-common/tst-printf.c	2002-03-13 13:25:30.000000000 +0100
@@ -1,4 +1,5 @@
-/* Copyright (C) 1991,92,93,95,96,97,98,99, 2000 Free Software Foundation, Inc.
+/* Copyright (C) 1991,92,93,95,96,97,98,99, 2000, 2002
+     Free Software Foundation, Inc.
    This file is part of the GNU C Library.
 
    The GNU C Library is free software; you can redistribute it and/or
@@ -30,6 +31,7 @@
 
 static void rfg1 (void);
 static void rfg2 (void);
+static void rfg3 (void);
 
 
 static void
@@ -159,7 +161,7 @@
   printf("long octal negative:\t\"%lo\"\n", -2345L);
   printf("long unsigned decimal number:\t\"%lu\"\n", -123456L);
   printf("zero-padded LDN:\t\"%010ld\"\n", -123456L);
-  printf("left-adjusted ZLDN:\t\"%-010ld\"\n", -123456);
+  printf("left-adjusted ZLDN:\t\"%-010ld\"\n", -123456L);
   printf("space-padded LDN:\t\"%10ld\"\n", -123456L);
   printf("left-adjusted SLDN:\t\"%-10ld\"\n", -123456L);
 
@@ -218,8 +220,9 @@
     char buf[20];
     char buf2[512];
     printf ("snprintf (\"%%30s\", \"foo\") == %d, \"%.*s\"\n",
-	    snprintf (buf, sizeof (buf), "%30s", "foo"), sizeof (buf), buf);
-    printf ("snprintf (\"%%.999999u\", 10)\n",
+	    snprintf (buf, sizeof (buf), "%30s", "foo"), (int) sizeof (buf),
+	    buf);
+    printf ("snprintf (\"%%.999999u\", 10) == %d\n",
 	    snprintf(buf2, sizeof(buf2), "%.999999u", 10));
   }
 
@@ -269,6 +272,7 @@
   puts ("--- Should be no further output. ---");
   rfg1 ();
   rfg2 ();
+  rfg3 ();
 
   {
     char bytes[7];
@@ -352,3 +356,23 @@
   if (strcmp (buf, " 021") != 0)
     printf ("got: '%s', expected: '%s'\n", buf, " 021");
 }
+
+static void
+rfg3 (void)
+{
+  char buf[100];
+  double g = 5.0000001;
+  unsigned long l = 1234567890;
+  double d = 321.7654321;
+  char *s = "test-string";
+  int i = 12345;
+  int h = 1234;
+
+  sprintf (buf,
+	   "%1$*5$d %2$*6$hi %3$*7$lo %4$*8$f %9$*12$e %10$*13$g %11$*14$s",
+	   i, h, l, d, 8, 5, 14, 14, d, g, s, 14, 3, 14);
+  if (strcmp (buf,
+	      "   12345  1234    11145401322     321.765432   3.217654e+02   5    test-string") != 0)
+    printf ("got: '%s', expected: '%s'\n", buf,
+	    "   12345  1234    11145401322     321.765432   3.217654e+02   5    test-string");
+}
Index: stdio-common/vfprintf.c
===================================================================
RCS file: /cvs/glibc/libc/stdio-common/vfprintf.c,v
retrieving revision 1.106
diff -u -a -u -r1.106 vfprintf.c
--- stdio-common/vfprintf.c	2002/03/11 20:59:45	1.106
+++ stdio-common/vfprintf.c	2002/03/13 12:10:20
@@ -581,8 +581,10 @@
 	  else								      \
 	    if (is_long_num)						      \
 	      signed_number = args_value[fspec->data_arg].pa_long_int;	      \
-	    else							      \
+	    else if (!is_short)						      \
 	      signed_number = args_value[fspec->data_arg].pa_int;	      \
+	    else	      						      \
+	      signed_number = args_value[fspec->data_arg].pa_short_int;	      \
 									      \
 	  is_negative = signed_number < 0;				      \
 	  number.word = is_negative ? (- signed_number) : signed_number;      \

-- 
Andreas Schwab, SuSE Labs, schwab@suse.de
SuSE GmbH, Deutschherrnstr. 15-19, D-90429 Nürnberg
Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: vfprintf with positional arg and 'h' flag
  2002-03-13  4:36 vfprintf with positional arg and 'h' flag Andreas Schwab
@ 2002-03-13 15:21 ` Ulrich Drepper
  2002-03-14  2:15   ` Andreas Schwab
  0 siblings, 1 reply; 3+ messages in thread
From: Ulrich Drepper @ 2002-03-13 15:21 UTC (permalink / raw)
  To: Andreas Schwab; +Cc: libc-hacker

[-- Attachment #1: Type: text/plain, Size: 674 bytes --]

On Wed, 2002-03-13 at 04:36, Andreas Schwab wrote:
> This fixes the use of the 'h' flag together with positional arguments,
> giving wrong output on big-endian, and reading uninitialized memory on
> little-endian.

Thanks, I've applied the patch.  But reading uninitialized memory is no
bug.  Using it would be.  We are reading uninitialized memory in
numerous places.  Just to make this clear, there won't be any changes
because of this.

-- 
---------------.                          ,-.   1325 Chesapeake Terrace
Ulrich Drepper  \    ,-------------------'   \  Sunnyvale, CA 94089 USA
Red Hat          `--' drepper at redhat.com   `------------------------

[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 232 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: vfprintf with positional arg and 'h' flag
  2002-03-13 15:21 ` Ulrich Drepper
@ 2002-03-14  2:15   ` Andreas Schwab
  0 siblings, 0 replies; 3+ messages in thread
From: Andreas Schwab @ 2002-03-14  2:15 UTC (permalink / raw)
  To: Ulrich Drepper; +Cc: libc-hacker

Ulrich Drepper <drepper@redhat.com> writes:

|> On Wed, 2002-03-13 at 04:36, Andreas Schwab wrote:
|> > This fixes the use of the 'h' flag together with positional arguments,
|> > giving wrong output on big-endian, and reading uninitialized memory on
|> > little-endian.
|> 
|> Thanks, I've applied the patch.  But reading uninitialized memory is no
|> bug.  Using it would be.

It had used it.  You were able to get useless numbers even on
little-endian.

Andreas.

-- 
Andreas Schwab, SuSE Labs, schwab@suse.de
SuSE GmbH, Deutschherrnstr. 15-19, D-90429 Nürnberg
Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2002-03-14 10:15 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-03-13  4:36 vfprintf with positional arg and 'h' flag Andreas Schwab
2002-03-13 15:21 ` Ulrich Drepper
2002-03-14  2:15   ` Andreas Schwab

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