From 67b9fab0e6f9fac918523df18ae2d8bbb88d7748 Mon Sep 17 00:00:00 2001 From: Martin Sebor Date: Mon, 26 Nov 2018 16:55:36 -0700 Subject: [PATCH] PR 87756 - missing unterminated argument warning using address of a constant character gcc/ChangeLog: PR 87756 * expr.c (string_constant): Handle top-level decls of all character types and subobjects of narrow character type. gcc/testsuite/ChangeLog: PR 87756 * gcc.dg/warn-sprintf-no-nul-2.c: Move incomplete tests from here... * gcc.dg/warn-sprintf-no-nul-3.c: ...to here and complete them. From-SVN: r266494 --- gcc/ChangeLog | 6 ++ gcc/expr.c | 10 ++- gcc/testsuite/ChangeLog | 10 ++- gcc/testsuite/gcc.dg/warn-sprintf-no-nul-2.c | 61 ----------------- gcc/testsuite/gcc.dg/warn-sprintf-no-nul-3.c | 71 ++++++++++++++++++++ 5 files changed, 93 insertions(+), 65 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/warn-sprintf-no-nul-3.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 19a802c6c72..828dc23c21b 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,9 @@ +2018-11-26 Martin Sebor + + PR 87756 + * expr.c (string_constant): Handle top-level decls of all character + types and subobjects of narrow character type. + 2018-11-27 Alan Modra * config.gcc (powerpc*-*-linux*): Add linux.h to tm_file. diff --git a/gcc/expr.c b/gcc/expr.c index 021a04c5944..ef29ec5c40a 100644 --- a/gcc/expr.c +++ b/gcc/expr.c @@ -11497,10 +11497,16 @@ string_constant (tree arg, tree *ptr_offset, tree *mem_size, tree *decl) if (decl) *decl = array; - if (TREE_CODE (init) == INTEGER_CST) + if (TREE_CODE (init) == INTEGER_CST + && (TREE_CODE (TREE_TYPE (array)) == INTEGER_TYPE + || TYPE_MAIN_VARIANT (eltype) == char_type_node)) { /* For a reference to (address of) a single constant character, - store the native representation of the character in CHARBUF. */ + store the native representation of the character in CHARBUF. + If the reference is to an element of an array or a member + of a struct, only consider narrow characters until ctors + for wide character arrays are transformed to STRING_CSTs + like those for narrow arrays. */ unsigned char charbuf[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT]; int len = native_encode_expr (init, charbuf, sizeof charbuf, 0); if (len > 0) diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 989b548276e..34790af2167 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,9 @@ +2018-11-26 Martin Sebor + + PR 87756 + * gcc.dg/warn-sprintf-no-nul-2.c: Move incomplete tests from here... + * gcc.dg/warn-sprintf-no-nul-3.c: ...to here and complete them. + 2018-11-26 Marek Polacek PR c++/88120 - ICE when calling save_expr in a template. @@ -26,7 +32,7 @@ 2018-11-26 Jozef Lawrynowicz - * lib/target-supports.exp (check_effective_target_newlib_nano_io): New. + * lib/target-supports.exp (check_effective_target_newlib_nano_io): New. * gcc.c-torture/execute/920501-8.c: Register undefined linker symbol _printf_float for newlib_nano_io target. * gcc.c-torture/execute/930513-1.c: Likewise. @@ -70,7 +76,7 @@ * gcc.dg/tree-ssa/loop-interchange-1.c: Likewise. * gcc.dg/tree-ssa/loop-interchange-1b.c: Likewise. * lib/target-supports.exp (check_effective_target_size20plus): New. - (check_effective_target_size32plus): Update comment. + (check_effective_target_size32plus): Update comment. 2018-11-26 Uros Bizjak diff --git a/gcc/testsuite/gcc.dg/warn-sprintf-no-nul-2.c b/gcc/testsuite/gcc.dg/warn-sprintf-no-nul-2.c index d3d776c03a8..3372a8b99ec 100644 --- a/gcc/testsuite/gcc.dg/warn-sprintf-no-nul-2.c +++ b/gcc/testsuite/gcc.dg/warn-sprintf-no-nul-2.c @@ -3,9 +3,6 @@ { dg-do compile } { dg-options "-O2 -Wall -ftrack-macro-expansion=0" } */ -typedef __SIZE_TYPE__ size_t; -typedef __WCHAR_TYPE__ wchar_t; - int sprintf (char*, const char*, ...); extern char* dest (void); @@ -71,61 +68,3 @@ void test_sprintf_s (void) T (sprintf (D, "%s", &str3[3])); T (sprintf (D, "%s", &str3[4])); /* { dg-warning "\\\[-Warray-bounds" } */ } - - -const char wnul = '\0'; -const char wnonul = 'a'; -const char wcs3[] = "123"; - -const struct -{ - char a, b, s[3]; -} w1 = { '\0', 'b', "123" }, - w2[2] = { - { '\0', 'c', "12" }, - { 'd', '\0', "123" } - }; - -void test_sprintf_ls (void) -{ - T (sprintf (D, "%s", &wnul)); - T (sprintf (D, "%s", &wnonul)); /* { dg-warning "nul-terminated" } */ - T (sprintf (D, "%.1s", &wnonul)); - T (sprintf (D, "%.2s", &wnonul)); /* { dg-warning "nul-terminated" } */ - - T (sprintf (D, "%s", &w1.a)); - T (sprintf (D, "%s", &w1.b)); /* { dg-warning "nul-terminated" } */ - T (sprintf (D, "%.1s", &w1.b)); - T (sprintf (D, "%.2s", &w1.b)); /* { dg-warning "nul-terminated" } */ - T (sprintf (D, "%s", w1.s)); /* { dg-warning "nul-terminated" } */ - T (sprintf (D, "%.3s", w1.s)); - T (sprintf (D, "%.4s", w1.s)); /* { dg-warning "nul-terminated" } */ - - T (sprintf (D, "%.2s", w1.s + 1)); - T (sprintf (D, "%.3s", w1.s + 1)); /* { dg-warning "nul-terminated" } */ - - T (sprintf (D, "%s", &w2[0].a)); - T (sprintf (D, "%s", &w2[0].b)); /* { dg-warning "nul-terminated" } */ - T (sprintf (D, "%.1s", &w2[0].b)); - T (sprintf (D, "%.2s", &w2[0].b)); /* { dg-warning "nul-terminated" } */ - T (sprintf (D, "%s", w2[0].s)); - T (sprintf (D, "%.3s", w2[0].s)); - T (sprintf (D, "%.4s", w2[0].s)); - - T (sprintf (D, "%.2s", w2[0].s + 1)); - T (sprintf (D, "%.3s", w2[0].s + 1)); - - T (sprintf (D, "%s", &w2[1].a)); /* { dg-warning "nul-terminated" } */ - T (sprintf (D, "%.1s", &w2[1].a)); - T (sprintf (D, "%.2s", &w2[1].a)); /* { dg-warning "nul-terminated" } */ - T (sprintf (D, "%s", &w2[1].b)); - T (sprintf (D, "%s", w2[1].s)); /* { dg-warning "nul-terminated" } */ - T (sprintf (D, "%.3s", w2[1].s)); - T (sprintf (D, "%.4s", w2[1].s)); /* { dg-warning "nul-terminated" } */ - - T (sprintf (D, "%.2s", w2[1].s + 1)); - T (sprintf (D, "%.3s", w2[1].s + 1)); /* { dg-warning "nul-terminated" } */ - - T (sprintf (D, "%s", &wcs3[3])); - T (sprintf (D, "%s", &wcs3[4])); /* { dg-warning "\\\[-Warray-bounds" } */ -} diff --git a/gcc/testsuite/gcc.dg/warn-sprintf-no-nul-3.c b/gcc/testsuite/gcc.dg/warn-sprintf-no-nul-3.c new file mode 100644 index 00000000000..1cfbe62547a --- /dev/null +++ b/gcc/testsuite/gcc.dg/warn-sprintf-no-nul-3.c @@ -0,0 +1,71 @@ +/* PR tree-optimization/87756 - missing unterminated argument warning + using address of a constant character + { dg-do compile } + { dg-options "-O2 -Wall -ftrack-macro-expansion=0" } */ + +typedef __WCHAR_TYPE__ wchar_t; + +int sprintf (char*, const char*, ...); + +extern char* dest (void); +extern void sink (int, ...); + +#define D dest () +#define T(expr) sink (0, (expr)) + +const wchar_t wnul = L'\0'; +const wchar_t wnonul = L'a'; +const wchar_t wcs3[] = L"123"; + +const struct +{ + wchar_t a, b, s[3]; +} w1 = { L'\0', L'b', L"123" }, + w2[2] = { + { L'\0', L'c', L"12" }, + { L'd', L'\0', L"123" } + }; + +void test_sprintf_ls (void) +{ + T (sprintf (D, "%ls", &wnul)); + T (sprintf (D, "%ls", &wnonul)); /* { dg-warning "nul-terminated" } */ + T (sprintf (D, "%.1ls", &wnonul)); + T (sprintf (D, "%.2ls", &wnonul)); /* { dg-warning "nul-terminated" } */ + + T (sprintf (D, "%ls", &w1.a)); + T (sprintf (D, "%ls", &w1.b)); /* { dg-warning "nul-terminated" "pr88211" { xfail *-*-* } } */ + T (sprintf (D, "%.1ls", &w1.b)); + T (sprintf (D, "%.2ls", &w1.b)); /* { dg-warning "nul-terminated" "pr88211" { xfail *-*-* } } */ + T (sprintf (D, "%ls", w1.s)); /* { dg-warning "nul-terminated" } */ + T (sprintf (D, "%.3ls", w1.s)); + T (sprintf (D, "%.4ls", w1.s)); /* { dg-warning "nul-terminated" } */ + + T (sprintf (D, "%.2ls", w1.s + 1)); + T (sprintf (D, "%.3ls", w1.s + 1)); /* { dg-warning "nul-terminated" } */ + + T (sprintf (D, "%ls", &w2[0].a)); + T (sprintf (D, "%ls", &w2[0].b)); /* { dg-warning "nul-terminated" "pr88211" { xfail *-*-* } } */ + T (sprintf (D, "%.1ls", &w2[0].b)); + T (sprintf (D, "%.2ls", &w2[0].b)); /* { dg-warning "nul-terminated" "pr88211" { xfail *-*-* } } */ + T (sprintf (D, "%ls", w2[0].s)); + T (sprintf (D, "%.3ls", w2[0].s)); + T (sprintf (D, "%.4ls", w2[0].s)); + + T (sprintf (D, "%.2ls", w2[0].s + 1)); + T (sprintf (D, "%.3ls", w2[0].s + 1)); + + T (sprintf (D, "%ls", &w2[1].a)); /* { dg-warning "nul-terminated" "pr88211" { xfail *-*-* } } */ + T (sprintf (D, "%.1ls", &w2[1].a)); + T (sprintf (D, "%.2ls", &w2[1].a)); /* { dg-warning "nul-terminated" "pr88211" { xfail *-*-* } } */ + T (sprintf (D, "%ls", &w2[1].b)); + T (sprintf (D, "%ls", w2[1].s)); /* { dg-warning "nul-terminated" } */ + T (sprintf (D, "%.3ls", w2[1].s)); + T (sprintf (D, "%.4ls", w2[1].s)); /* { dg-warning "nul-terminated" } */ + + T (sprintf (D, "%.2ls", w2[1].s + 1)); + T (sprintf (D, "%.3ls", w2[1].s + 1));/* { dg-warning "nul-terminated" } */ + + T (sprintf (D, "%ls", &wcs3[3])); + T (sprintf (D, "%ls", &wcs3[4])); /* { dg-warning "\\\[-Warray-bounds" } */ +} -- 2.30.2