public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] use strnlen in pretty printer for "%.*s" (PR 81859)
@ 2017-08-16 17:20 Martin Sebor
  2017-08-16 22:38 ` David Malcolm
  0 siblings, 1 reply; 3+ messages in thread
From: Martin Sebor @ 2017-08-16 17:20 UTC (permalink / raw)
  To: Gcc Patch List

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

Bug 81859 points out that my fix for bug 81586 wasn't quite
right (or complete):  the argument of a %.*s directive need
not be a nul-terminated string when the precision is less
than the initialized size of the array the argument points
to.  The attached tweak uses strnlen to avoid reading past
the end of a non-nul terminated array.

The patch has been tested on x86_64-linux and by running
self tests under Valgrind.  I'll go ahead and commit it as
obvious sometime later today if there are no objections in
the meantime.

Martin


[-- Attachment #2: gcc-81859.diff --]
[-- Type: text/x-patch, Size: 1685 bytes --]

PR c/81859 - [8 Regression] valgrind error from warn_about_normalization

gcc/ChangeLog:

	PR c/81859
	* pretty-print.c (pp_format): Use strnlen in %.*s to avoid reading
	past the end of an array.
	(test_pp_format): Add test cases.

Index: gcc/pretty-print.c
===================================================================
--- gcc/pretty-print.c	(revision 251100)
+++ gcc/pretty-print.c	(working copy)
@@ -668,15 +668,11 @@ pp_format (pretty_printer *pp, text_info *text)
 
 	    s = va_arg (*text->args_ptr, const char *);
 
-	    /* Negative precision is treated as if it were omitted.  */
-	    if (n < 0)
-	      n = INT_MAX;
+	    /* Append the lesser of precision and strlen (s) characters
+	       from the array (which need not be a nul-terminated string).
+	       Negative precision is treated as if it were omitted.  */
+	    size_t len = n < 0 ? strlen (s) : strnlen (s, n);
 
-	    /* 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;
@@ -1438,6 +1434,13 @@ test_pp_format ()
   ASSERT_PP_FORMAT_2 ("A 12345678", "%c %x", 'A', 0x12345678);
   ASSERT_PP_FORMAT_2 ("hello world 12345678", "%s %x", "hello world",
 		      0x12345678);
+
+  /* Not nul-terminated.  */
+  char arr[5] = { '1', '2', '3', '4', '5' };
+  ASSERT_PP_FORMAT_2 ("123", "%.*s", 3, arr);
+  ASSERT_PP_FORMAT_2 ("1234", "%.*s", -1, "1234");
+  ASSERT_PP_FORMAT_2 ("12345", "%.*s", 7, "12345");
+
   /* We can't test for %p; the pointer is printed in an implementation-defined
      manner.  */
   ASSERT_PP_FORMAT_2 ("normal colored normal 12345678",

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

* Re: [PATCH] use strnlen in pretty printer for "%.*s" (PR 81859)
  2017-08-16 17:20 [PATCH] use strnlen in pretty printer for "%.*s" (PR 81859) Martin Sebor
@ 2017-08-16 22:38 ` David Malcolm
  2017-08-17 19:01   ` Martin Sebor
  0 siblings, 1 reply; 3+ messages in thread
From: David Malcolm @ 2017-08-16 22:38 UTC (permalink / raw)
  To: Martin Sebor, Gcc Patch List

On Wed, 2017-08-16 at 10:12 -0600, Martin Sebor wrote:
> PR c/81859 - [8 Regression] valgrind error from
> warn_about_normalization
> 
> gcc/ChangeLog:
> 
> 	PR c/81859
> 	* pretty-print.c (pp_format): Use strnlen in %.*s to avoid
> reading
> 	past the end of an array.
> 	(test_pp_format): Add test cases.
> 
> Index: gcc/pretty-print.c
> ===================================================================
> --- gcc/pretty-print.c	(revision 251100)
> +++ gcc/pretty-print.c	(working copy)
> @@ -668,15 +668,11 @@ pp_format (pretty_printer *pp, text_info *text)
>  
>  	    s = va_arg (*text->args_ptr, const char *);
>  
> -	    /* Negative precision is treated as if it were
> omitted.  */
> -	    if (n < 0)
> -	      n = INT_MAX;
> +	    /* Append the lesser of precision and strlen (s)
> characters
> +	       from the array (which need not be a nul-terminated
> string).
> +	       Negative precision is treated as if it were
> omitted.  */
> +	    size_t len = n < 0 ? strlen (s) : strnlen (s, n);
>  
> -	    /* 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;
> @@ -1438,6 +1434,13 @@ test_pp_format ()
>    ASSERT_PP_FORMAT_2 ("A 12345678", "%c %x", 'A', 0x12345678);
>    ASSERT_PP_FORMAT_2 ("hello world 12345678", "%s %x", "hello
> world",
>  		      0x12345678);
> +
> +  /* Not nul-terminated.  */
> +  char arr[5] = { '1', '2', '3', '4', '5' };
> +  ASSERT_PP_FORMAT_2 ("123", "%.*s", 3, arr);
> +  ASSERT_PP_FORMAT_2 ("1234", "%.*s", -1, "1234");
> +  ASSERT_PP_FORMAT_2 ("12345", "%.*s", 7, "12345");
> +

The other examples in this selftest append a trailing argument with a
known bit pattern (0x12345678), to ensure that we're consuming
arguments correctly.

Please can you do the same for these tests.

Thanks
Dave

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

* Re: [PATCH] use strnlen in pretty printer for "%.*s" (PR 81859)
  2017-08-16 22:38 ` David Malcolm
