Skip to content

[clang][analyzer] PointerSubChecker should not warn on pointers converted to numerical type #111846

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 11, 2024

Conversation

balazske
Copy link
Collaborator

Pointer values casted to integer (non-pointer) type should be able to be subtracted as usual.

…rted to numerical value

Pointer values casted to integer (non-pointer) type should be able to be
subtracted as usual.
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:static analyzer labels Oct 10, 2024
@llvmbot
Copy link
Member

llvmbot commented Oct 10, 2024

@llvm/pr-subscribers-clang

@llvm/pr-subscribers-clang-static-analyzer-1

Author: Balázs Kéri (balazske)

Changes

Pointer values casted to integer (non-pointer) type should be able to be subtracted as usual.


Full diff: https://github.com/llvm/llvm-project/pull/111846.diff

2 Files Affected:

  • (modified) clang/lib/StaticAnalyzer/Checkers/PointerSubChecker.cpp (+4)
  • (modified) clang/test/Analysis/pointer-sub.c (+7)
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) {

@NagyDonat NagyDonat changed the title [clang][analyzer] PointerSubChecker should not warn on pointers converted to numerical value [clang][analyzer] PointerSubChecker should not warn on pointers converted to numerical type Oct 11, 2024
Copy link
Contributor

@NagyDonat NagyDonat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change LGTM, I think we can merge it.

My only nitpick was that I tweaked the PR title to make it a bit more accurate. (Feel free to tweak it further / discuss this if you disagree with my choice.)

@karka228
Copy link
Collaborator

The change LGTM, I think we can merge it.

I guess the testcase pointer-sub.c have to be fixed to work on the windows buildbot, also. Right?

@NagyDonat
Copy link
Contributor

The change LGTM, I think we can merge it.

I guess the testcase pointer-sub.c have to be fixed to work on the windows buildbot, also. Right?

Right, thanks for catching my mistake!

@balazske balazske merged commit f74f568 into llvm:main Oct 11, 2024
8 checks passed
DanielCChen pushed a commit to DanielCChen/llvm-project that referenced this pull request Oct 16, 2024
…rted to numerical type (llvm#111846)

Pointer values casted to integer (non-pointer) type should be able to be
subtracted as usual.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:static analyzer clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants