Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 20, 2025

Conversation

ilovepi
Copy link
Contributor

@ilovepi ilovepi commented Mar 18, 2025

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

Copy link
Member

@evelez7 evelez7 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixes the crashes I was experiencing.

@llvmbot
Copy link
Member

llvmbot commented Mar 19, 2025

@llvm/pr-subscribers-clang-tools-extra

Author: Paul Kirth (ilovepi)

Changes

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


Full diff: https://github.com/llvm/llvm-project/pull/131939.diff

2 Files Affected:

  • (modified) clang-tools-extra/clang-doc/HTMLGenerator.cpp (+1-1)
  • (added) clang-tools-extra/test/clang-doc/DR-131697.cpp (+22)
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"
+}
+]

@ilovepi ilovepi force-pushed the users/ilovepi/clang-doc-md-test branch from 03e1275 to 5a6bdbc Compare March 19, 2025 15:25
@ilovepi ilovepi force-pushed the users/ilovepi/clang-doc-fix-optional branch 2 times, most recently from 2de1ac2 to ea33858 Compare March 19, 2025 21:05
Base automatically changed from users/ilovepi/clang-doc-md-test to main March 19, 2025 23:14
@ilovepi ilovepi force-pushed the users/ilovepi/clang-doc-fix-optional branch 4 times, most recently from bc2b1ec to 215f9a3 Compare March 20, 2025 00:55
Copy link
Contributor Author

ilovepi commented Mar 20, 2025

Merge activity

  • Mar 20, 5:02 PM EDT: A user started a stack merge that includes this pull request via Graphite.
  • Mar 20, 5:03 PM EDT: Graphite rebased this pull request as part of a merge.
  • Mar 20, 5:04 PM EDT: A user merged this pull request with Graphite.

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
@ilovepi ilovepi force-pushed the users/ilovepi/clang-doc-fix-optional branch from 215f9a3 to b1653a1 Compare March 20, 2025 21:03
@ilovepi ilovepi merged commit a87a64b into main Mar 20, 2025
6 of 10 checks passed
@ilovepi ilovepi deleted the users/ilovepi/clang-doc-fix-optional branch March 20, 2025 21:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[clang-doc] crashes when generating HTML without --repository flag
4 participants