1 // RUN: %clang_analyze_cc1 -verify %s \
2 // RUN: -analyzer-checker=alpha.unix.cstring.BufferOverlap
4 // RUN: %clang_analyze_cc1 -verify %s -DUSE_BUILTINS \
5 // RUN: -analyzer-checker=alpha.unix.cstring.BufferOverlap
7 // RUN: %clang_analyze_cc1 -verify %s -DVARIANT \
8 // RUN: -analyzer-checker=alpha.unix.cstring.BufferOverlap
10 // RUN: %clang_analyze_cc1 -verify %s -DVARIANT -DUSE_BUILTINS \
11 // RUN: -analyzer-checker=alpha.unix.cstring.BufferOverlap
13 // This provides us with four possible sprintf() definitions.
16 #define BUILTIN(f) __builtin_##f
17 #else /* USE_BUILTINS */
19 #endif /* USE_BUILTINS */
21 typedef typeof(sizeof(int)) size_t;
25 #define __sprintf_chk BUILTIN(__sprintf_chk)
26 #define __snprintf_chk BUILTIN(__snprintf_chk)
27 int __sprintf_chk (char * __restrict str
, int flag
, size_t os
,
28 const char * __restrict fmt
, ...);
29 int __snprintf_chk (char * __restrict str
, size_t len
, int flag
, size_t os
,
30 const char * __restrict fmt
, ...);
32 #define sprintf(str, ...) __sprintf_chk(str, 0, __builtin_object_size(str, 0), __VA_ARGS__)
33 #define snprintf(str, len, ...) __snprintf_chk(str, len, 0, __builtin_object_size(str, 0), __VA_ARGS__)
37 #define sprintf BUILTIN(sprintf)
38 int sprintf(char *restrict buffer
, const char *restrict format
, ... );
39 int snprintf(char *restrict buffer
, size_t bufsz
,
40 const char *restrict format
, ... );
43 void test_sprintf1() {
45 sprintf(a
, "%d/%s", 1, a
); // expected-warning{{Arguments must not be overlapping buffers}}
48 void test_sprintf2() {
50 sprintf(a
, "%s", a
); // expected-warning{{Arguments must not be overlapping buffers}}
53 void test_sprintf3() {
55 sprintf(a
, "%s/%s", a
, a
); // expected-warning{{Arguments must not be overlapping buffers}}
58 void test_sprintf4() {
60 sprintf(a
, "%d", 42); // no-warning
63 void test_sprintf5() {
66 sprintf(a
, "%s", b
); // no-warning
69 void test_snprintf1() {
71 snprintf(a
, sizeof(a
), "%d/%s", 1, a
); // expected-warning{{Arguments must not be overlapping buffers}}
74 void test_snprintf2() {
76 snprintf(a
+1, sizeof(a
)-1, "%d/%s", 1, a
); // expected-warning{{Arguments must not be overlapping buffers}}
79 void test_snprintf3() {
81 snprintf(a
, sizeof(a
), "%s", a
); // expected-warning{{Arguments must not be overlapping buffers}}
84 void test_snprintf4() {
86 snprintf(a
, sizeof(a
), "%s/%s", a
, a
); // expected-warning{{Arguments must not be overlapping buffers}}
89 void test_snprintf5() {
91 snprintf(a
, sizeof(a
), "%d", 42); // no-warning
94 void test_snprintf6() {
97 snprintf(a
, sizeof(a
), "%s", b
); // no-warning