https://gcc.gnu.org/g:6adfb81ac2a530d4ae2a48276ac00013a2d7c575
commit r17-6533-g6adfb81ac2a530d4ae2a48276ac00013a2d7c575 Author: David Malcolm <[email protected]> Date: Fri Nov 20 20:27:22 2026 -0500 analyzer: add tests for 2 bugs fixed by pointer comparison rewrite I went through bugzilla and found the following bugs fixed by r17-609-g573b66baa6cb8d (aka "analyzer: fix pointer comparisons [PR125304]"). This patch adds test coverage for them remaining fixed. gcc/testsuite/ChangeLog: PR analyzer/109199 PR analyzer/124703 * c-c++-common/analyzer/pointer-comparison-pr109199.c: New test. * gcc.dg/analyzer/glibc-free-of-non-heap-pr124703.c: New test. Signed-off-by: David Malcolm <[email protected]> Diff: --- .../analyzer/pointer-comparison-pr109199.c | 9 +++ .../analyzer/glibc-free-of-non-heap-pr124703.c | 68 ++++++++++++++++++++++ 2 files changed, 77 insertions(+) diff --git a/gcc/testsuite/c-c++-common/analyzer/pointer-comparison-pr109199.c b/gcc/testsuite/c-c++-common/analyzer/pointer-comparison-pr109199.c new file mode 100644 index 000000000000..37c439c1efaf --- /dev/null +++ b/gcc/testsuite/c-c++-common/analyzer/pointer-comparison-pr109199.c @@ -0,0 +1,9 @@ +#include "analyzer-decls.h" + +void +test (void) +{ + int *b[1] = {}; + int **c = &b[0]; + __analyzer_eval((((c) + 1) == ((&b[0]) + 1))); /* { dg-warning "TRUE" "PR analyzer/109199" } */ +} diff --git a/gcc/testsuite/gcc.dg/analyzer/glibc-free-of-non-heap-pr124703.c b/gcc/testsuite/gcc.dg/analyzer/glibc-free-of-non-heap-pr124703.c new file mode 100644 index 000000000000..b4ce8834cf00 --- /dev/null +++ b/gcc/testsuite/gcc.dg/analyzer/glibc-free-of-non-heap-pr124703.c @@ -0,0 +1,68 @@ +/* Regression test for -Wanalyzer-free-of-non-heap false positive + (PR analyzer/124703) reduced from glibc's dynarray implementation + (malloc/dynarray-skeleton.c). */ + +/* { dg-additional-options "-O2" } */ + +typedef __SIZE_TYPE__ size_t; + +#define SCRATCH_SIZE 4 + +struct dynarray { + size_t used; + char *array; + char scratch[SCRATCH_SIZE]; +}; + +static void +dynarray_init (struct dynarray *list) +{ + list->used = 0; + list->array = list->scratch; +} + +static inline void +dynarray_free_array (struct dynarray *list) +{ + if (list->array != list->scratch) + __builtin_free (list->array); /* { dg-bogus "-Wanalyzer-free-of-non-heap" "PR analyzer/124703" } */ +} + +static void +dynarray_free (struct dynarray *list) +{ + dynarray_free_array (list); + dynarray_init (list); +} + +static int +dynarray_add (struct dynarray *list, char c) +{ + /* Don't add past the scratch space. But this never happens in this test. */ + if (list->used == SCRATCH_SIZE) + return 1; + + /* Add element when scratch space is still available. */ + list->array[list->used++] = c; + return 0; +} + +struct parser { + struct dynarray items; +}; + +int +main (void) +{ + struct parser p; + dynarray_init (&p.items); + + /* All these adds only go into the scratch space. */ + dynarray_add (&p.items, 'a'); + dynarray_add (&p.items, 'b'); + dynarray_add (&p.items, 'c'); + dynarray_add (&p.items, 'd'); + + dynarray_free (&p.items); + return 0; +}
