49 lines · c
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=true5//6// Just exercise the analyzer on code that has at one point caused issues7// (i.e., no assertions or crashes).8 9void clang_analyzer_dump_int(int);10 11static 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 getting18// the RvalueType of an ElementRegion.19typedef struct F12_struct {} F12_typedef;20typedef void* void_typedef;21void_typedef f2_helper(void);22static 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. Before30// realizing that the value is undefined, we need to somehow figure out31// what type of value do we expect.32void 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 it38// and should just assume it's unknown and do not crash.39void 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 crash48}49