[clang] [Wunsafe-buffer-usage] False positives for & expression indexing constant size array (arr[anything & 0]) (PR #112284)
Malavika Samak via cfe-commits
cfe-commits at lists.llvm.org
Mon Oct 28 16:40:39 PDT 2024
================
@@ -427,6 +427,48 @@ AST_MATCHER(ArraySubscriptExpr, isSafeArraySubscript) {
// - e. g. "Try harder to find a NamedDecl to point at in the note."
// already duplicated
// - call both from Sema and from here
+ std::function<llvm::APInt(const Expr *exp, unsigned int limit)>
+ 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()));
+ Max.setAllBits();
+
+ if (RecLimit == 0)
+ return Max;
+
+ //RecLimit--;
+
+ if (const auto *IntLit = dyn_cast<IntegerLiteral>(exp)) {
+ const APInt ArrIdx = IntLit->getValue();
+ return ArrIdx;
+ }
+
+ if (const auto *BinEx = dyn_cast<BinaryOperator>(exp)) {
+ llvm::APInt LHS = SafeMaskedAccess(BinEx->getLHS()->IgnoreParenCasts(), RecLimit);
+ llvm::APInt RHS = SafeMaskedAccess(BinEx->getRHS()->IgnoreParenCasts(), RecLimit);
----------------
malavikasamak wrote:
The code has now re-organized with ASTVisitors. I don't think casts should pose a major challenges anymore, but please let me know if you think otherwise. Also have added more type cast tests to cover all bases.
https://github.com/llvm/llvm-project/pull/112284
More information about the cfe-commits
mailing list