Skip to content

Fix crash logging non-UTF-8 narrow strings - #545

Merged
lo-simon merged 2 commits into
sony:masterfrom
garethsb:bugfix/non-utf8-log-messages
Nov 19, 2026
Merged

lo-simon merged 2 commits into
sony:masterfrom
garethsb:bugfix/non-utf8-log-messages

Conversation

@garethsb

Copy link
Copy Markdown
Contributor

This replaces #523 by @marcusfreisleben. It keeps his commit unchanged and adds one commit with the changes suggested in the review there.

Problem

On Windows, utility::s2us throws std::range_error when a narrow string is not valid UTF-8, for example an exception message in the ANSI code page. When that happens while converting a log message, the exception escapes the async logging thread and terminates the process. On Linux and macOS, s2us passes the bytes through, so the Logging API returns invalid UTF-8.

Changes

Marcus's commit adds utility::s2us_lenient, uses it for log messages, source locations and exception messages in API error responses, and catches any exception in log_gate::service. See #523 for the details.

The second commit changes how s2us_lenient handles a string that is not valid UTF-8:

  • On Windows, the string is still decoded using the ANSI code page, unless that code page is UTF-8.
  • Otherwise, each invalid byte is replaced with U+FFFD, and the valid UTF-8 around it is unchanged. Previously, the whole string was decoded as Latin-1, so one bad byte turned every multi-byte character in the string into mojibake. The decoder is boost::locale::utf, which is header-only and available in Boost 1.54 and later.
  • On Windows, the string is decoded once, rather than once to validate it and again to convert it.

It also:

  • converts log categories with s2us again, since they are constants defined in code
  • omits error_description from WWW-Authenticate when the message contains characters that RFC 6749 section 5.2 does not allow; the JSON error body still carries the message

Testing

Built and tested on Linux. The new tests in basic_utils_test.cpp cover valid UTF-8 around an invalid byte, a truncated sequence, and, on Windows with code page 1252, 0xFC decoding to U+00FC. The Windows code paths have not been run yet, apart from what CI covers.

Co-authored-by: Gareth's 🤖 buddy

marcusfreisleben and others added 2 commits November 18, 2026 10:54
utility::s2us assumes UTF-8, throws std::range_error otherwise. Log messages
carry arbitrary narrow text, e.g. std::exception::what(), with no such
guarantee - on Windows typically the ANSI code page. Throw lands on the async
logging worker thread, where slog's message_service::run catches only
stopped_exception, so it escapes the thread function and terminates the process.

Add utility::s2us_lenient: UTF-8 fast path, Windows ANSI code page fallback,
Latin-1 otherwise. Never throws, never discards a message. s2us contract
unchanged.

Validation uses conversions::to_utf16string, the same decoder s2us uses on a
wide build, so the fast path accepts exactly what s2us accepts. Note that
decoder is not a strict UTF-8 validator - it tolerates surrogates, overlongs
and code points above U+10FFFF - so a hand-rolled check would have diverged.
Converts s rather than the decoded value, so the result is identical to s2us
wherever s2us succeeded, on narrow builds too.

Skips the ANSI code page when it is itself UTF-8 (Windows 10 and later), where
it would repeat the failed decode and substitute U+FFFD, losing the characters
at issue.

Applied in json_from_message (message, file, function, categories) and at the
what() boundaries in the API error responses.

Guard log_gate::service as a last resort, so no logging failure can terminate
the application.

The invalid bytes also reached the JSON on narrow builds, i.e. the Logging API
emitted invalid UTF-8; testInsertLogEventNonUtf8 covers that on all platforms
and fails against the unfixed code.

Built and tested on darwin only. The ANSI code page branch is untested.
When a narrow string is not valid UTF-8 and the Windows ANSI code page
does not apply, s2us_lenient now decodes it as UTF-8 and replaces each
invalid byte with U+FFFD, rather than decoding the whole string as
Latin-1. One bad byte no longer turns the valid UTF-8 around it into
mojibake. The decoder is boost::locale::utf, which is header-only and
available in every supported Boost version. Boost's decode consumes the
byte that cuts a sequence short, so decoding resumes after the replaced
byte, which keeps, for example, the space in "caf\xE9 au lait".

On a wide build, return the result of the validating decode rather than
decoding a second time.

Define s2us_lenient in basic_utils.cpp, so the fallback it calls is not
declared in the header.

Log categories are constants defined in code, not text from the platform,
so convert them with s2us again.

Omit error_description from WWW-Authenticate when the message contains
characters outside the set RFC 6749 section 5.2 allows, rather than
putting a converted message in the header. The JSON error body still
carries the message.

Signed-off-by: Gareth Sylvester-Bradley <garethsb@nvidia.com>
@garethsb
garethsb marked this pull request as draft November 18, 2026 13:56
@garethsb
garethsb marked this pull request as ready for review November 18, 2026 14:01
@lo-simon
lo-simon merged commit d598668 into sony:master Nov 19, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants