Repository navigation
Conversation
_mxml_error() was called with "%c" and a Unicode character value in four places. A value whose low byte is zero (U+0100, U+0200, U+1F600, ...) converted to a nul and truncated the message, and a value from 0x80 to 0xff emitted a lone byte that is not valid UTF-8. Both reach the public error callback. Add mxml_error_char() to format a character value for a message: printable ASCII as-is, EOF as "EOF", anything else as 0x%04x, which matches the existing "Invalid UTF-8 sequence for character 0x%04x." messages in the same file. Before: len=38 msg=[XML does not start with '<' (saw '\xe9').] len=34 msg=[XML does not start with '<' (saw '] After: len=43 msg=[XML does not start with '<' (saw '0x00e9').] len=43 msg=[XML does not start with '<' (saw '0x0100').] ASCII output is unchanged. make test passes.
michaelrsweet
requested changes
Sep 6, 2026
Owner
There was a problem hiding this comment.
I'm not keen on the proposed changes - both because the coding style doesn't match and the additional code just to report an error seems wasteful.
I do like the idea of reporting Unicode code points instead ("U+xxxx"), and we should treat end-of-file errors separately from bad characters although reporting "U+FFFFFFFF" would probably be just fine too...
Owner
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.
Fix truncated and invalid-UTF-8 error messages for non-ASCII characters
_mxml_error()is called with"%c"and a value frommxml_getc()in four places.The problem is that
mxml_getc()returns a decoded Unicode code point, not a single byte.%cis then used to print that value as though it were a byte.This causes two different bugs for non-ASCII characters:
0x00(for example U+0100, U+0200, and U+1F600),%ccan produce a NUL byte. Since NUL terminates a C string, the error message is truncated.0x80to0xFF(for example U+00E9),%ccan put that value into the message as a single byte. That byte is not a valid UTF-8 encoding of the character, so the error callback receives an invalid UTF-8 string.Both cases are observable through the public
mxmlOptionsSetErrorCallback()callback.Fix
This adds a small static helper,
mxml_error_char(), next tomxml_getc().The helper converts the character value into a safe string for use in error messages:
EOFis formatted asEOF0x%04xFor example:
Using the hexadecimal form avoids inserting raw non-ASCII bytes into the message and therefore keeps the callback output valid UTF-8.
The
0x%04xformat also matches the existing"Invalid UTF-8 sequence for character 0x%04x."messages in the same file, so the output remains consistent with the library's existing diagnostics.Printable ASCII messages are unchanged, so existing output and tests that match those messages continue to work.
Reproducer
Before / after
ASCII messages are unchanged.
Changes
mxml_error_char(), a static helper next tomxml_getc().mxml-file.c(lines 862, 1352, 1527, and 1709).Testing
make testpasses.-Wall -Wextra; no new warnings were introduced.-Wimplicit-fallthroughwarning atmxml-file.c:931is also present on unmodified master.How it was found
The issue was found while fuzzing mxml with generated XML documents.
The truncation showed up as an anomalous extra entry when parser diagnostics were grouped by message template: the same error path was producing different strings depending on which character had been rejected.
That led to the
"%c"call sites, where a decoded Unicode code point was being formatted as a single byte.