Debian Codegraph @

gcc/testsuite/gcc.dg/analyzer/pr99193-1.c

raw
1 /* { dg-additional-options "-Wno-analyzer-too-complex" } */
2
3 /* C only: C++ exceptions cause a Wanalyzer-va-list-leak warning to be emitted
4 at the end of commandrf. Therefore this test has been duplicated as
5 c-c++-common/analyzer/pr99193-1-noexcept.c */
6
7 /* Verify absence of false positive from -Wanalyzer-mismatching-deallocation
8 on realloc(3).
9 Based on https://github.com/libguestfs/libguestfs/blob/f19fd566f6387ce7e4d82409528c9dde374d25e0/daemon/command.c#L115
10 which is GPLv2 or later. */
11
12 typedef __SIZE_TYPE__ size_t;
13 typedef __builtin_va_list va_list;
14
15 #include "analyzer-decls.h"
16
17 extern void *malloc (size_t __size)
18 __attribute__ ((__nothrow__ , __leaf__))
19 __attribute__ ((__malloc__))
20 __attribute__ ((__alloc_size__ (1)));
21 extern void perror (const char *__s);
22 extern void *realloc (void *__ptr, size_t __size)
23 __attribute__ ((__nothrow__ , __leaf__))
24 __attribute__ ((__warn_unused_result__))
25 __attribute__ ((__alloc_size__ (2)));
26
27 extern void guestfs_int_cleanup_free (void *ptr);
28 extern int commandrvf (char **stdoutput, char **stderror, unsigned flags,
29 char const* const *argv);
30 #define CLEANUP_FREE __attribute__((cleanup(guestfs_int_cleanup_free)))
31
32 int
33 commandrf (char **stdoutput, char **stderror, unsigned flags,
34 const char *name, ...)
35 {
36 va_list args;
37 CLEANUP_FREE const char **argv = NULL;
38 char *s;
39 int i, r;
40
41 /* Collect the command line arguments into an array. */
42 i = 2;
43 argv = (const char **) malloc (sizeof (char *) * i);
44
45 if (argv == NULL) {
46 perror ("malloc");
47 return -1;
48 }
49 argv[0] = (char *) name;
50 argv[1] = NULL;
51
52 __builtin_va_start (args, name);
53
54 while ((s = __builtin_va_arg (args, char *)) != NULL) {
55 const char **p = (const char **) realloc (argv, sizeof (char *) * (++i)); /* { dg-bogus "'free'" } */
56 if (p == NULL) {
57 perror ("realloc");
58 __builtin_va_end (args);
59 return -1;
60 }
61 argv = p;
62 argv[i-2] = s;
63 argv[i-1] = NULL;
64 }
65
66 __builtin_va_end (args);
67
68 r = commandrvf (stdoutput, stderror, flags, argv);
69
70 return r;
71 }