forked from OSchip/llvm-project
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@ca.ibm.com> Reviewed By: anarazel (Andres Freund) Differential Revision: https://reviews.llvm.org/D126816
This commit is contained in:
parent
5c902af572
commit
efbf0136b4
|
@ -10845,8 +10845,10 @@ static void diagnoseSubtractionOnNullPointer(Sema &S, SourceLocation Loc,
|
||||||
if (S.Diags.getSuppressSystemWarnings() && S.SourceMgr.isInSystemMacro(Loc))
|
if (S.Diags.getSuppressSystemWarnings() && S.SourceMgr.isInSystemMacro(Loc))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
S.Diag(Loc, diag::warn_pointer_sub_null_ptr)
|
S.DiagRuntimeBehavior(Loc, Pointer,
|
||||||
<< S.getLangOpts().CPlusPlus << Pointer->getSourceRange();
|
S.PDiag(diag::warn_pointer_sub_null_ptr)
|
||||||
|
<< S.getLangOpts().CPlusPlus
|
||||||
|
<< Pointer->getSourceRange());
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Diagnose invalid arithmetic on two function pointers.
|
/// Diagnose invalid arithmetic on two function pointers.
|
||||||
|
|
|
@ -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 *)(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}}
|
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
|
#ifndef SYSTEM_WARNINGS
|
||||||
SYSTEM_MACRO(f);
|
SYSTEM_MACRO(f);
|
||||||
#else
|
#else
|
||||||
|
|
|
@ -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 *)(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++
|
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
|
#ifndef SYSTEM_WARNINGS
|
||||||
SYSTEM_MACRO(f);
|
SYSTEM_MACRO(f);
|
||||||
#else
|
#else
|
||||||
|
|
Loading…
Reference in New Issue