[llvm] [Support] Trim trailing null character in PrettyStackTraceFormat. (PR #77351)
Jon Ross-Perkins via llvm-commits
llvm-commits at lists.llvm.org
Mon Jan 8 10:37:03 PST 2024
https://github.com/jonmeow created https://github.com/llvm/llvm-project/pull/77351
Space for the `\0` is added on line 247, but the value is not trimmed. `SmallVector` prints the entire contents, including the `\0`. To fix, pop the back value to remove the `\0`.
Sample program, and `cat -v` can be used to show the null (as `^@`):
```
#include "llvm/Support/PrettyStackTrace.h"
auto main() -> int {
auto msg = llvm::PrettyStackTraceFormat("x");
llvm::EnablePrettyStackTrace();
assert(false);
}
```
Before:
```
$ ./crash_test |& cat -v
crash_test: toolchain/driver/crash_test.cpp:6: int main(): Assertion `false' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0. x^@
```
After:
```
$ ./crash_test |& cat -v
crash_test: toolchain/driver/crash_test.cpp:6: int main(): Assertion `false' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0. x
```
>From c090ffd9d62a8e17a757c807d010d2e0d7907e16 Mon Sep 17 00:00:00 2001
From: jonmeow <jperkins at google.com>
Date: Mon, 8 Jan 2024 10:26:09 -0800
Subject: [PATCH] [Support] Trim trailing null character in
PrettyStackTraceFormat.
---
llvm/lib/Support/PrettyStackTrace.cpp | 2 ++
1 file changed, 2 insertions(+)
diff --git a/llvm/lib/Support/PrettyStackTrace.cpp b/llvm/lib/Support/PrettyStackTrace.cpp
index f9f1b8a419b820..fc3e08e3662217 100644
--- a/llvm/lib/Support/PrettyStackTrace.cpp
+++ b/llvm/lib/Support/PrettyStackTrace.cpp
@@ -249,6 +249,8 @@ PrettyStackTraceFormat::PrettyStackTraceFormat(const char *Format, ...) {
va_start(AP, Format);
vsnprintf(Str.data(), Size, Format, AP);
va_end(AP);
+ // Remove the '\0' for printing.
+ Str.pop_back();
}
void PrettyStackTraceFormat::print(raw_ostream &OS) const { OS << Str << "\n"; }
More information about the llvm-commits
mailing list