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

DonĂ¡t Nagy via cfe-commits cfe-commits at lists.llvm.org
Thu May 30 11:30:22 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}}
----------------
NagyDonat wrote:

> Wrt. `&x - 1`, that should be valid as well, given that the resulting pointer is not dereferenced.

The standard explicitly disallows this, see [[expr.add] part 4.2](http://eel.is/c++draft/expr.add#4.2) and 4.3 (in the most recent C++ draft standard, other versions may be different).

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


More information about the cfe-commits mailing list