Debian Codegraph @

gcc/testsuite/c-c++-common/analyzer/pr99193-1-noexcept.c

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