From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2209) id 6A6173858431; Wed, 1 Feb 2023 02:22:21 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6A6173858431 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1675218141; bh=WddJUUyfV13xB/L7Uh/4P1UYd6f+rqwSMl8SA1fQSSs=; h=From:To:Subject:Date:From; b=F72DTKAAB3wu/IUOMAdvlHeIm2A7MN5V0rQuN37zsGE0Gzx5geDP8VoHIqQ3R1gnW nXAGe9l0mteliAUOZJvnB9TKPfzzCoEOdsRtI3+KbyCJaBEyYAL+2lnx6Hm2qO3zT8 v3ACCT+40jfMV3O7red7H24OqVZUmalxJXTe8MKQ= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: David Malcolm To: gcc-cvs@gcc.gnu.org Subject: [gcc r13-5614] analyzer: fix -Wanalyzer-allocation-size false -ve on alloca [PR108616] X-Act-Checkin: gcc X-Git-Author: David Malcolm X-Git-Refname: refs/heads/master X-Git-Oldrev: a90316c6ceddfbb47b3c2161baf446ccb87df5ff X-Git-Newrev: 70d34f2a30a5f1a2a09f547d92243db32d79f3f7 Message-Id: <20230201022221.6A6173858431@sourceware.org> Date: Wed, 1 Feb 2023 02:22:21 +0000 (GMT) List-Id: https://gcc.gnu.org/g:70d34f2a30a5f1a2a09f547d92243db32d79f3f7 commit r13-5614-g70d34f2a30a5f1a2a09f547d92243db32d79f3f7 Author: David Malcolm Date: Tue Jan 31 21:18:10 2023 -0500 analyzer: fix -Wanalyzer-allocation-size false -ve on alloca [PR108616] gcc/analyzer/ChangeLog: PR analyzer/108616 * pending-diagnostic.cc (fixup_location_in_macro_p): Add "alloca" to macros that we shouldn't unwind inside. gcc/testsuite/ChangeLog: PR analyzer/108616 * gcc.dg/analyzer/allocation-size-multiline-3.c: New test. * gcc.dg/analyzer/test-alloca.h: New test. Signed-off-by: David Malcolm Diff: --- gcc/analyzer/pending-diagnostic.cc | 6 +++ .../gcc.dg/analyzer/allocation-size-multiline-3.c | 44 ++++++++++++++++++++++ gcc/testsuite/gcc.dg/analyzer/test-alloca.h | 3 ++ 3 files changed, 53 insertions(+) diff --git a/gcc/analyzer/pending-diagnostic.cc b/gcc/analyzer/pending-diagnostic.cc index 79e6c5528eb..e36ed4fd9c1 100644 --- a/gcc/analyzer/pending-diagnostic.cc +++ b/gcc/analyzer/pending-diagnostic.cc @@ -139,6 +139,12 @@ static bool fixup_location_in_macro_p (cpp_hashnode *macro) { ht_identifier ident = macro->ident; + + /* Don't unwind inside "alloca" macro, so that we don't suppress warnings + from it (due to being in system headers). */ + if (ht_ident_eq (ident, "alloca")) + return true; + /* Don't unwind inside macros, so that we don't suppress warnings from them (due to being in system headers). */ if (ht_ident_eq (ident, "va_start") diff --git a/gcc/testsuite/gcc.dg/analyzer/allocation-size-multiline-3.c b/gcc/testsuite/gcc.dg/analyzer/allocation-size-multiline-3.c new file mode 100644 index 00000000000..e27364a8e83 --- /dev/null +++ b/gcc/testsuite/gcc.dg/analyzer/allocation-size-multiline-3.c @@ -0,0 +1,44 @@ +/* Verify that we warn for incorrect uses of "alloca" (which may be in a + macro in a system header), and that the output looks correct. */ + +/* { dg-additional-options "-fdiagnostics-path-format=inline-events -fdiagnostics-show-caret -fanalyzer-fine-grained" } */ +/* { dg-require-effective-target alloca } */ + +#include +#include "test-alloca.h" + +void test_constant_99 (void) +{ + int32_t *ptr = alloca (99); /* { dg-warning "allocated buffer size is not a multiple of the pointee's size" } */ +} + +/* { dg-begin-multiline-output "" } + int32_t *ptr = alloca (99); + ^~~~~~ + 'test_constant_99': events 1-2 + | + | int32_t *ptr = alloca (99); + | ^~~~~~ + | | + | (1) allocated 99 bytes here + | (2) assigned to 'int32_t *' {aka 'int *'} here; 'sizeof (int32_t {aka int})' is '4' + | + { dg-end-multiline-output "" } */ + +void test_symbolic (int n) +{ + int32_t *ptr = alloca (n * 2); /* { dg-warning "allocated buffer size is not a multiple of the pointee's size" } */ +} + +/* { dg-begin-multiline-output "" } + int32_t *ptr = alloca (n * 2); + ^~~~~~ + 'test_symbolic': events 1-2 + | + | int32_t *ptr = alloca (n * 2); + | ^~~~~~ + | | + | (1) allocated 'n * 2' bytes here + | (2) assigned to 'int32_t *' {aka 'int *'} here; 'sizeof (int32_t {aka int})' is '4' + | + { dg-end-multiline-output "" } */ diff --git a/gcc/testsuite/gcc.dg/analyzer/test-alloca.h b/gcc/testsuite/gcc.dg/analyzer/test-alloca.h new file mode 100644 index 00000000000..f4045f92adc --- /dev/null +++ b/gcc/testsuite/gcc.dg/analyzer/test-alloca.h @@ -0,0 +1,3 @@ +#pragma GCC system_header + +#define alloca(X) __builtin_alloca(X)