[clang] [clang][analyzer] Improved PointerSubChecker (PR #93676)

Balazs Benics via cfe-commits cfe-commits at lists.llvm.org
Thu May 30 02:42:58 PDT 2024


================
@@ -0,0 +1,74 @@
+// RUN: %clang_analyze_cc1 -analyzer-checker=alpha.core.PointerSub -verify %s
+
+void f1(void) {
+  int x, y, z[10];
+  int d = &y - &x; // expected-warning{{Subtraction of two pointers that do not point into the same array is undefined behavior}}
+  d = z - &y; // expected-warning{{Subtraction of two pointers that do not point into the same array is undefined behavior}}
+  d = &x - &x; // expected-warning{{Subtraction of two pointers that do not point into the same array is undefined behavior}}
----------------
steakhal wrote:

The tests look good, except for `&x - &x`, which should be valid.
Wrt. `&x - 1`, that should be valid as well, given that the resulting pointer is not dereferenced. However, clang's constrant interpreter disagrees with gcc's on that case: https://godbolt.org/z/dMd1rMazY
However, like I said, I believe gcc's right by accepting that code.


https://github.com/llvm/llvm-project/pull/93676


More information about the cfe-commits mailing list