From: David Malcolm <dmalcolm@redhat.com>
To: gcc-patches@gcc.gnu.org
Cc: David Malcolm <dmalcolm@redhat.com>
Subject: [committed] analyzer: add test coverage for flexible array members [PR98247]
Date: Fri, 9 Sep 2022 17:24:01 -0400 [thread overview]
Message-ID: <20220909212401.626138-1-dmalcolm@redhat.com> (raw)
Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu.
Pushed to trunk as r13-2571-g084dc9a0c6cec1.
gcc/testsuite/ChangeLog:
PR analyzer/98247
* gcc.dg/analyzer/flexible-array-member-1.c: New test.
Signed-off-by: David Malcolm <dmalcolm@redhat.com>
---
.../gcc.dg/analyzer/flexible-array-member-1.c | 100 ++++++++++++++++++
1 file changed, 100 insertions(+)
create mode 100644 gcc/testsuite/gcc.dg/analyzer/flexible-array-member-1.c
diff --git a/gcc/testsuite/gcc.dg/analyzer/flexible-array-member-1.c b/gcc/testsuite/gcc.dg/analyzer/flexible-array-member-1.c
new file mode 100644
index 00000000000..2df085a43f2
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/analyzer/flexible-array-member-1.c
@@ -0,0 +1,100 @@
+#include <stdlib.h>
+#include <string.h>
+
+struct str {
+ size_t len;
+ char data[];
+};
+
+struct str *
+test_const_size (void)
+{
+ struct str *str = malloc(sizeof(str) + 10);
+ if (str) {
+ str->len = 10;
+ memset(str->data, 'x', 10);
+ return str;
+ }
+ return NULL;
+}
+
+struct str *
+test_const_size_oob_1 (void)
+{
+ /* Forgetting to add space for the trailing array. */
+ struct str *str = malloc(sizeof(str));
+ if (str) {
+ str->len = 10;
+ memset(str->data, 'x', 10); /* { dg-warning "heap-based buffer overflow" "Wanalyzer-out-of-bounds" } */
+ /* { dg-warning "'memset' writing 10 bytes into a region of size 0 overflows the destination" "Wstringop-overflow" { target *-*-* } .-1 } */
+ return str;
+ }
+ return NULL;
+}
+
+struct str *
+test_const_size_oob_2 (void)
+{
+ struct str *str = malloc(sizeof(str) + 10);
+ if (str) {
+ str->len = 10;
+ /* Using the wrong size here. */
+ memset(str->data, 'x', 11); /* { dg-warning "heap-based buffer overflow" "Wanalyzer-out-of-bounds" } */
+ /* { dg-warning "'memset' writing 11 bytes into a region of size 10 overflows the destination" "Wstringop-overflow" { target *-*-* } .-1 } */
+ return str;
+ }
+ return NULL;
+}
+
+struct str *
+test_symbolic_size (size_t len)
+{
+ struct str *str = malloc(sizeof(str) + len);
+ if (str) {
+ str->len = len;
+ memset(str->data, 'x', len);
+ return str;
+ }
+ return NULL;
+}
+
+struct str *
+test_symbolic_size_oob (size_t len)
+{
+ /* Forgetting to add space for the trailing array. */
+ struct str *str = malloc(sizeof(str));
+ if (str) {
+ str->len = len;
+ memset(str->data, 'x', len); /* { dg-warning "heap-based buffer overflow" "PR analyzer/98247" { xfail *-*-* } } */
+ // TODO(xfail): we don't yet complain about this case, which occurs when len > 0
+ return str;
+ }
+ return NULL;
+}
+
+struct str *
+test_symbolic_size_with_terminator (size_t len)
+{
+ struct str *str = malloc(sizeof(str) + len + 1);
+ if (str) {
+ str->len = len;
+ memset(str->data, 'x', len);
+ str->data[len] = '\0';
+ return str;
+ }
+ return NULL;
+}
+
+struct str *
+test_symbolic_size_with_terminator_oob (size_t len)
+{
+ /* Forgetting to add 1 for the terminator. */
+ struct str *str = malloc(sizeof(str) + len);
+ if (str) {
+ str->len = len;
+ memset(str->data, 'x', len);
+ str->data[len] = '\0'; /* { dg-warning "heap-based buffer overflow" } */
+ return str;
+ }
+ return NULL;
+}
--
2.26.3
reply other threads:[~2022-09-09 21:24 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=20220909212401.626138-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).