Skip to content

Fix truncated and invalid-UTF-8 error messages for non-ASCII characters - #357

Closed
riyadomf wants to merge 1 commit into
michaelrsweet:masterfrom
riyadomf:fix/error-message-char-formatting
Closed

riyadomf wants to merge 1 commit into
michaelrsweet:masterfrom
riyadomf:fix/error-message-char-formatting

Conversation

@riyadomf

@riyadomf riyadomf commented Sep 4, 2026 •

Copy link
Copy Markdown

Fix truncated and invalid-UTF-8 error messages for non-ASCII characters

_mxml_error() is called with "%c" and a value from mxml_getc() in four places.

The problem is that mxml_getc() returns a decoded Unicode code point, not a single byte. %c is then used to print that value as though it were a byte.

This causes two different bugs for non-ASCII characters:

  • For code points whose low byte is 0x00 (for example U+0100, U+0200, and U+1F600), %c can produce a NUL byte. Since NUL terminates a C string, the error message is truncated.
  • For code points from 0x80 to 0xFF (for example U+00E9), %c can 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 to mxml_getc().

The helper converts the character value into a safe string for use in error messages:

  • printable ASCII characters are returned as-is
  • EOF is formatted as EOF
  • everything else is formatted as 0x%04x

For example:

U+0041   -> A
U+00E9   -> 0x00e9
U+0100   -> 0x0100
U+1F600  -> 0x1f600

Using the hexadecimal form avoids inserting raw non-ASCII bytes into the message and therefore keeps the callback output valid UTF-8.

The 0x%04x format 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

#include <stdio.h>
#include <string.h>
#include "mxml.h"

static void on_err(void *cbdata, const char *m)
{
  (void)cbdata;
  printf("len=%2zu msg=[", strlen(m));
  for (const unsigned char *p = (const unsigned char *)m; *p; p ++)
    if (*p >= 0x20 && *p < 0x7f) putchar(*p); else printf("\\x%02x", *p);
  puts("]");
}

int main(void)
{
  // U+0041 'A', U+00E9, U+0100, U+1F600, each followed by "<r/>"
  const char *docs[] = {
    "A<r/>",
    "\xc3\xa9<r/>",
    "\xc4\x80<r/>",
    "\xf0\x9f\x98\x80<r/>"
  };

  for (int i = 0; i < 4; i ++)
  {
    mxml_options_t *o = mxmlOptionsNew();
    mxmlOptionsSetErrorCallback(o, on_err, NULL);
    mxml_node_t *t = mxmlLoadString(NULL, o, docs[i]);
    if (t) mxmlDelete(t);
    mxmlOptionsDelete(o);
  }
  return 0;
}

Before / after

                      before                                          after

U+0041  len=38 [... (saw 'A').]                     len=38 [... (saw 'A').]

U+00E9  len=38 [... (saw '\xe9').]   invalid UTF-8  len=43 [... (saw '0x00e9').]

U+0100  len=34 [... (saw ']          truncated      len=43 [... (saw '0x0100').]

U+1F600 len=34 [... (saw ']          truncated      len=44 [... (saw '0x1f600').]

ASCII messages are unchanged.

Changes

  • Added mxml_error_char(), a static helper next to mxml_getc().
  • Updated four call sites in mxml-file.c (lines 862, 1352, 1527, and 1709).
  • Each call site wraps its temporary buffer in a block so the declaration remains valid in any supported C dialect and the surrounding code stays unchanged.

Testing

  • make test passes.
  • Built with -Wall -Wextra; no new warnings were introduced.
  • The existing -Wimplicit-fallthrough warning at mxml-file.c:931 is also present on unmodified master.
  • Verified the fix with the reproducer for U+0041, U+00E9, U+0100, U+0200, U+1F600, U+2028, and U+FFFD.

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.

_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 michaelrsweet self-assigned this Sep 6, 2026

@michaelrsweet michaelrsweet left a comment •

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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...

@michaelrsweet michaelrsweet added the enhancement New feature or request label Sep 6, 2026
@michaelrsweet michaelrsweet added this to the Stable milestone Sep 6, 2026
@michaelrsweet

Copy link
Copy Markdown
Owner

[master d986100] Report bad characters using U+XXXX instead of '%c' (Issue #357)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants