| 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 |
/* C only: C++ fpermissive already emits errors. */ |
| 5 |
#include "analyzer-decls.h" |
| 6 |
|
| 7 |
extern int |
| 8 |
sprintf(char* dst, const char* fmt, ...) |
| 9 |
__attribute__((__nothrow__)); |
| 10 |
|
| 11 |
|
| 12 |
#define NULL ((void *)0) |
| 13 |
|
| 14 |
int |
| 15 |
test_uninit_fmt_buf (char *dst) |
| 16 |
{ |
| 17 |
const char fmt[10]; |
| 18 |
return sprintf (dst, fmt); /* { dg-warning "use of uninitialized value 'fmt\\\[0\\\]'" } */ |
| 19 |
/* { dg-message "while looking for null terminator for argument 2 \\('&fmt'\\) of 'sprintf'..." "event" { target *-*-* } .-1 } */ |
| 20 |
} |
| 21 |
|
| 22 |
int |
| 23 |
test_fmt_not_terminated (char *dst) |
| 24 |
{ |
| 25 |
const char fmt[3] = "foo"; |
| 26 |
return sprintf (dst, fmt); /* { dg-warning "stack-based buffer over-read" } */ |
| 27 |
/* { dg-message "while looking for null terminator for argument 2 \\('&fmt'\\) of 'sprintf'..." "event" { target *-*-* } .-1 } */ |
| 28 |
} |