Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent recursive includes of keymap components #389

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/xkbcomp/ast-build.c
Original file line number Diff line number Diff line change
Expand Up @@ -534,8 +534,8 @@ IncludeCreate(struct xkb_context *ctx, char *str, enum merge_mode merge)
}

XkbFile *
XkbFileCreate(enum xkb_file_type type, char *name, ParseCommon *defs,
enum xkb_map_flags flags)
XkbFileCreate(enum xkb_file_type type, const char *file_name, char *name,
ParseCommon *defs, enum xkb_map_flags flags)
{
XkbFile *file;

Expand All @@ -545,6 +545,7 @@ XkbFileCreate(enum xkb_file_type type, char *name, ParseCommon *defs,

XkbEscapeMapName(name);
file->file_type = type;
file->file_name = file_name;
file->name = name ? name : strdup("(unnamed)");
file->defs = defs;
file->flags = flags;
Expand All @@ -570,7 +571,7 @@ XkbFileFromComponents(struct xkb_context *ctx,
if (!include)
goto err;

file = XkbFileCreate(type, NULL, (ParseCommon *) include, 0);
file = XkbFileCreate(type, NULL, NULL, (ParseCommon *) include, 0);
if (!file) {
FreeInclude(include);
goto err;
Expand All @@ -582,7 +583,7 @@ XkbFileFromComponents(struct xkb_context *ctx,
defsLast = defsLast->next = &file->common;
}

file = XkbFileCreate(FILE_TYPE_KEYMAP, NULL, defs, 0);
file = XkbFileCreate(FILE_TYPE_KEYMAP, NULL, NULL, defs, 0);
if (!file)
goto err;

Expand Down
4 changes: 2 additions & 2 deletions src/xkbcomp/ast-build.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ IncludeStmt *
IncludeCreate(struct xkb_context *ctx, char *str, enum merge_mode merge);

XkbFile *
XkbFileCreate(enum xkb_file_type type, char *name, ParseCommon *defs,
enum xkb_map_flags flags);
XkbFileCreate(enum xkb_file_type type, const char *file_name, char *name,
ParseCommon *defs, enum xkb_map_flags flags);

void
FreeStmt(ParseCommon *stmt);
Expand Down
1 change: 1 addition & 0 deletions src/xkbcomp/ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ enum xkb_map_flags {
typedef struct {
ParseCommon common;
enum xkb_file_type file_type;
const char *file_name;
char *name;
ParseCommon *defs;
enum xkb_map_flags flags;
Expand Down
25 changes: 21 additions & 4 deletions src/xkbcomp/compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ typedef struct {
typedef struct {
char *name;
int errorCount;
/* shared list of parents includes of the current processed file */
include_parents *include_parents;
SymInterpInfo default_interp;
darray(SymInterpInfo) interps;
LedInfo default_led;
Expand Down Expand Up @@ -148,10 +150,12 @@ ReportLedNotArray(CompatInfo *info, LedInfo *ledi, const char *field)

static void
InitCompatInfo(CompatInfo *info, struct xkb_context *ctx,
include_parents *parents,
ActionsInfo *actions, const struct xkb_mod_set *mods)
{
memset(info, 0, sizeof(*info));
info->ctx = ctx;
info->include_parents = parents;
info->actions = actions;
info->mods = *mods;
info->default_interp.merge = MERGE_OVERRIDE;
Expand Down Expand Up @@ -430,22 +434,25 @@ HandleIncludeCompatMap(CompatInfo *info, IncludeStmt *include)
{
CompatInfo included;

InitCompatInfo(&included, info->ctx, info->actions, &info->mods);
InitCompatInfo(&included, info->ctx, info->include_parents,
info->actions, &info->mods);
included.name = include->stmt;
include->stmt = NULL;

for (IncludeStmt *stmt = include; stmt; stmt = stmt->next_incl) {
CompatInfo next_incl;
XkbFile *file;

file = ProcessIncludeFile(info->ctx, stmt, FILE_TYPE_COMPAT);
file = ProcessIncludeFile(info->ctx, info->include_parents,
stmt, FILE_TYPE_COMPAT);
if (!file) {
info->errorCount += 10;
ClearCompatInfo(&included);
return false;
}

InitCompatInfo(&next_incl, info->ctx, info->actions, &included.mods);
InitCompatInfo(&next_incl, info->ctx, info->include_parents,
info->actions, &included.mods);
next_incl.default_interp = info->default_interp;
next_incl.default_interp.merge = stmt->merge;
next_incl.default_led = info->default_led;
Expand Down Expand Up @@ -760,6 +767,10 @@ HandleCompatMapFile(CompatInfo *info, XkbFile *file, enum merge_mode merge)
free(info->name);
info->name = strdup_safe(file->name);

include_parents_append(info->ctx, info->include_parents,
file->file_name, file->name,
!!(file->flags & MAP_IS_DEFAULT));

for (ParseCommon *stmt = file->defs; stmt; stmt = stmt->next) {
switch (stmt->type) {
case STMT_INCLUDE:
Expand Down Expand Up @@ -800,6 +811,8 @@ HandleCompatMapFile(CompatInfo *info, XkbFile *file, enum merge_mode merge)
break;
}
}

darray_remove_last(*info->include_parents);
}

/* Temporary struct for CopyInterps. */
Expand Down Expand Up @@ -909,12 +922,14 @@ CompileCompatMap(XkbFile *file, struct xkb_keymap *keymap,
{
CompatInfo info;
ActionsInfo *actions;
include_parents parents = darray_new();

actions = NewActionsInfo();
if (!actions)
return false;

InitCompatInfo(&info, keymap->ctx, actions, &keymap->mods);
InitCompatInfo(&info, keymap->ctx, &parents,
actions, &keymap->mods);
info.default_interp.merge = merge;
info.default_led.merge = merge;

Expand All @@ -927,10 +942,12 @@ CompileCompatMap(XkbFile *file, struct xkb_keymap *keymap,

ClearCompatInfo(&info);
FreeActionsInfo(actions);
darray_free(parents);
return true;

err_info:
ClearCompatInfo(&info);
FreeActionsInfo(actions);
darray_free(parents);
return false;
}
66 changes: 62 additions & 4 deletions src/xkbcomp/include.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,48 @@ ParseIncludeMap(char **str_inout, char **file_rtrn, char **map_rtrn,
return true;
}

void
include_parents_append(struct xkb_context *ctx, include_parents *parents,
const char *file, char *map, bool is_map_default)
{
struct include_atom parent = {
.file = isempty(file) ? XKB_ATOM_NONE
: xkb_atom_intern(ctx, file, strlen(file)),
.map = isempty(map) ? XKB_ATOM_NONE
: xkb_atom_intern(ctx, map, strlen(map)),
.is_map_default = is_map_default
};

darray_append(*parents, parent);
}

static inline bool
include_atom_eq(struct include_atom *atom, xkb_atom_t file, xkb_atom_t map)
{
return atom->file == file &&
(atom->map == map ||
/* in case map is null, it must be the default map */
(map == XKB_ATOM_NONE && atom->is_map_default));
}

static struct include_atom *
include_parents_lookup(struct xkb_context *ctx, include_parents *parents,
const char *file_str, char *map_str)
{
struct include_atom *parent;
xkb_atom_t file = isempty(file_str)
? XKB_ATOM_NONE
: xkb_atom_intern(ctx, file_str, strlen(file_str));
xkb_atom_t map = isempty(map_str)
? XKB_ATOM_NONE
: xkb_atom_intern(ctx, map_str, strlen(map_str));
darray_foreach(parent, *parents) {
if (include_atom_eq(parent, file, map))
return parent;
}
return NULL;
}

static const char *xkb_file_type_include_dirs[_FILE_TYPE_NUM_ENTRIES] = {
[FILE_TYPE_KEYCODES] = "keycodes",
[FILE_TYPE_TYPES] = "types",
Expand Down Expand Up @@ -289,18 +331,36 @@ FindFileInXkbPath(struct xkb_context *ctx, const char *name,
}

XkbFile *
ProcessIncludeFile(struct xkb_context *ctx, IncludeStmt *stmt,
ProcessIncludeFile(struct xkb_context *ctx,
include_parents *parents, IncludeStmt *stmt,
enum xkb_file_type file_type)
{
FILE *file;
XkbFile *xkb_file = NULL;
struct include_atom *recursive_inc;
unsigned int offset = 0;

/* Check recursive imports */
recursive_inc = include_parents_lookup(ctx, parents, stmt->file, stmt->map);
if (recursive_inc) {
struct include_atom *current_file = &darray_item(*parents, darray_size(*parents) - 1);
log_err(ctx, XKB_LOG_MESSAGE_NO_ID,
"Recursive import of %s \"%s(%s)\" in \"%s(%s)\"\n",
xkb_file_type_to_string(file_type),
xkb_atom_text(ctx, recursive_inc->file),
xkb_atom_text(ctx, recursive_inc->map),
xkb_atom_text(ctx, current_file->file),
xkb_atom_text(ctx, current_file->map));
return NULL;
}

file = FindFileInXkbPath(ctx, stmt->file, file_type, NULL, &offset);
if (!file)
if (!file) {
return NULL;
}

while (file) {

xkb_file = XkbParseFile(ctx, file, stmt->file, stmt->map);
fclose(file);

Expand Down Expand Up @@ -336,7 +396,5 @@ ProcessIncludeFile(struct xkb_context *ctx, IncludeStmt *stmt,
stmt->file);
}

/* FIXME: we have to check recursive includes here (or somewhere) */

return xkb_file;
}
18 changes: 17 additions & 1 deletion src/xkbcomp/include.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,21 @@
#ifndef XKBCOMP_INCLUDE_H
#define XKBCOMP_INCLUDE_H

#include "context.h"
#include "atom.h"

struct include_atom {
xkb_atom_t file;
xkb_atom_t map;
bool is_map_default;
};

typedef darray(struct include_atom) include_parents;

void
include_parents_append(struct xkb_context *ctx, include_parents *parents,
const char *file, char *map, bool is_map_default);

bool
ParseIncludeMap(char **str_inout, char **file_rtrn, char **map_rtrn,
char *nextop_rtrn, char **extra_data);
Expand All @@ -37,7 +52,8 @@ FindFileInXkbPath(struct xkb_context *ctx, const char *name,
unsigned int *offset);

XkbFile *
ProcessIncludeFile(struct xkb_context *ctx, IncludeStmt *stmt,
ProcessIncludeFile(struct xkb_context *ctx,
include_parents *parents, IncludeStmt *stmt,
enum xkb_file_type file_type);

#endif
24 changes: 19 additions & 5 deletions src/xkbcomp/keycodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ typedef struct {
typedef struct {
char *name;
int errorCount;
/* shared list of parents includes of the current processed file */
include_parents *include_parents;

xkb_keycode_t min_key_code;
xkb_keycode_t max_key_code;
Expand Down Expand Up @@ -155,10 +157,12 @@ ClearKeyNamesInfo(KeyNamesInfo *info)
}

static void
InitKeyNamesInfo(KeyNamesInfo *info, struct xkb_context *ctx)
InitKeyNamesInfo(KeyNamesInfo *info, struct xkb_context *ctx,
include_parents *parents)
{
memset(info, 0, sizeof(*info));
info->ctx = ctx;
info->include_parents = parents;
info->min_key_code = XKB_KEYCODE_INVALID;
#if XKB_KEYCODE_INVALID < XKB_KEYCODE_MAX
#error "Hey, you can't be changing stuff like that."
Expand Down Expand Up @@ -339,22 +343,23 @@ HandleIncludeKeycodes(KeyNamesInfo *info, IncludeStmt *include)
{
KeyNamesInfo included;

InitKeyNamesInfo(&included, info->ctx);
InitKeyNamesInfo(&included, info->ctx, info->include_parents);
included.name = include->stmt;
include->stmt = NULL;

for (IncludeStmt *stmt = include; stmt; stmt = stmt->next_incl) {
KeyNamesInfo next_incl;
XkbFile *file;

file = ProcessIncludeFile(info->ctx, stmt, FILE_TYPE_KEYCODES);
file = ProcessIncludeFile(info->ctx, info->include_parents,
stmt, FILE_TYPE_KEYCODES);
if (!file) {
info->errorCount += 10;
ClearKeyNamesInfo(&included);
return false;
}

InitKeyNamesInfo(&next_incl, info->ctx);
InitKeyNamesInfo(&next_incl, info->ctx, info->include_parents);

HandleKeycodesFile(&next_incl, file, MERGE_OVERRIDE);

Expand Down Expand Up @@ -494,6 +499,10 @@ HandleKeycodesFile(KeyNamesInfo *info, XkbFile *file, enum merge_mode merge)
free(info->name);
info->name = strdup_safe(file->name);

include_parents_append(info->ctx, info->include_parents,
file->file_name, file->name,
!!(file->flags & MAP_IS_DEFAULT));

for (ParseCommon *stmt = file->defs; stmt; stmt = stmt->next) {
switch (stmt->type) {
case STMT_INCLUDE:
Expand Down Expand Up @@ -529,6 +538,8 @@ HandleKeycodesFile(KeyNamesInfo *info, XkbFile *file, enum merge_mode merge)
break;
}
}

darray_remove_last(*info->include_parents);
}

/***====================================================================***/
Expand Down Expand Up @@ -661,8 +672,9 @@ CompileKeycodes(XkbFile *file, struct xkb_keymap *keymap,
enum merge_mode merge)
{
KeyNamesInfo info;
include_parents parents = darray_new();

InitKeyNamesInfo(&info, keymap->ctx);
InitKeyNamesInfo(&info, keymap->ctx, &parents);

HandleKeycodesFile(&info, file, merge);
if (info.errorCount != 0)
Expand All @@ -672,9 +684,11 @@ CompileKeycodes(XkbFile *file, struct xkb_keymap *keymap,
goto err_info;

ClearKeyNamesInfo(&info);
darray_free(parents);
return true;

err_info:
ClearKeyNamesInfo(&info);
darray_free(parents);
return false;
}
4 changes: 2 additions & 2 deletions src/xkbcomp/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ XkbFile : XkbCompositeMap
XkbCompositeMap : OptFlags XkbCompositeType OptMapName OBRACE
XkbMapConfigList
CBRACE SEMI
{ $$ = XkbFileCreate($2, $3, (ParseCommon *) $5.head, $1); }
{ $$ = XkbFileCreate($2, param->scanner->file_name, $3, (ParseCommon *) $5.head, $1); }
;

XkbCompositeType: XKB_KEYMAP { $$ = FILE_TYPE_KEYMAP; }
Expand All @@ -281,7 +281,7 @@ XkbMapConfig : OptFlags FileType OptMapName OBRACE
DeclList
CBRACE SEMI
{
$$ = XkbFileCreate($2, $3, $5.head, $1);
$$ = XkbFileCreate($2, param->scanner->file_name, $3, $5.head, $1);
}
;

Expand Down
Loading