analyzer: Fix PR analyzer/101980
[official-gcc.git] / gcc / testsuite / gcc.dg / analyzer / pr99193-1.c
blobc6179e9894884f47dc0fe61c881e269d687e4291
1 /* Verify absence of false positive from -Wanalyzer-mismatching-deallocation
2 on realloc(3).
3 Based on https://github.com/libguestfs/libguestfs/blob/f19fd566f6387ce7e4d82409528c9dde374d25e0/daemon/command.c#L115
4 which is GPLv2 or later. */
6 typedef __SIZE_TYPE__ size_t;
7 typedef __builtin_va_list va_list;
9 #define NULL ((void *)0)
11 extern void *malloc (size_t __size)
12 __attribute__ ((__nothrow__ , __leaf__))
13 __attribute__ ((__malloc__))
14 __attribute__ ((__alloc_size__ (1)));
15 extern void perror (const char *__s);
16 extern void *realloc (void *__ptr, size_t __size)
17 __attribute__ ((__nothrow__ , __leaf__))
18 __attribute__ ((__warn_unused_result__))
19 __attribute__ ((__alloc_size__ (2)));
21 extern void guestfs_int_cleanup_free (void *ptr);
22 extern int commandrvf (char **stdoutput, char **stderror, unsigned flags,
23 char const* const *argv);
24 #define CLEANUP_FREE __attribute__((cleanup(guestfs_int_cleanup_free)))
26 int
27 commandrf (char **stdoutput, char **stderror, unsigned flags,
28 const char *name, ...)
30 va_list args;
31 CLEANUP_FREE const char **argv = NULL;
32 char *s;
33 int i, r;
35 /* Collect the command line arguments into an array. */
36 i = 2;
37 argv = malloc (sizeof (char *) * i);
39 if (argv == NULL) {
40 perror ("malloc");
41 return -1;
43 argv[0] = (char *) name;
44 argv[1] = NULL;
46 __builtin_va_start (args, name);
48 while ((s = __builtin_va_arg (args, char *)) != NULL) {
49 const char **p = realloc (argv, sizeof (char *) * (++i)); /* { dg-bogus "'free'" } */
50 if (p == NULL) {
51 perror ("realloc");
52 __builtin_va_end (args);
53 return -1;
55 argv = p;
56 argv[i-2] = s;
57 argv[i-1] = NULL;
60 __builtin_va_end (args);
62 r = commandrvf (stdoutput, stderror, flags, argv);
64 return r;