https://github.com/ilovepi updated https://github.com/llvm/llvm-project/pull/131939
>From d7fe558c1c7a3f18bc4019e6edcd86e2a98a46a0 Mon Sep 17 00:00:00 2001 From: Paul Kirth <paulki...@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..50556ecd2635b --- /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" +} +] _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits