================
@@ -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
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to