Skip to content

Commit

Permalink
[analyzer] Fix crashing getSValFromInitListExpr for nested initlists
Browse files Browse the repository at this point in the history
In the following example, we will end up hitting the `llvm_unreachable()`:
https://godbolt.org/z/5sccc95Ec
```lang=C++
enum class E {};
const E glob[] = {{}};
void initlistWithinInitlist() {
  clang_analyzer_dump(glob[0]); // crashes at loading from `glob[0]`
}
```

We should just return `std::nullopt` instead for these cases.
It's better than crashing.

Reviewed By: xazax.hun

Differential Revision: https://reviews.llvm.org/D146538
  • Loading branch information
steakhal committed Mar 22, 2023
1 parent 9bb96fd commit 558b46f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
8 changes: 6 additions & 2 deletions clang/lib/StaticAnalyzer/Core/RegionStore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1849,8 +1849,12 @@ std::optional<SVal> RegionStoreManager::getSValFromInitListExpr(
// Go to the nested initializer list.
ILE = IL;
}
llvm_unreachable(
"Unhandled InitListExpr sub-expressions or invalid offsets.");

assert(ILE);

// FIXME: Unhandeled InitListExpr sub-expression, possibly constructing an
// enum?
return std::nullopt;
}

/// Returns an SVal, if possible, for the specified position in a string
Expand Down
7 changes: 7 additions & 0 deletions clang/test/Analysis/initialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,3 +249,10 @@ void glob_array_parentheses1() {
clang_analyzer_eval(glob_arr9[1][2] == 7); // expected-warning{{TRUE}}
clang_analyzer_eval(glob_arr9[1][3] == 0); // expected-warning{{TRUE}}
}

enum class E {};
const E glob[] = {{}};
void initlistWithinInitlist() {
// no-crash
clang_analyzer_dump(glob[0]); // expected-warning-re {{reg_${{[0-9]+}}<enum E Element{glob,0 S64b,enum E}>}}
}

0 comments on commit 558b46f

Please sign in to comment.