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:
Jamie Schmeiser 2022-06-03 10:10:37 -04:00
parent 5c902af572
commit efbf0136b4
3 changed files with 24 additions and 2 deletions

View File

@ -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.

View File

@ -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

View File

@ -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