Skip to content

[analyzer] Improve bug report hashing, merge similar reports #98621

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions clang/lib/StaticAnalyzer/Checkers/ArrayBoundCheckerV2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -374,13 +374,14 @@ static std::optional<int64_t> getConcreteValue(std::optional<NonLoc> SV) {

static Messages getPrecedesMsgs(const SubRegion *Region, NonLoc Offset) {
std::string RegName = getRegionName(Region);
SmallString<128> Buf;
llvm::raw_svector_ostream Out(Buf);
Out << "Access of " << RegName << " at negative byte offset";
if (auto ConcreteIdx = Offset.getAs<nonloc::ConcreteInt>())
Out << ' ' << ConcreteIdx->getValue();

// We're not reporting the Offset, because we don't want to spam the user
// with similar reports that differ only in different offset values.
// See https://github.com/llvm/llvm-project/issues/86969 for details.
(void)Offset;

return {formatv("Out of bound access to memory preceding {0}", RegName),
std::string(Buf)};
formatv("Access of {0} at negative byte offset", RegName)};
}

/// Try to divide `Val1` and `Val2` (in place) by `Divisor` and return true if
Expand Down Expand Up @@ -609,7 +610,7 @@ void ArrayBoundCheckerV2::performCheck(const Expr *E, CheckerContext &C) const {
// CHECK UPPER BOUND
DefinedOrUnknownSVal Size = getDynamicExtent(State, Reg, SVB);
if (auto KnownSize = Size.getAs<NonLoc>()) {
// In a situation where both overflow and overflow are possible (but the
// In a situation where both underflow and overflow are possible (but the
// index is either tainted or known to be invalid), the logic of this
// checker will first assume that the offset is non-negative, and then
// (with this additional assumption) it will detect an overflow error.
Expand Down
26 changes: 24 additions & 2 deletions clang/test/Analysis/out-of-bounds-diagnostics.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,37 @@ int TenElements[10];
void arrayUnderflow(void) {
TenElements[-3] = 5;
// expected-warning@-1 {{Out of bound access to memory preceding 'TenElements'}}
// expected-note@-2 {{Access of 'TenElements' at negative byte offset -12}}
// expected-note@-2 {{Access of 'TenElements' at negative byte offset}}
}

int underflowWithDeref(void) {
int *p = TenElements;
--p;
return *p;
// expected-warning@-1 {{Out of bound access to memory preceding 'TenElements'}}
// expected-note@-2 {{Access of 'TenElements' at negative byte offset -4}}
// expected-note@-2 {{Access of 'TenElements' at negative byte offset}}
}

int rng(void);
int getIndex(void) {
switch (rng()) {
case 1: return -152;
case 2: return -160;
case 3: return -168;
default: return -172;
}
}

void gh86959(void) {
// Previously code like this produced many almost-identical bug reports that
// only differed in the byte offset value (which was reported by the checker
// at that time). Verify that now we only see one report.

// expected-note@+1 {{Entering loop body}}
while (rng())
TenElements[getIndex()] = 10;
// expected-warning@-1 {{Out of bound access to memory preceding 'TenElements'}}
// expected-note@-2 {{Access of 'TenElements' at negative byte offset}}
}

int scanf(const char *restrict fmt, ...);
Expand Down
Loading