[clang] [Wunsafe-buffer-usage] False positives for & expression indexing constant size array (arr[anything & 0]) (PR #112284)

Ziqing Luo via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 29 17:19:25 PDT 2024


================
@@ -420,6 +420,118 @@ AST_MATCHER(CXXConstructExpr, isSafeSpanTwoParamConstruct) {
   return false;
 }
 
+class MaxValueEval : public RecursiveASTVisitor<MaxValueEval> {
+
+  std::vector<llvm::APInt> val;
+  ASTContext &Context;
+  llvm::APInt Max;
+  unsigned bit_width;
+
+public:
+  typedef RecursiveASTVisitor<MaxValueEval> VisitorBase;
+
+  explicit MaxValueEval(ASTContext &Ctx, const Expr *exp) : Context(Ctx) {
+    bit_width = Ctx.getIntWidth(exp->getType());
+    Max = llvm::APInt::getSignedMaxValue(bit_width);
----------------
ziqingluo-90 wrote:

Also types of those bitwise operands matter.  For example, `((unsigned long long) -1) & ((unsigned) var)` is of type `unsigned long long` but the value is actually upper-bounded by the max value of `unsigned`.  (https://godbolt.org/z/b1zd1zEje)

Handle this in next version sounds good me.

https://github.com/llvm/llvm-project/pull/112284


More information about the cfe-commits mailing list