public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r13-6640] testsuite: add test coverage for PR analyzer/108045
@ 2023-03-13 18:54 David Malcolm
  0 siblings, 0 replies; only message in thread
From: David Malcolm @ 2023-03-13 18:54 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:129664c67b774511b53bfeee9e80a142ae7cd026

commit r13-6640-g129664c67b774511b53bfeee9e80a142ae7cd026
Author: David Malcolm <dmalcolm@redhat.com>
Date:   Mon Mar 13 14:53:04 2023 -0400

    testsuite: add test coverage for PR analyzer/108045
    
    PR analyzer/108045 reports a leak false positive from -fanalyzer.
    
    The false +ve as reported seems to have been fixed by
    r13-6589-g14f5e56a8a766c, but with -fanalyzer-call-summaries there are
    two additional false +ves which aren't fixed by that commit.
    
    This patch adds test coverage for these cases.
    
    gcc/testsuite/ChangeLog:
            PR analyzer/108045
            * gcc.dg/analyzer/leak-pr108045-with-call-summaries.c: New test.
            * gcc.dg/analyzer/leak-pr108045-without-call-summaries.c: New test.
    
    Signed-off-by: David Malcolm <dmalcolm@redhat.com>

Diff:
---
 .../analyzer/leak-pr108045-with-call-summaries.c   | 106 ++++++++++++++++++++
 .../leak-pr108045-without-call-summaries.c         | 107 +++++++++++++++++++++
 2 files changed, 213 insertions(+)

