-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[-Wunsafe-buffer-usage] Fix false positives for constant cases #92432
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -420,25 +420,63 @@ AST_MATCHER(ArraySubscriptExpr, isSafeArraySubscript) { | |
// already duplicated | ||
// - call both from Sema and from here | ||
|
||
const auto *BaseDRE = | ||
dyn_cast<DeclRefExpr>(Node.getBase()->IgnoreParenImpCasts()); | ||
if (!BaseDRE) | ||
if (const auto *BaseDRE = | ||
dyn_cast<DeclRefExpr>(Node.getBase()->IgnoreParenImpCasts())) { | ||
if (!BaseDRE->getDecl()) | ||
return false; | ||
if (const auto *CATy = Finder->getASTContext().getAsConstantArrayType( | ||
BaseDRE->getDecl()->getType())) { | ||
if (const auto *IdxLit = dyn_cast<IntegerLiteral>(Node.getIdx())) { | ||
const APInt ArrIdx = IdxLit->getValue(); | ||
// FIXME: ArrIdx.isNegative() we could immediately emit an error as | ||
// that's a bug | ||
if (ArrIdx.isNonNegative() && | ||
ArrIdx.getLimitedValue() < CATy->getLimitedSize()) | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
if (const auto *BaseSL = | ||
dyn_cast<StringLiteral>(Node.getBase()->IgnoreParenImpCasts())) { | ||
if (const auto *CATy = | ||
Finder->getASTContext().getAsConstantArrayType(BaseSL->getType())) { | ||
if (const auto *IdxLit = dyn_cast<IntegerLiteral>(Node.getIdx())) { | ||
const APInt ArrIdx = IdxLit->getValue(); | ||
// FIXME: ArrIdx.isNegative() we could immediately emit an error as | ||
// that's a bug | ||
if (ArrIdx.isNonNegative() && | ||
ArrIdx.getLimitedValue() < CATy->getLimitedSize()) | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
AST_MATCHER(BinaryOperator, isSafePtrArithmetic) { | ||
if (Node.getOpcode() != BinaryOperatorKind::BO_Add) | ||
return false; | ||
if (!BaseDRE->getDecl()) | ||
|
||
const auto *LHSDRE = dyn_cast<DeclRefExpr>(Node.getLHS()->IgnoreImpCasts()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess we might as well support the static const char *const abc = "abc";
abc[3];
abc + 3; But also arguably not urgent. |
||
if (!LHSDRE) | ||
return false; | ||
|
||
const auto *CATy = Finder->getASTContext().getAsConstantArrayType( | ||
BaseDRE->getDecl()->getType()); | ||
LHSDRE->getDecl()->getType()); | ||
if (!CATy) | ||
return false; | ||
|
||
if (const auto *IdxLit = dyn_cast<IntegerLiteral>(Node.getIdx())) { | ||
const APInt ArrIdx = IdxLit->getValue(); | ||
// FIXME: ArrIdx.isNegative() we could immediately emit an error as that's a | ||
// bug | ||
if (ArrIdx.isNonNegative() && | ||
ArrIdx.getLimitedValue() < CATy->getLimitedSize()) | ||
return true; | ||
} | ||
const auto *RHSIntLit = dyn_cast<IntegerLiteral>(Node.getRHS()); | ||
if (!RHSIntLit) | ||
return false; | ||
|
||
const APInt BufferOffset = RHSIntLit->getValue(); | ||
|
||
if (BufferOffset.isNonNegative() && | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't speak AST, but does this still handle (i.e. not warn on) cases like What about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. True, this is not covered - just like more complex arithmetic expressions with are not yet covered with fixits. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That seems reasonable, but if that's the route, I might want to file a bug pre-emptively on further cases that should, or should not, be considered for future work. One of my thoughts with the refactor suggestion below was that if the arithmetic/detection got more complex, it would be easier to implement consistently in a single place. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code is just a short-term solution until we get to do it properly. I don't want to invest effort into polishing the code because we should replace it. See this FIXME: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Feel free to file issues but at this point I think it's not a good use of our time because only the very trivial case gets recognized. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's this whole But it still won't handle the case where there's nested layers of arithmetic over the base pointer, like |
||
BufferOffset.getLimitedValue() < CATy->getLimitedSize()) | ||
return true; | ||
|
||
return false; | ||
} | ||
|
@@ -692,7 +730,8 @@ class PointerArithmeticGadget : public WarningGadget { | |
hasLHS(expr(hasPointerType()).bind(PointerArithmeticPointerTag)), | ||
hasRHS(HasIntegerType)); | ||
|
||
return stmt(binaryOperator(anyOf(PtrAtLeft, PtrAtRight)) | ||
return stmt(binaryOperator(anyOf(PtrAtLeft, PtrAtRight), | ||
unless(isSafePtrArithmetic())) | ||
.bind(PointerArithmeticTag)); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// RUN: %clang_cc1 -std=c++20 -Wno-everything -Wunsafe-buffer-usage \ | ||
// RUN: -fsafe-buffer-usage-suggestions \ | ||
// RUN: -verify %s | ||
|
||
void char_literal() { | ||
if ("abc"[2] == 'c') | ||
return; | ||
if ("def"[3] == '0') | ||
return; | ||
} | ||
|
||
void const_size_buffer_arithmetic() { | ||
char kBuf[64] = {}; | ||
const char* p = kBuf + 1; | ||
} | ||
|
||
// expected-no-diagnostics | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extreme nitpicking: I usually put those on top of the file because otherwise I'd be super confused where all those expected warnings are at. We also really don't want it to end up in the middle of the file if folks add more code at the bottom without reading the comment. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It feels like this block is repeated, more or less, several times. Is there a way to factor it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True but the block is also trivial and I am not convinced factoring it out as a separate function is worth it or leads to better code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking more a lambda. I don't know LLVM's coding conventions very well, though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it's worth it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suspect that this could have been a:
Which would also eliminate duplication. (Unless
DeclRefExpr->getDecl()->getType()
is somehow significantly different fromDeclRefExpr->getType()
.)Another thing we can try is to simply eliminate the
isa
entirely. Like, we know that it's "an" expression, and its type is a constant-size array type. Do we really need more? Why not simply trust the type system? (It's not like the C type system has ever lied to us right? 😅)