From 2123888d4de8cce9ade79933638a9548fd5fcef2 Mon Sep 17 00:00:00 2001 From: Kyle Kneitinger Date: Tue, 26 Apr 2016 00:20:42 -0700 Subject: [PATCH] Ensure config variables match on longest-length (#2306) fixes #2235 --- src/config_parser.c | 16 +++++++++++++++- testcases/t/183-config-variables.t | 16 ++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/config_parser.c b/src/config_parser.c index e97a37e1..5ee69e8d 100644 --- a/src/config_parser.c +++ b/src/config_parser.c @@ -879,9 +879,23 @@ bool parse_file(const char *f, bool use_nagbar) { v_value++; struct Variable *new = scalloc(1, sizeof(struct Variable)); + struct Variable *test = NULL, *loc = NULL; new->key = sstrdup(v_key); new->value = sstrdup(v_value); - SLIST_INSERT_HEAD(&variables, new, variables); + /* ensure that the correct variable is matched in case of one being + * the prefix of another */ + SLIST_FOREACH(test, &variables, variables) { + if (strlen(new->key) >= strlen(test->key)) + break; + loc = test; + } + + if (loc == NULL) { + SLIST_INSERT_HEAD(&variables, new, variables); + } else { + SLIST_INSERT_AFTER(loc, new, variables); + } + DLOG("Got new variable %s = %s\n", v_key, v_value); continue; } diff --git a/testcases/t/183-config-variables.t b/testcases/t/183-config-variables.t index 8fbbff70..65f16679 100644 --- a/testcases/t/183-config-variables.t +++ b/testcases/t/183-config-variables.t @@ -79,6 +79,22 @@ EOT is(launch_get_border($config), 'none', 'no border'); +##################################################################### +# test that longest matching variable name is substituted +##################################################################### + +$config = <<'EOT'; +# i3 config file (v4) +font -misc-fixed-medium-r-normal--13-120-75-75-C-70-iso10646-1 + +set $var normal title +set $vartest special title +set $vart mundane title +for_window [title="$vartest"] border none +EOT + +is(launch_get_border($config), 'none', 'no border'); + done_testing;