This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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


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
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;
 

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]