[PATCH] D128207: [clang-doc][NFC] Fix reference invalidation assertion failure.

liushuai wang via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Jun 20 07:28:57 PDT 2022


MTC created this revision.
MTC added reviewers: dexonsmith, mehdi_amini.
MTC added a project: clang-tools-extra.
Herald added subscribers: jsji, usaxena95, pengfei, kadircet, arphaman.
Herald added a project: All.
MTC requested review of this revision.
Herald added subscribers: cfe-commits, ilya-biryukov.

Fix the assertion failure caused by reference invalidation in `SmallString`. It seems that this bug was not caught when adding the assertion, see https://reviews.llvm.org/D91744.

We can reproduce this bug trivially through the following usage.

  $ clang-doc  --format=html test.cpp
  Emiting docs in html format.
  clang-doc: llvm-project/llvm/include/llvm/ADT/SmallVector.h:173: void llvm::SmallVectorTemplateCommon<T, <template-parameter-1-2> >::assertSafeToReferenceAfterResize(const void*, size_t) [with T = char; <template-parameter-1-2> = void; size_t = long unsigned int]: Assertion `isSafeToReferenceAfterResize(Elt, NewSize) && "Attempting to reference an element of the vector in an operation " "that invalidates it"' failed.
   #0 0x000055ce4806b843 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) llvm-project/llvm/lib/Support/Unix/Signals.inc:569:22
   #1 0x000055ce4806b8fa PrintStackTraceSignalHandler(void*) llvm-project/llvm/lib/Support/Unix/Signals.inc:636:1
   #2 0x000055ce4806989a llvm::sys::RunSignalHandlers() llvm-project/llvm/lib/Support/Signals.cpp:103:20
   #3 0x000055ce4806b277 SignalHandler(int) llvm-project/llvm/lib/Support/Unix/Signals.inc:407:1
   #4 0x00007f72a4533730 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x12730)
   #5 0x00007f72a3e1a7bb raise /build/glibc-fWwxX8/glibc-2.28/signal/../sysdeps/unix/sysv/linux/raise.c:51:1
   #6 0x00007f72a3e05535 abort /build/glibc-fWwxX8/glibc-2.28/stdlib/abort.c:81:7
   #7 0x00007f72a3e0540f _nl_load_domain /build/glibc-fWwxX8/glibc-2.28/intl/loadmsgcat.c:1177:9
   #8 0x00007f72a3e13102 (/lib/x86_64-linux-gnu/libc.so.6+0x30102)
   #9 0x000055ce47f76f1a llvm::SmallVectorTemplateCommon<char, void>::assertSafeToReferenceAfterResize(void const*, unsigned long) llvm-project/llvm/include/llvm/ADT/SmallVector.h:171:5
  #10 0x000055ce47f79efc llvm::SmallVectorTemplateCommon<char, void>::assertSafeToReferenceAfterClear(char const*, char const*) llvm-project/llvm/include/llvm/ADT/SmallVector.h:187:47
  #11 0x000055ce47f743ed void llvm::SmallVectorImpl<char>::assign<char const*, void>(char const*, char const*) llvm-project/llvm/include/llvm/ADT/SmallVector.h:713:5
  #12 0x000055ce47f6c587 llvm::SmallString<128u>::assign(llvm::StringRef) llvm-project/llvm/include/llvm/ADT/SmallString.h:53:3
  #13 0x000055ce47f64338 llvm::SmallString<128u>::operator=(llvm::StringRef) llvm-project/llvm/include/llvm/ADT/SmallString.h:279:13
  #14 0x000055ce47f4ab4c main llvm-project/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp:226:28
  #15 0x00007f72a3e0709b __libc_start_main /build/glibc-fWwxX8/glibc-2.28/csu/../csu/libc-start.c:342:3
  #16 0x000055ce47f4963a _start (./bin/clang-doc+0x54563a)
  [1]    1443196 abort (core dumped)  ./bin/clang-doc --format=html test.cpp


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D128207

Files:
  clang-tools-extra/clang-doc/tool/ClangDocMain.cpp


Index: clang-tools-extra/clang-doc/tool/ClangDocMain.cpp
===================================================================
--- clang-tools-extra/clang-doc/tool/ClangDocMain.cpp
+++ clang-tools-extra/clang-doc/tool/ClangDocMain.cpp
@@ -222,7 +222,7 @@
     std::string ClangDocPath = GetExecutablePath(argv[0], MainAddr);
     llvm::SmallString<128> AssetsPath;
     llvm::sys::path::native(ClangDocPath, AssetsPath);
-    AssetsPath = llvm::sys::path::parent_path(AssetsPath);
+    AssetsPath = std::string(llvm::sys::path::parent_path(AssetsPath));
     llvm::sys::path::append(AssetsPath, "..", "share", "clang");
     llvm::SmallString<128> DefaultStylesheet;
     llvm::sys::path::native(AssetsPath, DefaultStylesheet);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D128207.438384.patch
Type: text/x-patch
Size: 726 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220620/07620b2e/attachment.bin>


More information about the cfe-commits mailing list