Skip to content

Commit bc2b1ec

Browse files
committed
[clang-doc] Avoid deref of invalid std::optional
Since our existing guard is insufficient to prevent accessing the std::optional when in an invalid state, guard the access with `.value_or()`. This maintains the desired behavior, without running into UB. The new test should prevent regressions. Fixes #131697
1 parent be9b7a1 commit bc2b1ec

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

clang-tools-extra/clang-doc/HTMLGenerator.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ writeFileDefinition(const Location &L,
498498
return std::make_unique<TagNode>(
499499
HTMLTag::TAG_P, "Defined at line " + std::to_string(L.LineNumber) +
500500
" of file " + L.Filename);
501-
SmallString<128> FileURL(*RepositoryUrl);
501+
SmallString<128> FileURL(RepositoryUrl.value_or(""));
502502
llvm::sys::path::append(
503503
FileURL, llvm::sys::path::Style::posix,
504504
// If we're on Windows, the file name will be in the wrong format, and
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// RUN: rm -rf %t && mkdir -p %t
2+
// RUN: split-file %s %t
3+
// RUN: clang-doc -format=html %t/compile-commands.json %t/main.cpp
4+
5+
//--- main.cpp
6+
7+
class Foo {
8+
void getFoo();
9+
};
10+
11+
int main() {
12+
return 0;
13+
}
14+
15+
//--- compile-commands.json
16+
[
17+
{
18+
"directory": "foo",
19+
"file":"main.cpp",
20+
"command":"clang main.cpp -c"
21+
}
22+
]

0 commit comments

Comments
 (0)