Skip to content

Commit 1172ed5

Browse files
steakhaltru
authored andcommitted
[analyzer] Fix crashing getSValFromInitListExpr for nested initlists
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 (cherry picked from commit 558b46f)
1 parent 830229e commit 1172ed5

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

clang/lib/StaticAnalyzer/Core/RegionStore.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1849,8 +1849,12 @@ std::optional<SVal> RegionStoreManager::getSValFromInitListExpr(
18491849
// Go to the nested initializer list.
18501850
ILE = IL;
18511851
}
1852-
llvm_unreachable(
1853-
"Unhandled InitListExpr sub-expressions or invalid offsets.");
1852+
1853+
assert(ILE);
1854+
1855+
// FIXME: Unhandeled InitListExpr sub-expression, possibly constructing an
1856+
// enum?
1857+
return std::nullopt;
18541858
}
18551859

18561860
/// Returns an SVal, if possible, for the specified position in a string

clang/test/Analysis/initialization.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,3 +249,10 @@ void glob_array_parentheses1() {
249249
clang_analyzer_eval(glob_arr9[1][2] == 7); // expected-warning{{TRUE}}
250250
clang_analyzer_eval(glob_arr9[1][3] == 0); // expected-warning{{TRUE}}
251251
}
252+
253+
enum class E {};
254+
const E glob[] = {{}};
255+
void initlistWithinInitlist() {
256+
// no-crash
257+
clang_analyzer_dump(glob[0]); // expected-warning-re {{reg_${{[0-9]+}}<enum E Element{glob,0 S64b,enum E}>}}
258+
}

0 commit comments

Comments
 (0)