1713e07591995d761f65c7132289dce003a29870fAnna Zaks// RUN: %clang_cc1 -analyze -analyzer-checker=core -analyzer-config suppress-inlined-defensive-checks=true -verify %s 2cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks 3cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks// Perform inline defensive checks. 4cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaksvoid idc(int *p) { 5cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks if (p) 6cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks ; 7cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks} 8cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks 9cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaksint test01(int *p) { 10cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks if (p) 11cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks ; 12cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks return *p; // expected-warning {{Dereference of null pointer}} 13cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks} 14cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks 15cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaksint test02(int *p, int *x) { 16cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks if (p) 17cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks ; 18cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks idc(p); 19cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks if (x) 20cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks ; 21cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks return *p; // expected-warning {{Dereference of null pointer}} 22cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks} 23cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks 24cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaksint test03(int *p, int *x) { 25cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks idc(p); 26cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks if (p) 27cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks ; 28cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks return *p; // False negative 29cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks} 30cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks 31cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaksint deref04(int *p) { 32cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks return *p; // expected-warning {{Dereference of null pointer}} 33cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks} 34cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks 35cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaksint test04(int *p) { 36cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks if (p) 37cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks ; 38cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks idc(p); 39cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks return deref04(p); 40cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks} 41cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks 42cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaksint test11(int *q, int *x) { 43cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks int *p = q; 44cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks if (q) 45cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks ; 46cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks if (x) 47cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks ; 48cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks return *p; // expected-warning{{Dereference of null pointer}} 49cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks} 50cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks 51cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaksint test12(int *q) { 52cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks int *p = q; 53cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks idc(q); 54cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks return *p; 55cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks} 56cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks 57cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaksint test13(int *q) { 58cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks int *p = q; 59cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks idc(p); 60cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks return *p; 61cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks} 62cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks 63cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaksint test21(int *q, int *x) { 64cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks if (q) 65cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks ; 66cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks if (x) 67cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks ; 68cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks int *p = q; 69cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks return *p; // expected-warning{{Dereference of null pointer}} 70cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks} 71cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks 72cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaksint test22(int *q, int *x) { 73cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks idc(q); 74cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks if (x) 75cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks ; 76cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks int *p = q; 77cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks return *p; 78cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks} 79cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks 80cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaksint test23(int *q, int *x) { 81cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks idc(q); 82cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks if (x) 83cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks ; 84cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks int *p = q; 85cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks if (!p) 86cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks ; 87cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks return *p; // False negative 88cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks} 89cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks 90cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaksvoid use(char *p) { 91cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks if (!p) 92cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks return; 93cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks p[0] = 'a'; 94cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks} 95cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks 96cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaksvoid test24(char *buffer) { 97cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks use(buffer); 98cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks buffer[1] = 'b'; 99cc5dbdae70c6eb2423921f52a35ba4686d2969cfAnna Zaks} 1008ef064d53fb33b5a8f8743bcbb0a2fd5c3e97be1Anna Zaks 1018ef064d53fb33b5a8f8743bcbb0a2fd5c3e97be1Anna Zaks// Ensure idc works on pointers with constant offset. 1028ef064d53fb33b5a8f8743bcbb0a2fd5c3e97be1Anna Zaksvoid idcchar(const char *s2) { 1038ef064d53fb33b5a8f8743bcbb0a2fd5c3e97be1Anna Zaks if(s2) 1048ef064d53fb33b5a8f8743bcbb0a2fd5c3e97be1Anna Zaks ; 1058ef064d53fb33b5a8f8743bcbb0a2fd5c3e97be1Anna Zaks} 1068ef064d53fb33b5a8f8743bcbb0a2fd5c3e97be1Anna Zaksvoid testConstantOffset(char *value) { 1078ef064d53fb33b5a8f8743bcbb0a2fd5c3e97be1Anna Zaks char *cursor = value + 5; 1088ef064d53fb33b5a8f8743bcbb0a2fd5c3e97be1Anna Zaks idcchar(cursor); 1098ef064d53fb33b5a8f8743bcbb0a2fd5c3e97be1Anna Zaks if (*cursor) { 1108ef064d53fb33b5a8f8743bcbb0a2fd5c3e97be1Anna Zaks cursor++; 1118ef064d53fb33b5a8f8743bcbb0a2fd5c3e97be1Anna Zaks } 1128ef064d53fb33b5a8f8743bcbb0a2fd5c3e97be1Anna Zaks} 1137f79b78351af03a392ee16d8ec557d47746c33c6Anna Zaks 1147f79b78351af03a392ee16d8ec557d47746c33c6Anna Zaks// Ensure idc works for integer zero values (ex: suppressed div by zero). 1157f79b78351af03a392ee16d8ec557d47746c33c6Anna Zaksvoid idcZero(int assume) { 1167f79b78351af03a392ee16d8ec557d47746c33c6Anna Zaks if (assume) 1177f79b78351af03a392ee16d8ec557d47746c33c6Anna Zaks ; 1187f79b78351af03a392ee16d8ec557d47746c33c6Anna Zaks} 1197f79b78351af03a392ee16d8ec557d47746c33c6Anna Zaks 1207f79b78351af03a392ee16d8ec557d47746c33c6Anna Zaksint idcTriggerZeroValue(int m) { 1217f79b78351af03a392ee16d8ec557d47746c33c6Anna Zaks idcZero(m); 1227f79b78351af03a392ee16d8ec557d47746c33c6Anna Zaks return 5/m; // no-warning 1237f79b78351af03a392ee16d8ec557d47746c33c6Anna Zaks} 1247f79b78351af03a392ee16d8ec557d47746c33c6Anna Zaks 1257f79b78351af03a392ee16d8ec557d47746c33c6Anna Zaksint idcTriggerZeroValueThroughCall(int i) { 1267f79b78351af03a392ee16d8ec557d47746c33c6Anna Zaks return 5/i; // no-warning 1277f79b78351af03a392ee16d8ec557d47746c33c6Anna Zaks} 1287f79b78351af03a392ee16d8ec557d47746c33c6Anna Zaksvoid idcTrackZeroValueThroughCall(int x) { 1297f79b78351af03a392ee16d8ec557d47746c33c6Anna Zaks idcZero(x); 1307f79b78351af03a392ee16d8ec557d47746c33c6Anna Zaks idcTriggerZeroValueThroughCall(x); 1317f79b78351af03a392ee16d8ec557d47746c33c6Anna Zaks} 1327f79b78351af03a392ee16d8ec557d47746c33c6Anna Zaks 1337f79b78351af03a392ee16d8ec557d47746c33c6Anna Zaksint idcTriggerZeroThroughDoubleAssignemnt(int i) { 1347f79b78351af03a392ee16d8ec557d47746c33c6Anna Zaks return 5/i; // no-warning 1357f79b78351af03a392ee16d8ec557d47746c33c6Anna Zaks} 1367f79b78351af03a392ee16d8ec557d47746c33c6Anna Zaksvoid idcTrackZeroThroughDoubleAssignemnt(int x) { 1377f79b78351af03a392ee16d8ec557d47746c33c6Anna Zaks idcZero(x); 1387f79b78351af03a392ee16d8ec557d47746c33c6Anna Zaks int y = x; 1397f79b78351af03a392ee16d8ec557d47746c33c6Anna Zaks int z = y; 1407f79b78351af03a392ee16d8ec557d47746c33c6Anna Zaks idcTriggerZeroValueThroughCall(z); 1417f79b78351af03a392ee16d8ec557d47746c33c6Anna Zaks} 142