Debian Codegraph @

clang/test/Analysis/exercise-ps.c

raw
1 // RUN: %clang_analyze_cc1 %s -triple=x86_64-unknown-linux \
2 // RUN: -verify -Wno-error=implicit-function-declaration \
3 // RUN: -analyzer-checker=core,unix.Malloc,debug.ExprInspection \
4 // RUN: -analyzer-config core.CallAndMessage:ArgPointeeInitializedness=true
5 //
6 // Just exercise the analyzer on code that has at one point caused issues
7 // (i.e., no assertions or crashes).
8
9 void clang_analyzer_dump_int(int);
10
11 static void f1(const char *x, char *y) {
12 while (*x != 0) {
13 *y++ = *x++;
14 }
15 }
16
17 // This following case checks that we properly handle typedefs when getting
18 // the RvalueType of an ElementRegion.
19 typedef struct F12_struct {} F12_typedef;
20 typedef void* void_typedef;
21 void_typedef f2_helper(void);
22 static void f2(void *buf) {
23 F12_typedef* x;
24 x = f2_helper();
25 memcpy((&x[1]), (buf), 1); // expected-warning{{call to undeclared library function 'memcpy' with type 'void *(void *, const void *}} \
26 // expected-note{{include the header <string.h> or explicitly provide a declaration for 'memcpy'}}
27 }
28
29 // AllocaRegion is untyped. Void pointer isn't of much help either. Before
30 // realizing that the value is undefined, we need to somehow figure out
31 // what type of value do we expect.
32 void f3(void *dest) {
33 void *src = __builtin_alloca(5);
34 memcpy(dest, src, 1); // expected-warning{{2nd function call argument is a pointer to uninitialized value}}
35 }
36
37 // Reproduce crash from GH#94496. When array is used as subcript to another array, CSA cannot model it
38 // and should just assume it's unknown and do not crash.
39 void f4(char *array) {
40 char b[4] = {0};
41
42 _Static_assert(sizeof(int) == 4, "Wrong triple for the test");
43
44 clang_analyzer_dump_int(__builtin_bit_cast(int, b)); // expected-warning {{Unknown}}
45 clang_analyzer_dump_int(array[__builtin_bit_cast(int, b)]); // expected-warning {{Unknown}}
46
47 array[__builtin_bit_cast(int, b)] = 0x10; // no crash
48 }