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