Skip to content
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

[analyzer] Fix crash on using bitcast(<type>, <array>) as array subscript #101647

Merged
merged 4 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
14 changes: 13 additions & 1 deletion clang/lib/StaticAnalyzer/Core/Store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,19 @@ SVal StoreManager::getLValueElement(QualType elementType, NonLoc Offset,
const auto *ElemR = dyn_cast<ElementRegion>(BaseRegion);

// Convert the offset to the appropriate size and signedness.
Offset = svalBuilder.convertToArrayIndex(Offset).castAs<NonLoc>();
auto Off = svalBuilder.convertToArrayIndex(Offset).getAs<NonLoc>();
steakhal marked this conversation as resolved.
Show resolved Hide resolved
if (!Off) {
// Handle cases when LazyCompoundVal is used for an array index.
// Such case is possible if code does:
//
// char b[4];
// a[__builtin_bitcast(int, b)];
//
pskrgag marked this conversation as resolved.
Show resolved Hide resolved
// Return UnknownVal, since we cannot model it.
return UnknownVal();
}

Offset = Off.value();

if (!ElemR) {
// If the base region is not an ElementRegion, create one.
Expand Down
7 changes: 7 additions & 0 deletions clang/test/Analysis/exercise-ps.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,10 @@ void f3(void *dest) {
void *src = __builtin_alloca(5);
memcpy(dest, src, 1); // expected-warning{{2nd function call argument is a pointer to uninitialized value}}
}

// Reproduce crash from GH#94496. When array is used as subcript to another array, CSA cannot model it
// and should just assume it's unknown and do not crash.
void f4(char *array) {
char b[4] = {0};
array[__builtin_bit_cast(int, b)] = 0x10; // no crash
pskrgag marked this conversation as resolved.
Show resolved Hide resolved
}
Loading