1 // RUN: %clang_analyze_cc1 -x c++ -analyzer-checker=core -analyzer-output=text -verify %s 2 3 typedef unsigned char uint8_t; 4 5 #define UINT8_MAX 255 6 #define TCP_MAXWIN 65535 7 8 uint8_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 13 void 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{{The result of the left shift is undefined due to shifting by '255', which is greater or equal to the width of type 'int'}} 18 // expected-note@-1{{The result of the left shift is undefined due to shifting by '255', which is greater or equal to the width of type 'int'}} 19 } 20 21 namespace array_index_tracking { 22 void consume(int); 23 24 int 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 else 30 a = 2; 31 return a; // expected-note {{Returning the value 3 (loaded from 'a')}} 32 } 33 34 int getInt(); 35 36 void 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_tracking 52 53 namespace multi_array_index_tracking { 54 void consume(int); 55 56 int 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 else 62 a = 2; 63 return a; // expected-note {{Returning the value 3 (loaded from 'a')}} 64 } 65 66 int getInt(); 67 68 void 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_tracking 84