[clang-tools-extra] [clang-doc] Avoid deref of invalid std::optional (PR #131939)
Paul Kirth via cfe-commits
cfe-commits at lists.llvm.org
Thu Mar 20 14:03:23 PDT 2025
https://github.com/ilovepi updated https://github.com/llvm/llvm-project/pull/131939
>From b1653a1167f9923d1266d2971773d585251109a1 Mon Sep 17 00:00:00 2001
From: Paul Kirth <paulkirth at google.com>
Date: Tue, 18 Mar 2025 23:38:09 +0000
Subject: [PATCH] [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
---
clang-tools-extra/clang-doc/HTMLGenerator.cpp | 2 +-
.../test/clang-doc/DR-131697.cpp | 22 +++++++++++++++++++
2 files changed, 23 insertions(+), 1 deletion(-)
create mode 100644 clang-tools-extra/test/clang-doc/DR-131697.cpp
diff --git a/clang-tools-extra/clang-doc/HTMLGenerator.cpp b/clang-tools-extra/clang-doc/HTMLGenerator.cpp
index 18a0de826630c..a8404479569f9 100644
--- a/clang-tools-extra/clang-doc/HTMLGenerator.cpp
+++ b/clang-tools-extra/clang-doc/HTMLGenerator.cpp
@@ -498,7 +498,7 @@ writeFileDefinition(const Location &L,
return std::make_unique<TagNode>(
HTMLTag::TAG_P, "Defined at line " + std::to_string(L.LineNumber) +
" of file " + L.Filename);
- SmallString<128> FileURL(*RepositoryUrl);
+ SmallString<128> FileURL(RepositoryUrl.value_or(""));
llvm::sys::path::append(
FileURL, llvm::sys::path::Style::posix,
// If we're on Windows, the file name will be in the wrong format, and
diff --git a/clang-tools-extra/test/clang-doc/DR-131697.cpp b/clang-tools-extra/test/clang-doc/DR-131697.cpp
new file mode 100644
index 0000000000000..67bd55b897973
--- /dev/null
+++ b/clang-tools-extra/test/clang-doc/DR-131697.cpp
@@ -0,0 +1,22 @@
+// RUN: rm -rf %t && mkdir -p %t
+// RUN: split-file %s %t
+// RUN: clang-doc -format=html %t/compile_commands.json %t/main.cpp
+
+//--- main.cpp
+
+class Foo {
+ void getFoo();
+};
+
+int main() {
+ return 0;
+}
+
+//--- compile_commands.json
+[
+{
+ "directory": "foo",
+ "file":"main.cpp",
+ "command":"clang main.cpp -c"
+}
+]
More information about the cfe-commits
mailing list