[clang] [clang][analyzer] PointerSubChecker should not warn on pointers converted to numerical value (PR #111846)
Balázs Kéri via cfe-commits
cfe-commits at lists.llvm.org
Thu Oct 10 07:49:05 PDT 2024
https://github.com/balazske created https://github.com/llvm/llvm-project/pull/111846
Pointer values casted to integer (non-pointer) type should be able to be subtracted as usual.
>From a9e1790691e01892f7e1b17523cd43421445f3ba Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bal=C3=A1zs=20K=C3=A9ri?= <balazs.keri at ericsson.com>
Date: Thu, 10 Oct 2024 16:28:50 +0200
Subject: [PATCH] [clang][analyzer] PointerSubChecker should not warn on
pointers converted to numerical value
Pointer values casted to integer (non-pointer) type should be able to be
subtracted as usual.
---
clang/lib/StaticAnalyzer/Checkers/PointerSubChecker.cpp | 4 ++++
clang/test/Analysis/pointer-sub.c | 7 +++++++
2 files changed, 11 insertions(+)
diff --git a/clang/lib/StaticAnalyzer/Checkers/PointerSubChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/PointerSubChecker.cpp
index f0dc5efd75f7d6..7a85d9e2073068 100644
--- a/clang/lib/StaticAnalyzer/Checkers/PointerSubChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/PointerSubChecker.cpp
@@ -61,6 +61,10 @@ void PointerSubChecker::checkPreStmt(const BinaryOperator *B,
if (LR->getSymbolicBase() || RR->getSymbolicBase())
return;
+ if (!B->getLHS()->getType()->isPointerType() ||
+ !B->getRHS()->getType()->isPointerType())
+ return;
+
const auto *ElemLR = dyn_cast<ElementRegion>(LR);
const auto *ElemRR = dyn_cast<ElementRegion>(RR);
diff --git a/clang/test/Analysis/pointer-sub.c b/clang/test/Analysis/pointer-sub.c
index 1c9d676ebb8f24..7a1dcb653a28c4 100644
--- a/clang/test/Analysis/pointer-sub.c
+++ b/clang/test/Analysis/pointer-sub.c
@@ -10,6 +10,9 @@ void f1(void) {
d = &x - (&x + 1); // no-warning
d = (&x + 0) - &x; // no-warning
d = (z + 10) - z; // no-warning
+ d = (unsigned long)&y - (unsigned long)&x; // no-warning
+ unsigned long l = 1;
+ d = l - (unsigned long)&y; // no-warning
}
void f2(void) {
@@ -28,6 +31,10 @@ void f2(void) {
d = (int *)((char *)(&a[4]) + sizeof(int)) - &a[4]; // no-warning (pointers into the same array data)
d = (int *)((char *)(&a[4]) + 1) - &a[4]; // expected-warning{{Subtraction of two pointers that}}
+
+ long a1 = (long)&a[1];
+ long b1 = (long)&b[1];
+ d = a1 - b1;
}
void f3(void) {
More information about the cfe-commits
mailing list