[clang] [clang][analyzer] Improved PointerSubChecker (PR #93676)
DonĂ¡t Nagy via cfe-commits
cfe-commits at lists.llvm.org
Wed May 29 20:16:08 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:
This corner case is explicitly allowed by the standard: non-array variables act as if they were single-element arrays and it's valid to do (trivial) pointer arithmetic on them.
Even calculating the past-the-end pointer of a non-array object (e.g. `&x + 1`) is a valid (but unusual) operation; however the [description on cppreference](https://en.cppreference.com/w/cpp/language/operator_arithmetic) suggests that it's UB to take the difference `(&x+1)-&x`. You should probably look this up in the standard itself if you want to be accurate.
https://github.com/llvm/llvm-project/pull/93676
More information about the cfe-commits
mailing list