diff --git a/gcc/testsuite/gcc.dg/analyzer/leak-pr108045-with-call-summaries.c b/gcc/testsuite/gcc.dg/analyzer/leak-pr108045-with-call-summaries.c
new file mode 100644
index 00000000000..d63be06a9bb
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/analyzer/leak-pr108045-with-call-summaries.c
@@ -0,0 +1,106 @@
+/* { dg-additional-options "-fanalyzer-call-summaries" } */
+
+typedef __SIZE_TYPE__ size_t;
+#define NULL ((void *)0)
+
+/* data structures */
+
+struct screen_s {
+	size_t rows;
+	size_t cols;
+	char **data;
+};
+
+struct context_s {
+	struct screen_s *scr;
+};
+
+/* global context variable */
+
+static struct context_s *ctx;
+
+/* prototypes */
+
+struct screen_s *screen_create(size_t cols, size_t rows);
+void screen_destroy(struct screen_s *scr);
+void resize_screen(size_t cols, size_t rows);
+
+/* functions */
+
+struct screen_s *screen_create(size_t cols, size_t rows)
+{
+	struct screen_s *result = NULL;
+
+	result = __builtin_calloc(1, sizeof(*result));
+	if (!result)
+		return NULL;
+
+	result->cols = cols;
+	result->rows = rows;
+
+	/* make one allocation which will be accessed like a 2D array */
+	result->data = __builtin_calloc(rows, sizeof(result->data) + sizeof(*result->data) * cols);
+	if (!result->data) {
+		__builtin_free(result);
+		return NULL;
+	}
+
+	/* obtain pointer to start of data area */
+	char *ptr = (char *)(result->data + rows);
+
+	/* setup pointers for each row of data to allow 2D array access */
+	for (size_t row = 0; row < rows; row++)
+		result->data[row] = (ptr + row * cols);
+	/* array can now be accessed like data[row][col] */
+
+	return result;
+}
+
+void screen_destroy(struct screen_s *scr)
+{
+	if (!scr)
+		return;
+
+	__builtin_free(scr->data);
+
+	scr->data = NULL;
+	scr->rows = 0;
+	scr->cols = 0;
+
+	__builtin_free(scr);
+}
+
+void resize_screen(size_t cols, size_t rows)
+{
+	/* create a new screen */
+	struct screen_s *new_scr = NULL;
+	new_scr = screen_create(cols, rows); /* { dg-bogus "leak" "PR 108045" { xfail *-*-* } } */
+	if (!new_scr) {
+		return;
+	}
+
+	/* swap the old screen with the new one */
+	struct screen_s *old_scr = ctx->scr;
+	ctx->scr = new_scr;
+
+	/* omitted: copy the old screen contents to the new screen */
+
+	/* free the old screen */
+	screen_destroy(old_scr);
+}
+
+int main(void)
+{
+	ctx = __builtin_calloc(1, sizeof(*ctx));
+	if (!ctx)
+		__builtin_abort();
+
+	ctx->scr = screen_create(80, 25); /* { dg-bogus "leak" "PR 108045" { xfail *-*-* } } */
+	resize_screen(100, 20);
+
+	/* tidy up and quit */
+	screen_destroy(ctx->scr);
+	__builtin_free(ctx);
+	ctx = NULL;
+	return 0;
+}
diff --git a/gcc/testsuite/gcc.dg/analyzer/leak-pr108045-without-call-summaries.c b/gcc/testsuite/gcc.dg/analyzer/leak-pr108045-without-call-summaries.c
new file mode 100644
index 00000000000..ae7a7d5c159
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/analyzer/leak-pr108045-without-call-summaries.c
@@ -0,0 +1,107 @@
+/* { dg-additional-options "-fno-analyzer-call-summaries" } */
+/* { dg-additional-options "-Wno-analyzer-too-complex" } */
+
+typedef __SIZE_TYPE__ size_t;
+#define NULL ((void *)0)
+
+/* data structures */
+
+struct screen_s {
+	size_t rows;
+	size_t cols;
+	char **data;
+};
+
+struct context_s {
+	struct screen_s *scr;
+};
+
+/* global context variable */
+
+static struct context_s *ctx;
+
+/* prototypes */
+
+struct screen_s *screen_create(size_t cols, size_t rows);
+void screen_destroy(struct screen_s *scr);
+void resize_screen(size_t cols, size_t rows);
+
+/* functions */
+
+struct screen_s *screen_create(size_t cols, size_t rows)
+{
+	struct screen_s *result = NULL;
+
+	result = __builtin_calloc(1, sizeof(*result));
+	if (!result)
+		return NULL;
+
+	result->cols = cols;
+	result->rows = rows;
+
+	/* make one allocation which will be accessed like a 2D array */
+	result->data = __builtin_calloc(rows, sizeof(result->data) + sizeof(*result->data) * cols);
+	if (!result->data) {
+		__builtin_free(result);
+		return NULL;
+	}
+
+	/* obtain pointer to start of data area */
+	char *ptr = (char *)(result->data + rows);
+
+	/* setup pointers for each row of data to allow 2D array access */
+	for (size_t row = 0; row < rows; row++)
+		result->data[row] = (ptr + row * cols);
+	/* array can now be accessed like data[row][col] */
+
+	return result;
+}
+
+void screen_destroy(struct screen_s *scr)
+{
+	if (!scr)
+		return;
+
+	__builtin_free(scr->data);
+
+	scr->data = NULL;
+	scr->rows = 0;
+	scr->cols = 0;
+
+	__builtin_free(scr); /* { dg-bogus "leak" } */
+}
+
+void resize_screen(size_t cols, size_t rows)
+{
+	/* create a new screen */
+	struct screen_s *new_scr = NULL;
+	new_scr = screen_create(cols, rows); /* { dg-bogus "leak" } */
+	if (!new_scr) {
+		return;
+	}
+
+	/* swap the old screen with the new one */
+	struct screen_s *old_scr = ctx->scr;
+	ctx->scr = new_scr;
+
+	/* omitted: copy the old screen contents to the new screen */
+
+	/* free the old screen */
+	screen_destroy(old_scr);
+}
+
+int main(void)
+{
+	ctx = __builtin_calloc(1, sizeof(*ctx));
+	if (!ctx)
+		__builtin_abort();
+
+	ctx->scr = screen_create(80, 25); /* { dg-bogus "leak" } */
+	resize_screen(100, 20);
+
+	/* tidy up and quit */
+	screen_destroy(ctx->scr);
+	__builtin_free(ctx);
+	ctx = NULL;
+	return 0;
+}

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-03-13 18:54 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-13 18:54 [gcc r13-6640] testsuite: add test coverage for PR analyzer/108045 David Malcolm

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