[clang-tools-extra] a87a64b - [clang-doc] Avoid deref of invalid std::optional (#131939)

via cfe-commits cfe-commits at lists.llvm.org
Thu Mar 20 14:05:01 PDT 2025


Author: Paul Kirth
Date: 2025-03-20T14:04:58-07:00
New Revision: a87a64b2e487995f9de90a614c7caa7a888147df

URL: https://github.com/llvm/llvm-project/commit/a87a64b2e487995f9de90a614c7caa7a888147df
DIFF: https://github.com/llvm/llvm-project/commit/a87a64b2e487995f9de90a614c7caa7a888147df.diff

LOG: [clang-doc] Avoid deref of invalid std::optional (#131939)

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

Added: 
    clang-tools-extra/test/clang-doc/DR-131697.cpp

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

Removed: 
    


################################################################################
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