scanner: allow for a zero terminated string as keymap

As the documentation for xkb_keymap_new_from_buffer() states, the "input string
does not have to be zero-terminated". The actual implementation however failed
with "unrecognized token/syntax error" when it encountered a null byte.

Fix this by allowing a null byte at the last position of the buffer. Anything
else is likely a client error anyway.

Fixes #307
master
Peter Hutterer 2023-05-02 14:15:55 +10:00
parent 98b3314278
commit b06aedb8ba
2 changed files with 26 additions and 18 deletions

View File

@ -194,6 +194,10 @@ xkb_keymap_new_from_buffer(struct xkb_context *ctx,
if (!keymap)
return NULL;
/* Allow a zero-terminated string as a buffer */
if (length > 0 && buffer[length - 1] == '\0')
length--;
if (!ops->keymap_new_from_string(keymap, buffer, length)) {
xkb_keymap_unref(keymap);
return NULL;

View File

@ -46,7 +46,9 @@ main(int argc, char *argv[])
original = test_read_file(DATA_PATH);
assert(original);
keymap = test_compile_buffer(ctx, original, strlen(original));
/* Load a prebuild keymap, once without, once with the trailing \0 */
for (int i = 0; i <= 1; i++) {
keymap = test_compile_buffer(ctx, original, strlen(original) + i);
assert(keymap);
dump = xkb_keymap_get_as_string(keymap, XKB_KEYMAP_USE_ORIGINAL_FORMAT);
@ -66,9 +68,11 @@ main(int argc, char *argv[])
assert(0);
}
free(original);
free(dump);
xkb_keymap_unref(keymap);
}
free(original);
/* Make sure we can't (falsely claim to) compile an empty string. */
keymap = test_compile_buffer(ctx, "", 0);