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
This commit is contained in:
Martin Sebor 2018-11-26 16:55:36 -07:00
parent 426a04b84f
commit 67b9fab0e6
5 changed files with 93 additions and 65 deletions

View File

@ -1,3 +1,9 @@
2018-11-26 Martin Sebor <msebor@redhat.com>
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 <amodra@gmail.com>
* config.gcc (powerpc*-*-linux*): Add linux.h to tm_file.

View File

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

View File

@ -1,3 +1,9 @@
2018-11-26 Martin Sebor <msebor@redhat.com>
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 <polacek@redhat.com>
PR c++/88120 - ICE when calling save_expr in a template.
@ -26,7 +32,7 @@
2018-11-26 Jozef Lawrynowicz <jozef.l@mittosystems.com>
* 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 <ubizjak@gmail.com>

View File

@ -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" } */
}

View File

@ -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" } */
}