Repository navigation
backtrace truncated if msg contains null character \0 #28761
Description
Activity
- addedconfirmed-bugIssues and PRs for confirmed bugs.Issues and PRs for confirmed bugs.errorsIssues and PRs related to JavaScript errors originating in Node.js core.Issues and PRs related to JavaScript errors originating in Node.js core.
on Jul 19, 2019 I guess the question is:
- Do we want to escape special characters in err.message?
- If so, do we want to just special case for
\0, or include any other special characters? (off the top of my head, we would not want to escape\n), or make that configuration somehow?
I’d just print the error-message as-is, but including the
\0character.BTW chrome prints this by ignoring
\0.Do we want to special case for BOM or invalid surrogate characters as well? I guess whatever we do we should try to be consistent.
@joyeecheung That seems like a much more widely scoped issue than this bug for me … the problem here is, afaict, we convert the error information from JS strings to UTF-8 like we always do, but then use
snprintfandfprintfto prepare/print out the bytes that we actually print, and those functions special-case\0as the end-of-string. It’s just that we’re not keeping track of the string length while doing so…We can think about escaping characters or introducing special handling for some, but that seems more like a feature request and isn’t about correct vs incorrect behaviour?
we should preserve the '\0' without escaping, it makes it easier to debug in case user tries to see why there is a null char (in my case it was from here: nim-lang/Nim#11788)
also, that's what console.log does: it preserves the
'\0'but then use snprintf and fprintf to prepare/print out the bytes
solution: use
fwriteinstead ofsnprintf
https://stackoverflow.com/questions/6943928/show-special-characters-in-unix-while-using-less-commandIf you know the length of your string as n characters, you can output it using fwrite: if (n && fwrite(str, 1, n, stdout) != n) { /* Error handling. */ }preserve the '\0' without escaping
To preserve it in the output we need to escape it (replace it with
\\0).it makes it easier to debug in case user tries to see why there is a null char
Doesn't that also apply to
\n,\rand\t?There are three handling that I can think of:
- Escape
\0inerr.message(while not escaping\n,\rand friends) - Ignore
\0inerr.message- what browsers andconsole.logdo - Make that configurable? (along with
\n,\rand friends)
Reacted by Anna Henningsen- Escape
preserve the '\0' without escaping
To preserve it in the output we need to escape it (replace it with
\\0).@joyeecheung Can you explain why we need to escape it? Why is it not enough to just print the
\0character as-is, even if most terminals won’t display it (basically, like @timotheecour said, act likeconsole.log()here)?@addaleax By
print the \0 character as-isdo you mean we pass the original string with\0intosnprintf/fprintf? I think that is why they are truncated?@joyeecheung Yes, that’s why they are currently truncated as well – What I’m having in mind is that we shouldn’t use the standard C
snprintf/fprintffunctions for these strings, and instead use maybe a more C++-y equivalent ofsnprintfthat can deal with e.g.std::strings, and for writing to stderr something likefwriteas suggested by @timotheecour.@addaleax I see, I thought the request was to print
new Error('test\0test')as literallytest\0test, sorry about the confusion!In that case I think we just need to stop doing variadic arguments in
PrintErrorStringas that's the source of the C-style interpretation of the stringsmaybe a more C++-y equivalent of snprintf that can deal with e.g. std::strings, and for writing to stderr something like fwrite as suggested by @timotheecour.
unless you're using any format string arguments
fwriteshould be enough (but maybe I'm missing something?)4 remaining items
@himself65 Yeah, it’s quite a bit of work but I’m up for it – it’s not hard, it’s pretty straightforward, just a lot to do.
Reacted by Alex Yang- added a commit that references this issue
on Jan 21, 2020 - added a commit that references this issue
on Jan 23, 2020 - added a commit that references this issue
on Jul 27, 2026

backtrace truncated if msg contains null character
\0with this program
without the
\0the backtrace shows fine:node -v
v12.3.1
uname -a
Darwin 18.5.0 Darwin Kernel Version 18.5.0
note
according to https://stackoverflow.com/questions/13698677/null-character-in-strings
console.log correctly shows the string (no truncation) but the backtrace doesn't work when running node on cmd line.
note that on a browser (eg chrome) it works: no truncation: