From: David Malcolm <dmalcolm@redhat.com>
To: gcc-patches@gcc.gnu.org
Cc: David Malcolm <dmalcolm@redhat.com>
Subject: [pushed] analyzer: fix -Wanalyzer-allocation-size false -ve on alloca [PR108616]
Date: Tue, 31 Jan 2023 21:26:15 -0500 [thread overview]
Message-ID: <20230201022615.824902-1-dmalcolm@redhat.com> (raw)
Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu.
Pushed to trunk as r13-5614-g70d34f2a30a5f1.
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 <dmalcolm@redhat.com>
---
gcc/analyzer/pending-diagnostic.cc | 6 +++
.../analyzer/allocation-size-multiline-3.c | 44 +++++++++++++++++++
gcc/testsuite/gcc.dg/analyzer/test-alloca.h | 3 ++
3 files changed, 53 insertions(+)
create mode 100644 gcc/testsuite/gcc.dg/analyzer/allocation-size-multiline-3.c
create mode 100644 gcc/testsuite/gcc.dg/analyzer/test-alloca.h
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 <stdarg.h> 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 <stdint.h>
+#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)
--
2.26.3
reply other threads:[~2023-02-01 2:26 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230201022615.824902-1-dmalcolm@redhat.com \
--to=dmalcolm@redhat.com \
--cc=gcc-patches@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).