mirror of
https://github.com/systemd/systemd-stable.git
synced 2024-10-30 23:21:08 +03:00
fileio.c: do not parse comments after non-whitespace chars
systemd does not want to understand comments after the first non-whitespace char occured. key=foo #comment will result into key == "foo #comment" key="foo" #comment will result into key == "foo#comment" "key= #comment" will result into key == "#comment" "key #comment" is an invalid line
This commit is contained in:
parent
bef8951800
commit
98f59e59e0
@ -239,7 +239,7 @@ static int parse_env_file_internal(
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case PRE_VALUE:
|
case PRE_VALUE:
|
||||||
if (strchr(newline, c) || strchr(COMMENTS, c)) {
|
if (strchr(newline, c)) {
|
||||||
state = PRE_KEY;
|
state = PRE_KEY;
|
||||||
key[n_key] = 0;
|
key[n_key] = 0;
|
||||||
|
|
||||||
@ -247,7 +247,7 @@ static int parse_env_file_internal(
|
|||||||
value[n_value] = 0;
|
value[n_value] = 0;
|
||||||
|
|
||||||
/* strip trailing whitespace from key */
|
/* strip trailing whitespace from key */
|
||||||
while(strchr(WHITESPACE, key[--n_key]))
|
while(n_key && strchr(WHITESPACE, key[--n_key]))
|
||||||
key[n_key]=0;
|
key[n_key]=0;
|
||||||
|
|
||||||
r = push(key, value, userdata);
|
r = push(key, value, userdata);
|
||||||
@ -279,6 +279,7 @@ static int parse_env_file_internal(
|
|||||||
case VALUE:
|
case VALUE:
|
||||||
if (strchr(newline, c)) {
|
if (strchr(newline, c)) {
|
||||||
state = PRE_KEY;
|
state = PRE_KEY;
|
||||||
|
|
||||||
key[n_key] = 0;
|
key[n_key] = 0;
|
||||||
|
|
||||||
if (value)
|
if (value)
|
||||||
@ -289,7 +290,7 @@ static int parse_env_file_internal(
|
|||||||
value[last_whitespace] = 0;
|
value[last_whitespace] = 0;
|
||||||
|
|
||||||
/* strip trailing whitespace from key */
|
/* strip trailing whitespace from key */
|
||||||
while(strchr(WHITESPACE, key[--n_key]))
|
while(n_key && strchr(WHITESPACE, key[--n_key]))
|
||||||
key[n_key]=0;
|
key[n_key]=0;
|
||||||
|
|
||||||
r = push(key, value, userdata);
|
r = push(key, value, userdata);
|
||||||
@ -417,7 +418,7 @@ static int parse_env_file_internal(
|
|||||||
value[n_value] = 0;
|
value[n_value] = 0;
|
||||||
|
|
||||||
/* strip trailing whitespace from key */
|
/* strip trailing whitespace from key */
|
||||||
while(strchr(WHITESPACE, key[--n_key]))
|
while(n_key && strchr(WHITESPACE, key[--n_key]))
|
||||||
key[n_key]=0;
|
key[n_key]=0;
|
||||||
|
|
||||||
r = push(key, value, userdata);
|
r = push(key, value, userdata);
|
||||||
|
@ -47,16 +47,18 @@ static void test_parse_env_file(void) {
|
|||||||
fputs("one=BAR \n"
|
fputs("one=BAR \n"
|
||||||
"# comment\n"
|
"# comment\n"
|
||||||
" # comment \n"
|
" # comment \n"
|
||||||
|
" ; comment \n"
|
||||||
" two = bar \n"
|
" two = bar \n"
|
||||||
"invalid line\n"
|
"invalid line\n"
|
||||||
|
"invalid line #comment\n"
|
||||||
"three = \"333\n"
|
"three = \"333\n"
|
||||||
"xxxx\"\n"
|
"xxxx\"\n"
|
||||||
"four = \'44\\\"44\'\n"
|
"four = \'44\\\"44\'\n"
|
||||||
"five = \'55\\\'55\' \"FIVE\" cinco \n"
|
"five = \'55\\\'55\' \"FIVE\" cinco \n"
|
||||||
"six = seis sechs\\\n"
|
"six = seis sechs\\\n"
|
||||||
" sis\n"
|
" sis\n"
|
||||||
"seven=\"sevenval\"#comment\n"
|
"seven=\"sevenval\" #nocomment\n"
|
||||||
"eight=#comment\n"
|
"eight=eightval #nocomment\n"
|
||||||
"export nine=nineval\n"
|
"export nine=nineval\n"
|
||||||
"ten=", f);
|
"ten=", f);
|
||||||
|
|
||||||
@ -75,20 +77,14 @@ static void test_parse_env_file(void) {
|
|||||||
assert_se(streq(a[3], "four=44\"44"));
|
assert_se(streq(a[3], "four=44\"44"));
|
||||||
assert_se(streq(a[4], "five=55\'55FIVEcinco"));
|
assert_se(streq(a[4], "five=55\'55FIVEcinco"));
|
||||||
assert_se(streq(a[5], "six=seis sechs sis"));
|
assert_se(streq(a[5], "six=seis sechs sis"));
|
||||||
assert_se(streq(a[6], "seven=sevenval"));
|
assert_se(streq(a[6], "seven=sevenval#nocomment"));
|
||||||
assert_se(streq(a[7], "eight="));
|
assert_se(streq(a[7], "eight=eightval #nocomment"));
|
||||||
assert_se(streq(a[8], "export nine=nineval"));
|
assert_se(streq(a[8], "export nine=nineval"));
|
||||||
assert_se(streq(a[9], "ten="));
|
assert_se(streq(a[9], "ten="));
|
||||||
assert_se(a[10] == NULL);
|
assert_se(a[10] == NULL);
|
||||||
|
|
||||||
strv_env_clean_log(a, "/tmp/test-fileio");
|
strv_env_clean_log(a, "/tmp/test-fileio");
|
||||||
|
|
||||||
r = write_env_file("/tmp/test-fileio", a);
|
|
||||||
assert_se(r >= 0);
|
|
||||||
|
|
||||||
r = load_env_file("/tmp/test-fileio", NULL, &b);
|
|
||||||
assert_se(r >= 0);
|
|
||||||
|
|
||||||
k = 0;
|
k = 0;
|
||||||
STRV_FOREACH(i, b) {
|
STRV_FOREACH(i, b) {
|
||||||
log_info("Got2: <%s>", *i);
|
log_info("Got2: <%s>", *i);
|
||||||
@ -128,11 +124,17 @@ static void test_parse_env_file(void) {
|
|||||||
assert_se(streq(four, "44\"44"));
|
assert_se(streq(four, "44\"44"));
|
||||||
assert_se(streq(five, "55\'55FIVEcinco"));
|
assert_se(streq(five, "55\'55FIVEcinco"));
|
||||||
assert_se(streq(six, "seis sechs sis"));
|
assert_se(streq(six, "seis sechs sis"));
|
||||||
assert_se(streq(seven, "sevenval"));
|
assert_se(streq(seven, "sevenval#nocomment"));
|
||||||
assert_se(eight == NULL);
|
assert_se(streq(eight, "eightval #nocomment"));
|
||||||
assert_se(streq(nine, "nineval"));
|
assert_se(streq(nine, "nineval"));
|
||||||
assert_se(ten == NULL);
|
assert_se(ten == NULL);
|
||||||
|
|
||||||
|
r = write_env_file("/tmp/test-fileio", a);
|
||||||
|
assert_se(r >= 0);
|
||||||
|
|
||||||
|
r = load_env_file("/tmp/test-fileio", NULL, &b);
|
||||||
|
assert_se(r >= 0);
|
||||||
|
|
||||||
unlink(t);
|
unlink(t);
|
||||||
unlink("/tmp/test-fileio");
|
unlink("/tmp/test-fileio");
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user