[clang] efbf013 - Only issue warning for subtraction involving null pointers on live code paths
Jamie Schmeiser via cfe-commits
cfe-commits at lists.llvm.org
Fri Jun 3 07:11:55 PDT 2022
Author: Jamie Schmeiser
Date: 2022-06-03T10:10:37-04:00
New Revision: efbf0136b4108692ddd1a852b3f5b232c10d2097
URL: https://github.com/llvm/llvm-project/commit/efbf0136b4108692ddd1a852b3f5b232c10d2097
DIFF: https://github.com/llvm/llvm-project/commit/efbf0136b4108692ddd1a852b3f5b232c10d2097.diff
LOG: Only issue warning for subtraction involving null pointers on live code paths
Summary:
Change the warning produced for subtraction from (or with) a null pointer
to only be produced when the code path is live.
https://github.com/llvm/llvm-project/issues/54570
Author: Jamie Schmeiser <schmeise at ca.ibm.com>
Reviewed By: anarazel (Andres Freund)
Differential Revision: https://reviews.llvm.org/D126816
Added:
Modified:
clang/lib/Sema/SemaExpr.cpp
clang/test/Sema/pointer-subtraction.c
clang/test/Sema/pointer-subtraction.cpp
Removed:
################################################################################
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 3e8bd63e89ae9..6627aaf3e6d88 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -10845,8 +10845,10 @@ static void diagnoseSubtractionOnNullPointer(Sema &S, SourceLocation Loc,
if (S.Diags.getSuppressSystemWarnings() && S.SourceMgr.isInSystemMacro(Loc))
return;
- S.Diag(Loc, diag::warn_pointer_sub_null_ptr)
- << S.getLangOpts().CPlusPlus << Pointer->getSourceRange();
+ S.DiagRuntimeBehavior(Loc, Pointer,
+ S.PDiag(diag::warn_pointer_sub_null_ptr)
+ << S.getLangOpts().CPlusPlus
+ << Pointer->getSourceRange());
}
/// Diagnose invalid arithmetic on two function pointers.
diff --git a/clang/test/Sema/pointer-subtraction.c b/clang/test/Sema/pointer-subtraction.c
index c3dbbd47459fa..79d18a0f0a306 100644
--- a/clang/test/Sema/pointer-subtraction.c
+++ b/clang/test/Sema/pointer-subtraction.c
@@ -11,6 +11,16 @@ void a(void) {
f = (char *)(f - (char *)0); // expected-warning {{performing pointer subtraction with a null pointer has undefined behavior}}
f = (char *)((char *)0 - (char *)0); // expected-warning {{performing pointer subtraction with a null pointer has undefined behavior}} expected-warning {{performing pointer subtraction with a null pointer has undefined behavior}}
+ if (1)
+ f = (char *)((char *)0 - f); // expected-warning {{performing pointer subtraction with a null pointer has undefined behavior}}
+ else
+ f = (char *)((char *)0 - f);
+
+ if (0)
+ f = (char *)((char *)0 - f);
+ else
+ f = (char *)((char *)0 - f); // expected-warning {{performing pointer subtraction with a null pointer has undefined behavior}}
+
#ifndef SYSTEM_WARNINGS
SYSTEM_MACRO(f);
#else
diff --git a/clang/test/Sema/pointer-subtraction.cpp b/clang/test/Sema/pointer-subtraction.cpp
index efbb3255e5d31..28063a1fea6ef 100644
--- a/clang/test/Sema/pointer-subtraction.cpp
+++ b/clang/test/Sema/pointer-subtraction.cpp
@@ -11,6 +11,16 @@ void a() {
f = (char *)(f - (char *)0); // expected-warning {{performing pointer subtraction with a null pointer may have undefined behavior}}
f = (char *)((char *)0 - (char *)0); // valid in C++
+ if (1)
+ f = (char *)((char *)0 - f); // expected-warning {{performing pointer subtraction with a null pointer may have undefined behavior}}
+ else
+ f = (char *)((char *)0 - f);
+
+ if (0)
+ f = (char *)((char *)0 - f);
+ else
+ f = (char *)((char *)0 - f); // expected-warning {{performing pointer subtraction with a null pointer may have undefined behavior}}
+
#ifndef SYSTEM_WARNINGS
SYSTEM_MACRO(f);
#else
More information about the cfe-commits
mailing list