forked from labwc/labwc
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
environment: disallow recursive env var assignments
...like FOO=$FOO:bar to avoid environment variables growing on reconfigure.
- Loading branch information
Showing
7 changed files
with
134 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/* SPDX-License-Identifier: GPL-2.0-only */ | ||
#ifndef LABWC_IS_RECURSIVE_H | ||
#define LABWC_IS_RECURSIVE_H | ||
#include <stdbool.h> | ||
|
||
/** | ||
* is_recursive() - Check if env var assignment key=value is recursive | ||
* @key: environment variable | ||
* @value: the value to be assigned to the environment variable | ||
* | ||
* Note: Can be used to detect assignments like FOO=$FOO:bar which may not be | ||
* good to call multiple times. | ||
*/ | ||
bool is_recursive(const char *key, const char *value); | ||
|
||
#endif /* LABWC_IS_RECURSIVE_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// SPDX-License-Identifier: GPL-2.0-only | ||
#define _POSIX_C_SOURCE 200809L | ||
#include <ctype.h> | ||
#include <stdio.h> | ||
#include <string.h> | ||
#include "common/buf.h" | ||
#include "common/string-helpers.h" | ||
#include "config/is-recursive.h" | ||
|
||
static bool | ||
is_env_var_identifier_char(char p) | ||
{ | ||
return isalnum(p) || p == '_'; | ||
} | ||
|
||
bool | ||
is_recursive(const char *key, const char *value) | ||
{ | ||
if (string_null_or_empty(key) || string_null_or_empty(value)) { | ||
return false; | ||
} | ||
|
||
bool ret = false; | ||
struct buf needle = BUF_INIT; | ||
|
||
/* Given key=FOO lets start with a needle set to ${FOO} */ | ||
buf_add_fmt(&needle, "${%s}", key); | ||
if (strstr(value, needle.data)) { | ||
ret = true; | ||
goto out; | ||
} | ||
|
||
/* | ||
* Let's try searching for $FOO. That's a bit harder because $FOO exists | ||
* in $FOO (obviously) but not in $FOOO | ||
*/ | ||
buf_clear(&needle); | ||
buf_add_fmt(&needle, "$%s", key); | ||
const char *p = value; | ||
for (;;) { | ||
/* Go through all matches of $FOO */ | ||
p = strstr(p, needle.data); | ||
if (!p) { | ||
break; | ||
} | ||
if (is_env_var_identifier_char(p[needle.len])) { | ||
/* We are in a long identifier like $FOOOOO */ | ||
p += needle.len; | ||
continue; | ||
} | ||
ret = true; | ||
break; | ||
} | ||
|
||
out: | ||
buf_reset(&needle); | ||
return ret; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
labwc_sources += files( | ||
'is-recursive.c', | ||
'rcxml.c', | ||
'keybind.c', | ||
'session.c', | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ test_lib = static_library( | |
|
||
tests = [ | ||
'buf-simple', | ||
'recursion', | ||
] | ||
|
||
foreach t : tests | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// SPDX-License-Identifier: GPL-2.0-only | ||
#define _POSIX_C_SOURCE 200809L | ||
#include <setjmp.h> | ||
#include <stdarg.h> | ||
#include <stddef.h> | ||
#include <stdio.h> | ||
#include <string.h> | ||
#include <cmocka.h> | ||
#include "common/buf.h" | ||
#include "common/mem.h" | ||
#include "../src/config/is-recursive.c" | ||
|
||
static void | ||
test_recursion(void **state) | ||
{ | ||
(void)state; | ||
|
||
assert_false(is_recursive("FOO", "bar")); | ||
assert_false(is_recursive("FOO", "FOO")); | ||
assert_false(is_recursive("FOO", "$FOOOOOO")); | ||
|
||
assert_true(is_recursive("FOO", "$FOO:bar")); | ||
assert_true(is_recursive("FOO", "O$FOO")); | ||
assert_true(is_recursive("FOO", "$FOOO$FOO")); | ||
assert_true(is_recursive("FOO", "$FOOO$FOOOO$BAR$FOO")); | ||
assert_true(is_recursive("FOO", "$FOOO$FOOOO$BAR$FOO:bar")); | ||
} | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
const struct CMUnitTest tests[] = { | ||
cmocka_unit_test(test_recursion), | ||
}; | ||
|
||
return cmocka_run_group_tests(tests, NULL, NULL); | ||
} |