https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113984
Bug ID: 113984
Summary: -Wfree-nonheap-object false positive with VLA
parameter
Product: gcc
Version: 13.2.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: alx at kernel dot org
Target Milestone: ---
I can reproduce it with both of these:
$ gcc-13 --version | head -n1
gcc-13 (Debian 13.2.0-13) 13.2.0
$ gcc-14 --version | head -n1
gcc-14 (Debian 14-20240201-3) 14.0.1 20240131 (experimental) [master
r14-8680-g2f14c0dbb78]
See a small reproducer:
```c
#include <err.h>
#include <stddef.h>
#include <stdlib.h>
static const char **add_string(size_t *restrict n,
const char *strings[restrict *n], const char *restrict string);
int
main(int argc, char *argv[argc + 1])
{
size_t n = 0;
const char **strings = NULL;
add_string(&n, strings, argv[0]);
}
static const char **
add_string(size_t *restrict n, const char *strings[restrict *n],
const char *restrict string)
{
strings = reallocarray(strings, ++*n, sizeof(const char *));
if (strings == NULL)
err(EXIT_FAILURE, "reallocarray(3)");
strings[*n - 1] = string;
return strings;
}
```
alx@debian:~/tmp$ gcc-13 -Wall nonheap.c
nonheap.c: In function ‘add_string’:
nonheap.c:24:19: warning: ‘reallocarray’ called on unallocated object ‘strings’
[-Wfree-nonheap-object]
24 | strings = reallocarray(strings, ++*n, sizeof(const char *));
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
nonheap.c:21:44: note: declared here
21 | add_string(size_t *restrict n, const char *strings[restrict *n],
| ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~
alx@debian:~/tmp$ gcc-14 -Wall nonheap.c
nonheap.c: In function ‘add_string’:
nonheap.c:24:19: warning: ‘reallocarray’ called on unallocated object ‘strings’
[-Wfree-nonheap-object]
24 | strings = reallocarray(strings, ++*n, sizeof(const char *));
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
nonheap.c:21:44: note: declared here
21 | add_string(size_t *restrict n, const char *strings[restrict *n],
| ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~
If I change the function to have the parameter be `const char **restrict
strings`, the warning vanishes.