From: Ran Benita Date: Tue, 14 Aug 2012 08:49:19 +0000 (+0300) Subject: Allocate xkb_component_names on stack X-Git-Tag: xkbcommon-0.2.0~264 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=3634b1565e00f72fcbde655363b85f8f060d91b5;p=platform%2Fupstream%2Flibxkbcommon.git Allocate xkb_component_names on stack Instead of malloc'ing it as well. Also improve the error handling. Signed-off-by: Ran Benita --- diff --git a/src/xkbcomp/rules.c b/src/xkbcomp/rules.c index 8c0ba3a..02d7b23 100644 --- a/src/xkbcomp/rules.c +++ b/src/xkbcomp/rules.c @@ -956,6 +956,8 @@ get_components(struct rules *rules, const struct xkb_rule_names *mlvo, { struct multi_defs mdefs; + memset(kccgst, 0, sizeof(*kccgst)); + make_multi_defs(&mdefs, mlvo); clear_partial_matches(rules); @@ -976,8 +978,15 @@ get_components(struct rules *rules, const struct xkb_rule_names *mlvo, free_multi_defs(&mdefs); - return kccgst->keycodes && kccgst->symbols && kccgst->types && - kccgst->compat; + if (!kccgst->keycodes || !kccgst->symbols || !kccgst->types || + !kccgst->compat) { + free(kccgst->keycodes); + free(kccgst->symbols); + free(kccgst->types); + free(kccgst->compat); + return false; + } + return true; } static struct rules * @@ -1069,19 +1078,20 @@ free_rules(struct rules *rules) free(rules); } -struct xkb_component_names * +bool xkb_components_from_rules(struct xkb_context *ctx, - const struct xkb_rule_names *rmlvo) + const struct xkb_rule_names *rmlvo, + struct xkb_component_names *out) { + bool ret = false; struct rules *rules; - struct xkb_component_names *kccgst = NULL; FILE *file; char *path; char **include; file = XkbFindFileInPath(ctx, rmlvo->rules, FILE_TYPE_RULES, &path); if (!file) { - log_err(ctx, "could not find \"%s\" rules in XKB path\n", + log_err(ctx, "Could not find \"%s\" rules in XKB path\n", rmlvo->rules); log_err(ctx, "%zu include paths searched:\n", darray_size(ctx->includes)); @@ -1091,36 +1101,26 @@ xkb_components_from_rules(struct xkb_context *ctx, darray_size(ctx->failed_includes)); darray_foreach(include, ctx->failed_includes) log_err(ctx, "\t%s\n", *include); - return NULL; + return false; } rules = load_rules(ctx, file); if (!rules) { - log_err(ctx, "failed to load XKB rules \"%s\"\n", path); - goto err; + log_err(ctx, "Failed to load XKB rules \"%s\"\n", path); + goto err_file; } - kccgst = calloc(1, sizeof(*kccgst)); - if (!kccgst) { - log_err(ctx, "failed to allocate XKB components\n"); - goto err; + if (!get_components(rules, rmlvo, out)) { + log_err(ctx, "No components returned from XKB rules \"%s\"\n", path); + goto err_rules; } - if (!get_components(rules, rmlvo, kccgst)) { - free(kccgst->keycodes); - free(kccgst->types); - free(kccgst->compat); - free(kccgst->symbols); - free(kccgst); - kccgst = NULL; - log_err(ctx, "no components returned from XKB rules \"%s\"\n", path); - goto err; - } + ret = true; -err: +err_rules: free_rules(rules); - if (file) - fclose(file); +err_file: free(path); - return kccgst; + fclose(file); + return ret; } diff --git a/src/xkbcomp/rules.h b/src/xkbcomp/rules.h index 889f17a..0567a7b 100644 --- a/src/xkbcomp/rules.h +++ b/src/xkbcomp/rules.h @@ -29,8 +29,9 @@ #include "xkbcomp-priv.h" -struct xkb_component_names * +bool xkb_components_from_rules(struct xkb_context *ctx, - const struct xkb_rule_names *rmlvo); + const struct xkb_rule_names *rmlvo, + struct xkb_component_names *out); #endif /* RULES_H */ diff --git a/src/xkbcomp/xkbcomp.c b/src/xkbcomp/xkbcomp.c index cd5fea3..35e0ee1 100644 --- a/src/xkbcomp/xkbcomp.c +++ b/src/xkbcomp/xkbcomp.c @@ -32,44 +32,42 @@ static XkbFile * keymap_file_from_names(struct xkb_context *ctx, const struct xkb_rule_names *rmlvo) { - struct xkb_component_names *kkctgs; + struct xkb_component_names kkctgs; XkbFile *keycodes, *types, *compat, *symbols; IncludeStmt *inc; - kkctgs = xkb_components_from_rules(ctx, rmlvo); - if (!kkctgs) { + if (!xkb_components_from_rules(ctx, rmlvo, &kkctgs)) { log_err(ctx, - "couldn't look up rules '%s', model '%s', layout '%s', " + "Couldn't look up rules '%s', model '%s', layout '%s', " "variant '%s', options '%s'\n", rmlvo->rules, rmlvo->model, rmlvo->layout, rmlvo->variant, rmlvo->options); return NULL; } - inc = IncludeCreate(ctx, kkctgs->keycodes, MERGE_DEFAULT); + inc = IncludeCreate(ctx, kkctgs.keycodes, MERGE_DEFAULT); keycodes = CreateXKBFile(ctx, FILE_TYPE_KEYCODES, NULL, (ParseCommon *) inc, 0); - inc = IncludeCreate(ctx, kkctgs->types, MERGE_DEFAULT); + inc = IncludeCreate(ctx, kkctgs.types, MERGE_DEFAULT); types = CreateXKBFile(ctx, FILE_TYPE_TYPES, NULL, (ParseCommon *) inc, 0); AppendStmt(&keycodes->common, &types->common); - inc = IncludeCreate(ctx, kkctgs->compat, MERGE_DEFAULT); + inc = IncludeCreate(ctx, kkctgs.compat, MERGE_DEFAULT); compat = CreateXKBFile(ctx, FILE_TYPE_COMPAT, NULL, (ParseCommon *) inc, 0); AppendStmt(&keycodes->common, &compat->common); - inc = IncludeCreate(ctx, kkctgs->symbols, MERGE_DEFAULT); + inc = IncludeCreate(ctx, kkctgs.symbols, MERGE_DEFAULT); symbols = CreateXKBFile(ctx, FILE_TYPE_SYMBOLS, NULL, (ParseCommon *) inc, 0); AppendStmt(&keycodes->common, &symbols->common); - free(kkctgs->keycodes); - free(kkctgs->types); - free(kkctgs->compat); - free(kkctgs->symbols); - free(kkctgs); + free(kkctgs.keycodes); + free(kkctgs.types); + free(kkctgs.compat); + free(kkctgs.symbols); return CreateXKBFile(ctx, FILE_TYPE_KEYMAP, strdup(""), &keycodes->common, 0); @@ -220,9 +218,9 @@ xkb_map_new_from_names(struct xkb_context *ctx, const struct xkb_rule_names *rmlvo_in, enum xkb_map_compile_flags flags) { - struct xkb_keymap *keymap = NULL; struct xkb_rule_names rmlvo = *rmlvo_in; XkbFile *file; + struct xkb_keymap *keymap; if (isempty(rmlvo.rules)) rmlvo.rules = DEFAULT_XKB_RULES; @@ -233,13 +231,13 @@ xkb_map_new_from_names(struct xkb_context *ctx, file = keymap_file_from_names(ctx, &rmlvo); if (!file) { - log_err(ctx, "Failed to generate parsed XKB file from components\n"); + log_err(ctx, + "Failed to generate parsed XKB file from components\n"); return NULL; } keymap = compile_keymap(ctx, file); FreeXKBFile(file); - return keymap; } @@ -254,7 +252,7 @@ xkb_map_new_from_string(struct xkb_context *ctx, struct xkb_keymap *keymap; if (format != XKB_KEYMAP_FORMAT_TEXT_V1) { - log_err(ctx, "unsupported keymap format %d\n", format); + log_err(ctx, "Unsupported keymap format %d\n", format); return NULL; } diff --git a/test/rules-file.c b/test/rules-file.c index e6c6010..d239c2e 100644 --- a/test/rules-file.c +++ b/test/rules-file.c @@ -57,36 +57,34 @@ test_rules(struct xkb_context *ctx, struct test_data *data) const struct xkb_rule_names rmlvo = { data->rules, data->model, data->layout, data->variant, data->options }; - struct xkb_component_names *kccgst; + struct xkb_component_names kccgst; fprintf(stderr, "\n\nChecking : %s\t%s\t%s\t%s\t%s\n", data->rules, data->model, data->layout, data->variant, data->options); if (data->should_fail) - fprintf(stderr, "Expecting: NULL\n"); + fprintf(stderr, "Expecting: FAILURE\n"); else fprintf(stderr, "Expecting: %s\t%s\t%s\t%s\n", data->keycodes, data->types, data->compat, data->symbols); - kccgst = xkb_components_from_rules(ctx, &rmlvo); - if (!kccgst) { - fprintf(stderr, "Received: NULL\n"); + if (!xkb_components_from_rules(ctx, &rmlvo, &kccgst)) { + fprintf(stderr, "Received : FAILURE\n"); return data->should_fail; } fprintf(stderr, "Received : %s\t%s\t%s\t%s\n", - kccgst->keycodes, kccgst->types, kccgst->compat, kccgst->symbols); - - passed = streq(kccgst->keycodes, data->keycodes) && - streq(kccgst->types, data->types) && - streq(kccgst->compat, data->compat) && - streq(kccgst->symbols, data->symbols); - - free(kccgst->keycodes); - free(kccgst->types); - free(kccgst->compat); - free(kccgst->symbols); - free(kccgst); + kccgst.keycodes, kccgst.types, kccgst.compat, kccgst.symbols); + + passed = streq(kccgst.keycodes, data->keycodes) && + streq(kccgst.types, data->types) && + streq(kccgst.compat, data->compat) && + streq(kccgst.symbols, data->symbols); + + free(kccgst.keycodes); + free(kccgst.types); + free(kccgst.compat); + free(kccgst.symbols); return passed; }