Skip to content

Commit 2a8c12b

Browse files
malavikasamakMalavikaSamak
and
MalavikaSamak
authored
"Reland "[Wunsafe-buffer-usage] Fix false positive when const sized array is indexed by const evaluatable expressions (llvm#119340)"" (llvm#123713)
This reverts commit 7dd34ba. Fixed the assertion violation reported by 7dd34ba Co-authored-by: MalavikaSamak <[email protected]>
1 parent 5658bc4 commit 2a8c12b

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

clang/lib/Analysis/UnsafeBufferUsage.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -453,8 +453,13 @@ AST_MATCHER(ArraySubscriptExpr, isSafeArraySubscript) {
453453
return false;
454454
}
455455

456-
if (const auto *IdxLit = dyn_cast<IntegerLiteral>(Node.getIdx())) {
457-
const APInt ArrIdx = IdxLit->getValue();
456+
Expr::EvalResult EVResult;
457+
const Expr *IndexExpr = Node.getIdx();
458+
if (!IndexExpr->isValueDependent() &&
459+
IndexExpr->EvaluateAsInt(EVResult, Finder->getASTContext())) {
460+
llvm::APSInt ArrIdx = EVResult.Val.getInt();
461+
// FIXME: ArrIdx.isNegative() we could immediately emit an error as that's a
462+
// bug
458463
if (ArrIdx.isNonNegative() && ArrIdx.getLimitedValue() < limit)
459464
return true;
460465
}

clang/test/SemaCXX/warn-unsafe-buffer-usage-array.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,35 @@ char access_strings() {
9292
c = array_string[5];
9393
return c;
9494
}
95+
96+
struct T {
97+
int array[10];
98+
};
99+
100+
const int index = 1;
101+
102+
constexpr int get_const(int x) {
103+
if(x < 3)
104+
return ++x;
105+
else
106+
return x + 5;
107+
};
108+
109+
void array_indexed_const_expr(unsigned idx) {
110+
// expected-note@+2 {{change type of 'arr' to 'std::array' to label it for hardening}}
111+
// expected-warning@+1{{'arr' is an unsafe buffer that does not perform bounds checks}}
112+
int arr[10];
113+
arr[sizeof(int)] = 5;
114+
115+
int array[sizeof(T)];
116+
array[sizeof(int)] = 5;
117+
array[sizeof(T) -1 ] = 3;
118+
119+
int k = arr[6 & 5];
120+
k = arr[2 << index];
121+
k = arr[8 << index]; // expected-note {{used in buffer access here}}
122+
k = arr[16 >> 1];
123+
k = arr[get_const(index)];
124+
k = arr[get_const(5)]; // expected-note {{used in buffer access here}}
125+
k = arr[get_const(4)];
126+
}

0 commit comments

Comments
 (0)