Repository navigation
Fix crash logging non-UTF-8 narrow strings - #545
Merged
Merged
Conversation
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
marked this pull request as draft
November 18, 2026 13:56
garethsb
marked this pull request as ready for review
November 18, 2026 14:01
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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::s2usthrowsstd::range_errorwhen 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,s2uspasses 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 inlog_gate::service. See #523 for the details.The second commit changes how
s2us_lenienthandles a string that is not valid UTF-8:boost::locale::utf, which is header-only and available in Boost 1.54 and later.It also:
s2usagain, since they are constants defined in codeerror_descriptionfromWWW-Authenticatewhen the message contains characters that RFC 6749 section 5.2 does not allow; the JSON error body still carries the messageTesting
Built and tested on Linux. The new tests in
basic_utils_test.cppcover valid UTF-8 around an invalid byte, a truncated sequence, and, on Windows with code page 1252,0xFCdecoding to U+00FC. The Windows code paths have not been run yet, apart from what CI covers.Co-authored-by: Gareth's 🤖 buddy