<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/81273>81273</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            PrettyStackTraceFormat prints a null character after the formatted string
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          jonmeow
      </td>
    </tr>
</table>

<pre>
    When crashing, PrettyStackTraceFormat prints a null character at the end of the formatted output. The implementation is at https://github.com/llvm/llvm-project/blob/main/llvm/lib/Support/PrettyStackTrace.cpp#L238-L254

The key issue is with:

```
  const int Size = SizeOrError + 1; // '\0'
  Str.resize(Size);
```

which is printed in whole:

```
void PrettyStackTraceFormat::print(raw_ostream &OS) const { OS << Str << "\n"; }
```

The `+ 1` allocates space for the null character, and since `Str` is [llvm::SmallVector<char, 32> Str;](https://github.com/llvm/llvm-project/blob/main/llvm/include/llvm/Support/PrettyStackTrace.h#L83), every character -- including the null character -- is printed.

Sample program:

```
#include "llvm/Support/PrettyStackTrace.h"

auto main() -> int {
  auto msg = llvm::PrettyStackTraceFormat("x");
 llvm::EnablePrettyStackTrace();
  assert(false);
}
```

Output using `cat -v` to highlight the null character:

```
$ ./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^@
```

The `^@` represents the null character, and should've been elided when printing.

I proposed one option for a fix in #77351. An alternative might be to replace uses (e.g., format at the call site and switch to PrettyStackTraceString); I only see four: https://github.com/search?q=repo%3Allvm%2Fllvm-project%20PrettyStackTraceFormat&type=code
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysVt1u2zwSfRr6ZhBDJq2_C1_4JwYKFEgBF7uXBU2NJbYUqSUpu-nTL4ZyGidove3iAwJ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