diff mbox

fix the handling of string precision in pretty printer (PR 81586)

Message ID 388ea759-1c59-9fad-5006-1f6f70c01378@gmail.com
State New
Headers show

Commit Message

Martin Sebor July 27, 2017, 11:29 p.m. UTC
The pretty printer treats precision in %s directives as a request
to print exactly as many characters from the string argument when
what precision normally (in C) means is the maximum number of
characters to read from the string.  It doesn't mean to read
past the terminating NUL.

The attached patch fixes that.  Tested on x86_64-linux.

Martin

Comments

Jeff Law Aug. 2, 2017, 7:03 p.m. UTC | #1
On 07/27/2017 05:29 PM, Martin Sebor wrote:
> The pretty printer treats precision in %s directives as a request
> to print exactly as many characters from the string argument when
> what precision normally (in C) means is the maximum number of
> characters to read from the string.  It doesn't mean to read
> past the terminating NUL.
> 
> The attached patch fixes that.  Tested on x86_64-linux.
> 
> Martin
> 
> gcc-81586.diff
> 
> 
> PR c++/81586 - valgrind error in output_buffer_append_r with -Wall
> 
> gcc/ChangeLog:
> 
> 	PR c++/81586
> 	* pretty-print.c (pp_format): Correct the handling of %s precision.
OK.
jeff
diff mbox

Patch

PR c++/81586 - valgrind error in output_buffer_append_r with -Wall

gcc/ChangeLog:

	PR c++/81586
	* pretty-print.c (pp_format): Correct the handling of %s precision.

diff --git a/gcc/pretty-print.c b/gcc/pretty-print.c
index 570dec7..a79191b 100644
--- a/gcc/pretty-print.c
+++ b/gcc/pretty-print.c
@@ -667,7 +667,17 @@  pp_format (pretty_printer *pp, text_info *text)
 	      }
 
 	    s = va_arg (*text->args_ptr, const char *);
-	    pp_append_text (pp, s, s + n);
+
+	    /* Negative precision is treated as if it were omitted.  */
+	    if (n < 0)
+	      n = INT_MAX;
+
+	    /* Append the lesser of precision and strlen (s) characters.  */
+	    size_t len = strlen (s);
+	    if ((unsigned) n < len)
+	      len = n;
+
+	    pp_append_text (pp, s, s + len);
 	  }
 	  break;