Skip to content

Commit

Permalink
Revert "Produce warning for performing pointer arithmetic on a null p…
Browse files Browse the repository at this point in the history
…ointer."

This reverts commit dfc1e31.
See discussion on https://reviews.llvm.org/D98798
  • Loading branch information
nico committed May 12, 2021
1 parent 2117613 commit d8c227b
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 22 deletions.
12 changes: 1 addition & 11 deletions clang/lib/Sema/SemaExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10779,17 +10779,7 @@ QualType Sema::CheckSubtractionOperands(ExprResult &LHS, ExprResult &RHS,
LHS.get(), RHS.get()))
return QualType();

bool LHSIsNullPtr = LHS.get()->IgnoreParenCasts()->isNullPointerConstant(
Context, Expr::NPC_ValueDependentIsNotNull);
bool RHSIsNullPtr = RHS.get()->IgnoreParenCasts()->isNullPointerConstant(
Context, Expr::NPC_ValueDependentIsNotNull);

// Subtracting nullptr or from nullptr should produce
// a warning expect nullptr - nullptr is valid in C++ [expr.add]p7
if (LHSIsNullPtr && (!getLangOpts().CPlusPlus || !RHSIsNullPtr))
diagnoseArithmeticOnNullPointer(*this, Loc, LHS.get(), false);
if (RHSIsNullPtr && (!getLangOpts().CPlusPlus || !LHSIsNullPtr))
diagnoseArithmeticOnNullPointer(*this, Loc, RHS.get(), false);
// FIXME: Add warnings for nullptr - ptr.

// The pointee type may have zero size. As an extension, a structure or
// union may have zero size or an array may have zero length. In this
Expand Down
3 changes: 0 additions & 3 deletions clang/test/Sema/pointer-addition.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,4 @@ void a(S* b, void* c) {
// Cases that don't match the GNU inttoptr idiom get a different warning.
f = (char*)0 - i; // expected-warning {{performing pointer arithmetic on a null pointer has undefined behavior}}
int *g = (int*)0 + i; // expected-warning {{performing pointer arithmetic on a null pointer has undefined behavior}}
f = (char*)((char*)0 - f); // expected-warning {{performing pointer arithmetic on a null pointer has undefined behavior}}
f = (char*)(f - (char*)0); // expected-warning {{performing pointer arithmetic on a null pointer has undefined behavior}}
f = (char*)((char*)0 - (char*)0); // expected-warning {{performing pointer arithmetic on a null pointer has undefined behavior}} expected-warning {{performing pointer arithmetic on a null pointer has undefined behavior}}
}
8 changes: 0 additions & 8 deletions clang/test/Sema/pointer-addition.cpp

This file was deleted.

0 comments on commit d8c227b

Please sign in to comment.