Commit 86ef85d3 by Martin Sebor Committed by Martin Sebor

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.

From-SVN: r251157
parent f8c770dd
2017-08-17 Martin Sebor <msebor@redhat.com>
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.
2017-08-17 Richard Sandiford <richard.sandiford@linaro.org> 2017-08-17 Richard Sandiford <richard.sandiford@linaro.org>
* internal-fn.def (CLRSB, CLZ, CTZ, FFS, PARITY, POPCOUNT): Add * internal-fn.def (CLRSB, CLZ, CTZ, FFS, PARITY, POPCOUNT): Add
......
...@@ -668,14 +668,10 @@ pp_format (pretty_printer *pp, text_info *text) ...@@ -668,14 +668,10 @@ pp_format (pretty_printer *pp, text_info *text)
s = va_arg (*text->args_ptr, const char *); s = va_arg (*text->args_ptr, const char *);
/* Negative precision is treated as if it were omitted. */ /* Append the lesser of precision and strlen (s) characters
if (n < 0) from the array (which need not be a nul-terminated string).
n = INT_MAX; 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); pp_append_text (pp, s, s + len);
} }
...@@ -1438,6 +1434,13 @@ test_pp_format () ...@@ -1438,6 +1434,13 @@ test_pp_format ()
ASSERT_PP_FORMAT_2 ("A 12345678", "%c %x", 'A', 0x12345678); ASSERT_PP_FORMAT_2 ("A 12345678", "%c %x", 'A', 0x12345678);
ASSERT_PP_FORMAT_2 ("hello world 12345678", "%s %x", "hello world", ASSERT_PP_FORMAT_2 ("hello world 12345678", "%s %x", "hello world",
0x12345678); 0x12345678);
/* Not nul-terminated. */
char arr[5] = { '1', '2', '3', '4', '5' };
ASSERT_PP_FORMAT_3 ("123 12345678", "%.*s %x", 3, arr, 0x12345678);
ASSERT_PP_FORMAT_3 ("1234 12345678", "%.*s %x", -1, "1234", 0x12345678);
ASSERT_PP_FORMAT_3 ("12345 12345678", "%.*s %x", 7, "12345", 0x12345678);
/* We can't test for %p; the pointer is printed in an implementation-defined /* We can't test for %p; the pointer is printed in an implementation-defined
manner. */ manner. */
ASSERT_PP_FORMAT_2 ("normal colored normal 12345678", ASSERT_PP_FORMAT_2 ("normal colored normal 12345678",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment