[clang] [WIP][Wunsafe-buffer-usage] False positives for & expression indexing constant size array (arr[anything & 0]) (PR #112284)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Oct 15 14:17:22 PDT 2024
github-actions[bot] wrote:
<!--LLVM CODE FORMAT COMMENT: {clang-format}-->
:warning: C/C++ code formatter, clang-format found issues in your code. :warning:
<details>
<summary>
You can test this locally with the following command:
</summary>
``````````bash
git-clang-format --diff 2ff4c25b7efff64b3b662d0bedcfe7edebcf20b9 81af812176768eb663a09b5ccabe3c7211119b76 --extensions cpp -- clang/lib/Analysis/UnsafeBufferUsage.cpp clang/test/SemaCXX/warn-unsafe-buffer-usage-array.cpp
``````````
</details>
<details>
<summary>
View the diff from clang-format here.
</summary>
``````````diff
diff --git a/clang/lib/Analysis/UnsafeBufferUsage.cpp b/clang/lib/Analysis/UnsafeBufferUsage.cpp
index f80076ee90..017f7f48fe 100644
--- a/clang/lib/Analysis/UnsafeBufferUsage.cpp
+++ b/clang/lib/Analysis/UnsafeBufferUsage.cpp
@@ -431,14 +431,16 @@ AST_MATCHER(ArraySubscriptExpr, isSafeArraySubscript) {
SafeMaskedAccess;
unsigned int RecLimit = 5;
- SafeMaskedAccess = [&](const Expr *exp, unsigned int RecLimit) -> llvm::APInt {
- llvm::APInt Max = llvm::APInt::getMaxValue(Finder->getASTContext().getIntWidth(exp->getType()));
+ SafeMaskedAccess = [&](const Expr *exp,
+ unsigned int RecLimit) -> llvm::APInt {
+ llvm::APInt Max = llvm::APInt::getMaxValue(
+ Finder->getASTContext().getIntWidth(exp->getType()));
Max.setAllBits();
if (RecLimit == 0)
return Max;
- //RecLimit--;
+ // RecLimit--;
if (const auto *IntLit = dyn_cast<IntegerLiteral>(exp)) {
const APInt ArrIdx = IntLit->getValue();
@@ -446,24 +448,26 @@ AST_MATCHER(ArraySubscriptExpr, isSafeArraySubscript) {
}
if (const auto *BinEx = dyn_cast<BinaryOperator>(exp)) {
- llvm::APInt LHS = SafeMaskedAccess(BinEx->getLHS()->IgnoreParenCasts(), RecLimit);
- llvm::APInt RHS = SafeMaskedAccess(BinEx->getRHS()->IgnoreParenCasts(), RecLimit);
-
- switch(BinEx->getOpcode()) {
- case BO_And:
- LHS = LHS & RHS.getLimitedValue();
- return LHS;
- case BO_Or:
- LHS = LHS | RHS.getLimitedValue();
- return LHS;
- case BO_Shl:
- LHS = LHS << RHS.getLimitedValue();
- return LHS;
- case BO_Xor:
- LHS = LHS ^ RHS.getLimitedValue();
- return LHS;
- default:
- return Max;
+ llvm::APInt LHS =
+ SafeMaskedAccess(BinEx->getLHS()->IgnoreParenCasts(), RecLimit);
+ llvm::APInt RHS =
+ SafeMaskedAccess(BinEx->getRHS()->IgnoreParenCasts(), RecLimit);
+
+ switch (BinEx->getOpcode()) {
+ case BO_And:
+ LHS = LHS & RHS.getLimitedValue();
+ return LHS;
+ case BO_Or:
+ LHS = LHS | RHS.getLimitedValue();
+ return LHS;
+ case BO_Shl:
+ LHS = LHS << RHS.getLimitedValue();
+ return LHS;
+ case BO_Xor:
+ LHS = LHS ^ RHS.getLimitedValue();
+ return LHS;
+ default:
+ return Max;
}
}
@@ -490,7 +494,8 @@ AST_MATCHER(ArraySubscriptExpr, isSafeArraySubscript) {
return true;
} else if (const auto *BinEx = dyn_cast<BinaryOperator>(Node.getIdx())) {
APInt result = SafeMaskedAccess(Node.getIdx(), RecLimit);
- if (result.isNonNegative() && result.getLimitedValue() < CATy->getLimitedSize())
+ if (result.isNonNegative() &&
+ result.getLimitedValue() < CATy->getLimitedSize())
return true;
}
@@ -1198,7 +1203,7 @@ public:
Handler.handleUnsafeOperation(ASE, IsRelatedToDecl, Ctx);
}
SourceLocation getSourceLoc() const override { return ASE->getBeginLoc(); }
-
+
DeclUseList getClaimedVarUseSites() const override {
if (const auto *DRE =
dyn_cast<DeclRefExpr>(ASE->getBase()->IgnoreParenImpCasts())) {
``````````
</details>
https://github.com/llvm/llvm-project/pull/112284
More information about the cfe-commits
mailing list