Debian Codegraph @

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

raw
1 /* See e.g. https://en.cppreference.com/w/c/io/fprintf
2 and https://www.man7.org/linux/man-pages/man3/sprintf.3.html */
3
4 extern int
5 sprintf(char* dst, const char* fmt, ...)
6 __attribute__((__nothrow__));
7
8 #include "../../gcc.dg/analyzer/analyzer-decls.h"
9
10 int
11 test_passthrough (char* dst, const char* fmt)
12 {
13 /* This assumes that fmt doesn't have any arguments. */
14 return sprintf (dst, fmt);
15 }
16
17 void
18 test_known (void)
19 {
20 char buf[10];
21 int res = sprintf (buf, "foo");
22 /* TODO: ideally we would know the value of "res" is 3,
23 and known the content and strlen of "buf" after the call */
24 }
25
26 int
27 test_null_dst (void)
28 {
29 return sprintf (NULL, "hello world"); /* { dg-warning "use of NULL where non-null expected" } */
30 }
31
32 int
33 test_null_fmt (char *dst)
34 {
35 return sprintf (dst, NULL); /* { dg-warning "use of NULL where non-null expected" } */
36 }
37
38 int
39 test_uninit_dst (void)
40 {
41 char *dst;
42 return sprintf (dst, "hello world"); /* { dg-warning "use of uninitialized value 'dst'" } */
43 }
44
45 int
46 test_uninit_fmt_ptr (char *dst)
47 {
48 const char *fmt;
49 return sprintf (dst, fmt); /* { dg-warning "use of uninitialized value 'fmt'" } */
50 }
51
52 void
53 test_strlen_1 (void)
54 {
55 char buf[10];
56 sprintf (buf, "msg: %s\n", "abc");
57 __analyzer_eval (__builtin_strlen (buf) == 8); /* { dg-warning "UNKNOWN" } */
58 // TODO: ideally would be TRUE
59 }