Skip to content

Commit

Permalink
xkbcomp: skip heading UTF-8 encoded BOM (U+FEFF)
Browse files Browse the repository at this point in the history
Leading BOM is legal and is used as a signature — an indication that
an otherwise unmarked text file is in UTF-8.
See: https://www.unicode.org/faq/utf_bom.html#bom5 for further
details.
  • Loading branch information
wismill committed Oct 30, 2023
1 parent adef374 commit 8d66817
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/xkbcomp/scanner.c
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,11 @@ XkbParseString(struct xkb_context *ctx, const char *string, size_t len,
{
struct scanner scanner;
scanner_init(&scanner, ctx, string, len, file_name, NULL);

/* Skip UTF-8 encoded BOM (U+FEFF) */
/* See: https://www.unicode.org/faq/utf_bom.html#bom5 */
scanner_str(&scanner, "\xef\xbb\xbf", 3);

return parse(ctx, &scanner, map);
}

Expand Down
12 changes: 12 additions & 0 deletions test/buffercomp.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,18 @@ main(int argc, char *argv[])
keymap = test_compile_buffer(ctx, "", 0);
assert(!keymap);

/* Accept UTF-8 encoded BOM (U+FEFF) */
const char *bom =
"\xef\xbb\xbfxkb_keymap {"
" xkb_keycodes { include \"evdev\" };"
" xkb_types { include \"complete\" };"
" xkb_compat { include \"complete\" };"
" xkb_symbols { include \"pc\" };"
"};";
keymap = test_compile_buffer(ctx, bom, strlen(bom));
assert(keymap);
xkb_keymap_unref(keymap);

/* Make sure we can recompile our output for a normal keymap from rules. */
keymap = test_compile_rules(ctx, NULL, NULL,
"ru,ca,de,us", ",multix,neo,intl", NULL);
Expand Down

0 comments on commit 8d66817

Please sign in to comment.