aboutsummaryrefslogtreecommitdiff
path: root/gcc/pretty-print.c
diff options
context:
space:
mode:
authorMartin Sebor <msebor@redhat.com>2017-08-17 16:50:06 +0000
committerMartin Sebor <msebor@gcc.gnu.org>2017-08-17 10:50:06 -0600
commit86ef85d3f7e790e2bbace497fe55ca93d99dad32 (patch)
tree0ac480a59d309d6f4974999384db866f4ad37bbe /gcc/pretty-print.c
parentf8c770ddb4b5499780a5e2d58c861a9c903cdd29 (diff)
downloadgcc-86ef85d3f7e790e2bbace497fe55ca93d99dad32.zip
gcc-86ef85d3f7e790e2bbace497fe55ca93d99dad32.tar.gz
gcc-86ef85d3f7e790e2bbace497fe55ca93d99dad32.tar.bz2
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
Diffstat (limited to 'gcc/pretty-print.c')
-rw-r--r--gcc/pretty-print.c19
1 files changed, 11 insertions, 8 deletions
diff --git a/gcc/pretty-print.c b/gcc/pretty-print.c
index 556462f..7340cd4 100644
--- a/gcc/pretty-print.c
+++ b/gcc/pretty-print.c
@@ -668,14 +668,10 @@ 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. */
- size_t len = strlen (s);
- if ((unsigned) n < len)
- len = n;
+ /* 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);
pp_append_text (pp, s, s + len);
}
@@ -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_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
manner. */
ASSERT_PP_FORMAT_2 ("normal colored normal 12345678",