From 474da67ef9ec3658e4da9deb5373353532b2a840 Mon Sep 17 00:00:00 2001 From: Martin Sebor Date: Wed, 14 Dec 2016 17:23:16 +0000 Subject: PR c/78673 - sprintf missing attribute nonnull on destination argument PR c/78673 - sprintf missing attribute nonnull on destination argument PR c/17308 - nonnull attribute not as useful as it could be gcc/ChangeLog: PR c/17308 * builtin-attrs.def (ATTR_NONNULL_1_1, ATTR_NONNULL_1_2): Defined. (ATTR_NONNULL_1_3, ATTR_NONNULL_1_4, ATTR_NONNULL_1_5): Same. (ATTR_NOTHROW_NONNULL_1_1, ATTR_NOTHROW_NONNULL_1_2): Same. (ATTR_NOTHROW_NONNULL_1_3, ATTR_NOTHROW_NONNULL_1_4): Same. (ATTR_NOTHROW_NONNULL_1_5): Same. (ATTR_NONNULL_1_FORMAT_PRINTF_1_2): Same. (ATTR_NONNULL_1_FORMAT_PRINTF_2_0): Same. (ATTR_NONNULL_1_FORMAT_PRINTF_2_3): Same. (ATTR_NONNULL_1_FORMAT_PRINTF_3_0): Same. (ATTR_NONNULL_1_FORMAT_PRINTF_3_4): Same. (ATTR_NONNULL_1_FORMAT_PRINTF_4_0): Same. (ATTR_NONNULL_1_FORMAT_PRINTF_4_5): Same. * builtins.c (validate_arg): Add argument. Treat null pointers passed to nonnull arguments as invalid. (validate_arglist): Same. * builtins.def (fprintf, fprintf_unlocked): Add nonnull attribute. (printf, printf_unlocked, sprintf. vfprintf, vsprintf): Same. (__sprintf_chk, __vsprintf_chk, __fprintf_chk, __vfprintf_chk): Same. * calls.c (get_nonnull_ags, maybe_warn_null_arg): New functions. (initialize_argument_information): Diagnose null pointers passed to arguments declared nonnull. * calls.h (get_nonnull_args): Declared. gcc/c-family/ChangeLog: PR c/17308 * c-common.c (check_nonnull_arg): Disable when optimization is enabled. gcc/testsuite/ChangeLog: PR c/17308 * gcc.dg/builtins-nonnull.c: New test. * gcc.dg/nonnull-4.c: New test. From-SVN: r243661 --- gcc/testsuite/gcc.dg/nonnull-4.c | 79 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/nonnull-4.c (limited to 'gcc/testsuite/gcc.dg/nonnull-4.c') diff --git a/gcc/testsuite/gcc.dg/nonnull-4.c b/gcc/testsuite/gcc.dg/nonnull-4.c new file mode 100644 index 0000000..577a04c --- /dev/null +++ b/gcc/testsuite/gcc.dg/nonnull-4.c @@ -0,0 +1,79 @@ +/* PR c/78673 - sprintf missing attribute nonnull on destination argument + Test to verify that calls to user-defined functions declared with + the "nonnull" function attribute are diagnosed. */ +/* { dg-do compile } */ +/* { dg-options "-O2 -Wnonnull" } */ + +#define N(...) __attribute__ ((nonnull (__VA_ARGS__))) + +void N (1) f1_1 (void*); + +void N (1) f2_1 (void*, void*); +void N (1) N (2) f2_1_2 (void*, void*); + +void N (1) N (3) f3_1_3 (void*, void*, void*); + +void N (1, 2) N (4) g4_1_2_4 (void*, void*, void*, void*); +void N (1, 3) N (4) g4_1_3_4 (void*, void*, void*, void*); +void N (2, 3, 4) g4_2_3_4 (void*, void*, void*, void*); + +void N () g4_all (void*, void*, void*, void*); + +void N (1, 3, 5, 7, 11, 13) +g16_1_3_5_7_11_13 (void*, void*, void*, void*, + void*, void*, void*, void*, + void*, void*, void*, void*, + void*, void*, void*, void*); + +void* null (void) { return 0; } + +void test (void) +{ + void *p0 = null (); + void *px = &px; + + f1_1 (p0); /* { dg-warning "argument 1 null where non-null expected " } */ + f1_1 (px); + + f2_1 (p0, px); /* { dg-warning "argument 1 null" } */ + f2_1 (px, p0); + f2_1 (p0, p0); /* { dg-warning "argument 1 null" } */ + + f2_1_2 (p0, px); /* { dg-warning "argument 1 null" } */ + f2_1_2 (px, p0); /* { dg-warning "argument 2 null" } */ + f2_1_2 (p0, p0); /* { dg-warning "argument 1 null" } */ + /* { dg-warning "argument 2 null" "argument 2" { target *-*-* } .-1 } */ + + f3_1_3 (p0, px, px); /* { dg-warning "argument 1 null" } */ + f3_1_3 (px, p0, px); + f3_1_3 (px, px, p0); /* { dg-warning "argument 3 null" } */ + f3_1_3 (p0, p0, px); /* { dg-warning "argument 1 null" } */ + f3_1_3 (px, p0, p0); /* { dg-warning "argument 3 null" } */ + f3_1_3 (p0, p0, p0); /* { dg-warning "argument 1 null" } */ + /* { dg-warning "argument 3 null" "argument 3" { target *-*-* } .-1 } */ + + g4_1_2_4 (p0, px, px, px); /* { dg-warning "argument 1 null" } */ + g4_1_2_4 (px, p0, px, px); /* { dg-warning "argument 2 null" } */ + g4_1_2_4 (px, px, p0, px); + g4_1_2_4 (px, px, px, p0); /* { dg-warning "argument 4 null" } */ + + g4_1_3_4 (p0, px, px, px); /* { dg-warning "argument 1 null" } */ + g4_1_3_4 (px, p0, px, px); + g4_1_3_4 (px, px, p0, px); /* { dg-warning "argument 3 null" } */ + g4_1_3_4 (px, px, px, p0); /* { dg-warning "argument 4 null" } */ + + g4_2_3_4 (p0, px, px, px); + g4_2_3_4 (px, p0, px, px); /* { dg-warning "argument 2 null" } */ + g4_2_3_4 (px, px, p0, px); /* { dg-warning "argument 3 null" } */ + g4_2_3_4 (px, px, px, p0); /* { dg-warning "argument 4 null" } */ + + g4_all (p0, px, px, px); /* { dg-warning "argument 1 null" } */ + g4_all (px, p0, px, px); /* { dg-warning "argument 2 null" } */ + g4_all (px, px, p0, px); /* { dg-warning "argument 3 null" } */ + g4_all (px, px, px, p0); /* { dg-warning "argument 4 null" } */ + + g16_1_3_5_7_11_13 (px, px, px, px, px, px, px, px, + px, px, px, px, px, px, px, px); + + g16_1_3_5_7_11_13 (px, p0, px, p0, px, p0, px, p0, p0, p0, px, p0, p0, p0, p0, p0); /* { dg-warning "argument 13 null" } */ +} -- cgit v1.1