Debian Codegraph @

gcc/testsuite/c-c++-common/analyzer/sprintf-concat.c

raw
1 typedef __SIZE_TYPE__ size_t;
2 #include "../../gcc.dg/analyzer/analyzer-decls.h"
3
4 extern size_t
5 strlen(const char* __s) __attribute__((__nothrow__, __leaf__))
6 __attribute__((__pure__)) __attribute__((__nonnull__(1)));
7
8 extern void*
9 malloc(size_t __size) __attribute__((__nothrow__, __leaf__))
10 __attribute__((__malloc__)) __attribute__((__alloc_size__(1)));
11
12 extern int
13 sprintf(char* __restrict __s, const char* __restrict, ...)
14 __attribute__((__nothrow__));
15
16 char *
17 test_1 (const char *a, const char *b)
18 {
19 size_t sz = strlen (a) + strlen (b) + 2;
20 char *p = (char *) malloc (sz);
21 if (!p)
22 return NULL;
23 sprintf (p, "%s/%s", a, b);
24 return p;
25 }
26
27 void
28 test_2 (const char *a, const char *b)
29 {
30 size_t sz = strlen (a) + strlen (b) + 2;
31 char *p = (char *) malloc (sz); /* { dg-message "allocated here" } */
32 if (!p)
33 return;
34 sprintf (p, "%s/%s", a, b); /* { dg-warning "leak of 'p' " } */
35 }