Skip to content

Commit

Permalink
Revert "[Wunsafe-buffer-usage] Fix false positive when const sized ar…
Browse files Browse the repository at this point in the history
…ray is indexed by const evaluatable expressions (llvm#119340)"

This reverts commit 64c2156.
Causes asserts, see
llvm#119340 (comment)
  • Loading branch information
nico committed Jan 16, 2025
1 parent b1cef93 commit 7dd34ba
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 37 deletions.
7 changes: 2 additions & 5 deletions clang/lib/Analysis/UnsafeBufferUsage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -453,11 +453,8 @@ AST_MATCHER(ArraySubscriptExpr, isSafeArraySubscript) {
return false;
}

Expr::EvalResult EVResult;
if (Node.getIdx()->EvaluateAsInt(EVResult, Finder->getASTContext())) {
llvm::APSInt ArrIdx = EVResult.Val.getInt();
// FIXME: ArrIdx.isNegative() we could immediately emit an error as that's a
// bug
if (const auto *IdxLit = dyn_cast<IntegerLiteral>(Node.getIdx())) {
const APInt ArrIdx = IdxLit->getValue();
if (ArrIdx.isNonNegative() && ArrIdx.getLimitedValue() < limit)
return true;
}
Expand Down
32 changes: 0 additions & 32 deletions clang/test/SemaCXX/warn-unsafe-buffer-usage-array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,35 +92,3 @@ char access_strings() {
c = array_string[5];
return c;
}

struct T {
int array[10];
};

const int index = 1;

constexpr int get_const(int x) {
if(x < 3)
return ++x;
else
return x + 5;
};

void array_indexed_const_expr(unsigned idx) {
// expected-note@+2 {{change type of 'arr' to 'std::array' to label it for hardening}}
// expected-warning@+1{{'arr' is an unsafe buffer that does not perform bounds checks}}
int arr[10];
arr[sizeof(int)] = 5;

int array[sizeof(T)];
array[sizeof(int)] = 5;
array[sizeof(T) -1 ] = 3;

int k = arr[6 & 5];
k = arr[2 << index];
k = arr[8 << index]; // expected-note {{used in buffer access here}}
k = arr[16 >> 1];
k = arr[get_const(index)];
k = arr[get_const(5)]; // expected-note {{used in buffer access here}}
k = arr[get_const(4)];
}

0 comments on commit 7dd34ba

Please sign in to comment.