84 lines · cpp
1// RUN: %clang_analyze_cc1 -x c++ -analyzer-checker=core -analyzer-output=text -verify %s2 3typedef unsigned char uint8_t;4 5#define UINT8_MAX 2556#define TCP_MAXWIN 655357 8uint8_t get_uint8_max() {9 uint8_t rcvscale = UINT8_MAX; // expected-note{{'rcvscale' initialized to 255}}10 return rcvscale; // expected-note{{Returning the value 255 (loaded from 'rcvscale')}}11}12 13void shift_by_undefined_value() {14 uint8_t shift_amount = get_uint8_max(); // expected-note{{'shift_amount' initialized to 255}}15 // expected-note@-1{{Calling 'get_uint8_max'}}16 // expected-note@-2{{Returning from 'get_uint8_max'}}17 (void)(TCP_MAXWIN << shift_amount); // expected-warning{{Left shift by '255' overflows the capacity of 'int'}}18 // expected-note@-1{{The result of left shift is undefined because the right operand '255' is not smaller than 32, the capacity of 'int'}}19}20 21namespace array_index_tracking {22void consume(int);23 24int getIndex(int x) {25 int a;26 if (x > 0) // expected-note {{Assuming 'x' is > 0}}27 // expected-note@-1 {{Taking true branch}}28 a = 3; // expected-note {{The value 3 is assigned to 'a'}}29 else30 a = 2;31 return a; // expected-note {{Returning the value 3 (loaded from 'a')}}32}33 34int getInt();35 36void testArrayIndexTracking() {37 int arr[10];38 39 for (int i = 0; i < 3; ++i)40 // expected-note@-1 3{{Loop condition is true. Entering loop body}}41 // expected-note@-2 {{Loop condition is false. Execution continues on line 43}}42 arr[i] = 0;43 int x = getInt();44 int n = getIndex(x); // expected-note {{Calling 'getIndex'}}45 // expected-note@-1 {{Returning from 'getIndex'}}46 // expected-note@-2 {{'n' initialized to 3}}47 consume(arr[n]);48 // expected-note@-1 {{1st function call argument is an uninitialized value}}49 // expected-warning@-2{{1st function call argument is an uninitialized value}}50}51} // end of namespace array_index_tracking52 53namespace multi_array_index_tracking {54void consume(int);55 56int getIndex(int x) {57 int a;58 if (x > 0) // expected-note {{Assuming 'x' is > 0}}59 // expected-note@-1 {{Taking true branch}}60 a = 3; // expected-note {{The value 3 is assigned to 'a'}}61 else62 a = 2;63 return a; // expected-note {{Returning the value 3 (loaded from 'a')}}64}65 66int getInt();67 68void testArrayIndexTracking() {69 int arr[2][10];70 71 for (int i = 0; i < 3; ++i)72 // expected-note@-1 3{{Loop condition is true. Entering loop body}}73 // expected-note@-2 {{Loop condition is false. Execution continues on line 75}}74 arr[1][i] = 0;75 int x = getInt();76 int n = getIndex(x); // expected-note {{Calling 'getIndex'}}77 // expected-note@-1 {{Returning from 'getIndex'}}78 // expected-note@-2 {{'n' initialized to 3}}79 consume(arr[1][n]);80 // expected-note@-1 {{1st function call argument is an uninitialized value}}81 // expected-warning@-2{{1st function call argument is an uninitialized value}}82}83} // end of namespace mulit_array_index_tracking84