@ 2017-08-17 19:01   ` Martin Sebor
  0 siblings, 0 replies; 3+ messages in thread
From: Martin Sebor @ 2017-08-17 19:01 UTC (permalink / raw)
  To: David Malcolm, Gcc Patch List

On 08/16/2017 02:55 PM, David Malcolm wrote:
> On Wed, 2017-08-16 at 10:12 -0600, Martin Sebor wrote:
>> PR c/81859 - [8 Regression] valgrind error from
>> warn_about_normalization
>>
>> gcc/ChangeLog:
>>
>> 	PR c/81859
>> 	* pretty-print.c (pp_format): Use strnlen in %.*s to avoid
>> reading
>> 	past the end of an array.
>> 	(test_pp_format): Add test cases.
>>
>> Index: gcc/pretty-print.c
>> ===================================================================
>> --- gcc/pretty-print.c	(revision 251100)
>> +++ gcc/pretty-print.c	(working copy)
>> @@ -668,15 +668,11 @@ pp_format (pretty_printer *pp, text_info *text)
>>
>>  	    s = va_arg (*text->args_ptr, const char *);
>>
>> -	    /* Negative precision is treated as if it were
>> omitted.  */
>> -	    if (n < 0)
>> -	      n = INT_MAX;
>> +	    /* Append the lesser of precision and strlen (s)
>> characters
>> +	       from the array (which need not be a nul-terminated
>> string).
>> +	       Negative precision is treated as if it were
>> omitted.  */
>> +	    size_t len = n < 0 ? strlen (s) : strnlen (s, n);
>>
>> -	    /* 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;
>> @@ -1438,6 +1434,13 @@ test_pp_format ()
>>    ASSERT_PP_FORMAT_2 ("A 12345678", "%c %x", 'A', 0x12345678);
>>    ASSERT_PP_FORMAT_2 ("hello world 12345678", "%s %x", "hello
>> world",
>>  		      0x12345678);
>> +
>> +  /* Not nul-terminated.  */
>> +  char arr[5] = { '1', '2', '3', '4', '5' };
>> +  ASSERT_PP_FORMAT_2 ("123", "%.*s", 3, arr);
>> +  ASSERT_PP_FORMAT_2 ("1234", "%.*s", -1, "1234");
>> +  ASSERT_PP_FORMAT_2 ("12345", "%.*s", 7, "12345");
>> +
>
> The other examples in this selftest append a trailing argument with a
> known bit pattern (0x12345678), to ensure that we're consuming
> arguments correctly.
>
> Please can you do the same for these tests.

Sure.  I committed the updated fix in r251157.

Martin

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

end of thread, other threads:[~2017-08-17 16:58 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-08-16 17:20 [PATCH] use strnlen in pretty printer for "%.*s" (PR 81859) Martin Sebor
2017-08-16 22:38 ` David Malcolm
2017-08-17 19:01   ` Martin Sebor

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