50 lines · c
1// RUN: %clang_analyze_cc1 -analyzer-checker=security.PointerSub -analyzer-output=text -verify %s2 3void different_1() {4 int a[3]; // expected-note{{Array at the left-hand side of subtraction}}5 int b[3]; // expected-note{{Array at the right-hand side of subtraction}}6 int d = &a[2] - &b[0]; // expected-warning{{Subtraction of two pointers that do not point into the same array is undefined behavior}} \7 // expected-note{{Subtraction of two pointers that do not point into the same array is undefined behavior}}8}9 10void different_2() {11 int a[3]; // expected-note{{Array at the right-hand side of subtraction}}12 int b[3]; // expected-note{{Array at the left-hand side of subtraction}}13 int *p1 = a + 1;14 int *p2 = b;15 int d = p2 - p1; // expected-warning{{Subtraction of two pointers that do not point into the same array is undefined behavior}} \16 // expected-note{{Subtraction of two pointers that do not point into the same array is undefined behavior}}17}18 19int different_3() {20 struct {21 int array[5];22 } a, b;23 return &a.array[3] - &b.array[2]; // expected-warning{{Subtraction of two pointers that do not point into the same array is undefined behavior}} \24 // expected-note{{Subtraction of two pointers that do not point into the same array is undefined behavior}}25}26 27int different_4() {28 struct {29 int array1[5]; // expected-note{{Array at the left-hand side of subtraction}}30 int array2[5]; // expected-note{{Array at the right-hand side of subtraction}}31 } a;32 return &a.array1[3] - &a.array2[4]; // expected-warning{{Subtraction of two pointers that do not point into the same array is undefined behavior}} \33 // expected-note{{Subtraction of two pointers that do not point into the same array is undefined behavior}}34}35 36void different_5() {37 int d;38 static int x[10][10]; // expected-note2{{Array at the left-hand side of subtraction}}39 int *y1 = &(x[3][5]);40 char *z = ((char *) y1) + 2;41 int *y2 = (int *)(z - 2);42 int *y3 = ((int *)x) + 35; // This is offset for [3][5].43 44 d = y2 - y1; // expected-warning{{Subtraction of two pointers that do not point into the same array is undefined behavior}} \45 // expected-note{{Subtraction of two pointers that do not point into the same array is undefined behavior}}46 d = y3 - y1; // expected-warning{{Subtraction of two pointers that do not point into the same array is undefined behavior}} \47 // expected-note{{Subtraction of two pointers that do not point into the same array is undefined behavior}}48 d = y3 - y2;49}50