Skip to content

Commit

Permalink
symbols: Fix too liberal parsing of keysyms lists
Browse files Browse the repository at this point in the history
Currently we are too liberal when parsing symbols lists: e.g.
`[{a,{b}}]` is parsed as `[{a,b}]` but it should be rejected.
  • Loading branch information
wismill committed Oct 8, 2024
1 parent 7a8772b commit 3b3edd5
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 14 deletions.
14 changes: 10 additions & 4 deletions src/xkbcomp/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ resolve_keysym(struct parser_param *param, const char *name, xkb_keysym_t *sym_r
%type <any> Decl
%type <anyList> DeclList
%type <expr> Expr Term Lhs Terminal ArrayInit KeySyms
%type <expr> OptKeySymList KeySymList Action Coord CoordList
%type <expr> OptKeySymList MultiKeySymList KeySymList Action Coord CoordList
%type <exprList> OptExprList ExprList ActionList
%type <var> VarDecl SymbolsVarDecl
%type <varList> VarDeclList SymbolsBody
Expand Down Expand Up @@ -708,20 +708,26 @@ Terminal : String
{ $$ = ExprCreateKeyName($1); }
;

OptKeySymList : KeySymList { $$ = $1; }
OptKeySymList : MultiKeySymList { $$ = $1; }
| { $$ = NULL; }
;

KeySymList : KeySymList COMMA KeySym
MultiKeySymList : MultiKeySymList COMMA KeySym
{ $$ = ExprAppendKeysymList($1, $3); }
| KeySymList COMMA KeySyms
| MultiKeySymList COMMA KeySyms
{ $$ = ExprAppendMultiKeysymList($1, $3); }
| KeySym
{ $$ = ExprCreateKeysymList($1); }
| KeySyms
{ $$ = ExprCreateMultiKeysymList($1); }
;

KeySymList : KeySymList COMMA KeySym
{ $$ = ExprAppendKeysymList($1, $3); }
| KeySym
{ $$ = ExprCreateKeysymList($1); }
;

KeySyms : OBRACE KeySymList CBRACE
{ $$ = $2; }
;
Expand Down
63 changes: 53 additions & 10 deletions test/buffercomp.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,10 @@ test_encodings(struct xkb_context *ctx)
}

static void
test_recursive(void)
test_recursive(struct xkb_context *ctx)
{
struct xkb_context *ctx = test_get_context(0);
struct xkb_keymap *keymap;

assert(ctx);

const char* const keymaps[] = {
/* Recursive keycodes */
"Keycodes: recursive",
Expand Down Expand Up @@ -152,26 +149,70 @@ test_recursive(void)
" xkb_types { include \"complete\" };"
" xkb_compat { include \"complete\" };"
" xkb_symbols { include \"recursive(bar)\" };"
// "};"
"};"
};

int len = sizeof(keymaps) / sizeof(keymaps[0]);
const size_t len = ARRAY_SIZE(keymaps);

for (int k = 0; k < len; k++) {
for (size_t k = 0; k < len; k++) {
fprintf(stderr, "*** Recursive test: %s ***\n", keymaps[k++]);
keymap = test_compile_buffer(ctx, keymaps[k], strlen(keymaps[k]));
assert(!keymap);
}
}

xkb_context_unref(ctx);
static void
test_multi_keysyms(struct xkb_context *ctx)
{
struct xkb_keymap *keymap;
const char* const invalid_keymaps[] = {
#define make_keymap(keysyms) \
"xkb_keymap {\n" \
" xkb_keycodes {\n" \
" minimum= 8;\n" \
" maximum= 10;\n" \
" <AE01> = 10;\n" \
" };\n" \
" xkb_types { include \"basic\" };\n" \
" xkb_compat { include \"basic\" };\n" \
" xkb_symbols {\n" \
" key <AE01> { [ " keysyms " ] };\n" \
" };\n" \
"};"
make_keymap("{}"),
make_keymap("{ {} }"),
make_keymap("{ a, {} }"),
make_keymap("{ {}, b }"),
make_keymap("{ a, { b } }"),
make_keymap("{ { a }, b }"),
make_keymap("{ { a, b }, c }"),
make_keymap("{ a, { b, c } }"),
make_keymap("{ a, {}, c }"),
make_keymap("{ a, b, {} }"),
make_keymap("{ {}, b, c }"),
make_keymap("{ { a, b }, c, d }"),
make_keymap("{ a, { b, c }, d }"),
make_keymap("{ a, b, { c, d } }"),
make_keymap("{ { a, b }, { c, d } }"),
};
const size_t len = ARRAY_SIZE(invalid_keymaps);

for (size_t k = 0; k < len; k++) {
fprintf(stderr, "*** test_multi_keysyms: #%zu ***\n", k + 1);
keymap = test_compile_buffer(ctx, invalid_keymaps[k],
strlen(invalid_keymaps[k]));
assert_printf(!keymap,
"The following symbols *do* parse:\n%s\n",
invalid_keymaps[k]);
}
}

int
main(int argc, char *argv[])
{
test_init();

struct xkb_context *ctx = test_get_context(0);
struct xkb_context *ctx = test_get_context(CONTEXT_NO_FLAG);
struct xkb_keymap *keymap;
char *original, *dump;

Expand Down Expand Up @@ -229,9 +270,11 @@ main(int argc, char *argv[])
xkb_keymap_unref(keymap);
free(dump);

test_recursive(ctx);
test_multi_keysyms(ctx);

xkb_context_unref(ctx);

test_recursive();

return 0;
}

0 comments on commit 3b3edd5

Please sign in to comment.