Correctly handle %p in wprintf
diff mbox

Message ID 877g65wq0l.fsf@igel.home
State New
Headers show

Commit Message

Andreas Schwab May 1, 2014, 2:08 p.m. UTC
[BZ #16890]
	* stdio-common/vfprintf.c (process_arg) [%p]: Mark string as wide
	when compiling wprintf.
	* stdio-common/tstdiomisc.c (t3): New function.
	(main): Call it.
---
 stdio-common/tstdiomisc.c | 19 +++++++++++++++++++
 stdio-common/vfprintf.c   |  3 ++-
 2 files changed, 21 insertions(+), 1 deletion(-)

Comments

Ondřej Bílka May 1, 2014, 5:37 p.m. UTC | #1
Looks ok however

On Thu, May 01, 2014 at 04:08:10PM +0200, Andreas Schwab wrote:
> diff --git a/stdio-common/vfprintf.c b/stdio-common/vfprintf.c
> index f7e5f61..c4ff833 100644
> --- a/stdio-common/vfprintf.c
> +++ b/stdio-common/vfprintf.c
> @@ -936,7 +936,8 @@ vfprintf (FILE *s, const CHAR_T *format, va_list ap)
>  	    /* Make sure the full string "(nil)" is printed.  */	      \
>  	    if (prec < 5)						      \
>  	      prec = 5;							      \
> -	    is_long = 0;	/* This is no wide-char string.  */	      \
> +	    /* This is a wide string iff compiling wprintf.  */		      \
> +	    is_long = sizeof (CHAR_T) > 1;				      \
>  	    goto LABEL (print_string);					      \
>  	  }								      \

Should we do same thing here or not?

    LABEL (form_strerror):					    \
      /* Print description of error ERRNO.  */			    \
      string =							    \
        (CHAR_T *) __strerror_r (save_errno, (char *) work_buffer,  \
                                 sizeof work_buffer);		    \
      is_long = 0;              /* This is no wide-char string.  */ \
      goto LABEL (print_string)
Andreas Schwab May 1, 2014, 5:54 p.m. UTC | #2
Ondřej Bílka <neleai@seznam.cz> writes:

> Should we do same thing here or not?

No, there is no bug here.

Andreas.
Adhemerval Zanella May 1, 2014, 6:02 p.m. UTC | #3
On 01-05-2014 11:08, Andreas Schwab wrote:
> 	[BZ #16890]
> 	* stdio-common/vfprintf.c (process_arg) [%p]: Mark string as wide
> 	when compiling wprintf.
> 	* stdio-common/tstdiomisc.c (t3): New function.
> 	(main): Call it.
> ---
>  stdio-common/tstdiomisc.c | 19 +++++++++++++++++++
>  stdio-common/vfprintf.c   |  3 ++-
>  2 files changed, 21 insertions(+), 1 deletion(-)
>
> diff --git a/stdio-common/tstdiomisc.c b/stdio-common/tstdiomisc.c
> index 5a25ecc..2e0663a 100644
> --- a/stdio-common/tstdiomisc.c
> +++ b/stdio-common/tstdiomisc.c
> @@ -46,6 +46,24 @@ t2 (void)
>    return result;
>  }
>
> +static int
> +t3 (void)
> +{
> +  char buf[80];
> +  wchar_t wbuf[80];
> +  int result = 0;
> +  int retval;
> +
> +  retval = sprintf (buf, "%p", (char *) NULL);
> +  result |= retval != 5 || strcmp (buf, "(nil)") != 0;
> +
> +  retval = swprintf (wbuf, sizeof (wbuf) / sizeof (wbuf[0]),
> +		     L"%p", (char *) NULL);
> +  result |= retval != 5 || wcscmp (wbuf, L"(nil)") != 0;
> +
> +  return result;
> +}
> +
>  volatile double qnanval;
>  volatile long double lqnanval;
>  /* A sNaN is only guaranteed to be representable in variables with static (or
> @@ -243,6 +261,7 @@ main (int argc, char *argv[])
>
>    result |= t1 ();
>    result |= t2 ();
> +  result |= t3 ();
>    result |= F ();
>
>    result |= fflush (stdout) == EOF;
> diff --git a/stdio-common/vfprintf.c b/stdio-common/vfprintf.c
> index f7e5f61..c4ff833 100644
> --- a/stdio-common/vfprintf.c
> +++ b/stdio-common/vfprintf.c
> @@ -936,7 +936,8 @@ vfprintf (FILE *s, const CHAR_T *format, va_list ap)
>  	    /* Make sure the full string "(nil)" is printed.  */	      \
>  	    if (prec < 5)						      \
>  	      prec = 5;							      \
> -	    is_long = 0;	/* This is no wide-char string.  */	      \
> +	    /* This is a wide string iff compiling wprintf.  */		      \
> +	    is_long = sizeof (CHAR_T) > 1;				      \
>  	    goto LABEL (print_string);					      \
>  	  }								      \
>        }									      \

s/iff/if , besides that LGTM.
Roland McGrath May 1, 2014, 6:04 p.m. UTC | #4
> s/iff/if , besides that LGTM.

"iff" is standard shorthand for "if and only if" and seems appropriate
enough in this case.
Adhemerval Zanella May 1, 2014, 6:05 p.m. UTC | #5
On 01-05-2014 15:04, Roland McGrath wrote:
>> s/iff/if , besides that LGTM.
> "iff" is standard shorthand for "if and only if" and seems appropriate
> enough in this case.
>
Fair enough then.

Patch
diff mbox

diff --git a/stdio-common/tstdiomisc.c b/stdio-common/tstdiomisc.c
index 5a25ecc..2e0663a 100644
--- a/stdio-common/tstdiomisc.c
+++ b/stdio-common/tstdiomisc.c
@@ -46,6 +46,24 @@  t2 (void)
   return result;
 }
 
+static int
+t3 (void)
+{
+  char buf[80];
+  wchar_t wbuf[80];
+  int result = 0;
+  int retval;
+
+  retval = sprintf (buf, "%p", (char *) NULL);
+  result |= retval != 5 || strcmp (buf, "(nil)") != 0;
+
+  retval = swprintf (wbuf, sizeof (wbuf) / sizeof (wbuf[0]),
+		     L"%p", (char *) NULL);
+  result |= retval != 5 || wcscmp (wbuf, L"(nil)") != 0;
+
+  return result;
+}
+
 volatile double qnanval;
 volatile long double lqnanval;
 /* A sNaN is only guaranteed to be representable in variables with static (or
@@ -243,6 +261,7 @@  main (int argc, char *argv[])
 
   result |= t1 ();
   result |= t2 ();
+  result |= t3 ();
   result |= F ();
 
   result |= fflush (stdout) == EOF;
diff --git a/stdio-common/vfprintf.c b/stdio-common/vfprintf.c
index f7e5f61..c4ff833 100644
--- a/stdio-common/vfprintf.c
+++ b/stdio-common/vfprintf.c
@@ -936,7 +936,8 @@  vfprintf (FILE *s, const CHAR_T *format, va_list ap)
 	    /* Make sure the full string "(nil)" is printed.  */	      \
 	    if (prec < 5)						      \
 	      prec = 5;							      \
-	    is_long = 0;	/* This is no wide-char string.  */	      \
+	    /* This is a wide string iff compiling wprintf.  */		      \
+	    is_long = sizeof (CHAR_T) > 1;				      \
 	    goto LABEL (print_string);					      \
 	  }								      \
       }									      \