util-lib: follow shell syntax for escape in quotes

Fixes #10659.

This changes the behaviour of parsing environment files to more closely
follow POSIX shell standards.

This has the effect that these variables defined in a file:

    VAR1='\value'
    VAR2="\value"

Are now interpreted as `\value` instead of interpreting the `\`
character and interpreting them as `value`.

For more information about the behaviour followed, see:

	http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_02
This commit is contained in:
Louis Taylor
2019-01-14 21:35:10 +00:00
parent 80aff27aeb
commit e4a8db1fbd
2 changed files with 32 additions and 26 deletions

View File

@@ -35,7 +35,6 @@ static int parse_env_file_internal(
VALUE,
VALUE_ESCAPE,
SINGLE_QUOTE_VALUE,
SINGLE_QUOTE_VALUE_ESCAPE,
DOUBLE_QUOTE_VALUE,
DOUBLE_QUOTE_VALUE_ESCAPE,
COMMENT,
@@ -186,8 +185,6 @@ static int parse_env_file_internal(
case SINGLE_QUOTE_VALUE:
if (c == '\'')
state = PRE_VALUE;
else if (c == '\\')
state = SINGLE_QUOTE_VALUE_ESCAPE;
else {
if (!GREEDY_REALLOC(value, value_alloc, n_value+2))
return -ENOMEM;
@@ -197,17 +194,6 @@ static int parse_env_file_internal(
break;
case SINGLE_QUOTE_VALUE_ESCAPE:
state = SINGLE_QUOTE_VALUE;
if (!strchr(NEWLINE, c)) {
if (!GREEDY_REALLOC(value, value_alloc, n_value+2))
return -ENOMEM;
value[n_value++] = c;
}
break;
case DOUBLE_QUOTE_VALUE:
if (c == '\"')
state = PRE_VALUE;
@@ -225,12 +211,17 @@ static int parse_env_file_internal(
case DOUBLE_QUOTE_VALUE_ESCAPE:
state = DOUBLE_QUOTE_VALUE;
if (!strchr(NEWLINE, c)) {
if (c == '"') {
if (!GREEDY_REALLOC(value, value_alloc, n_value+2))
return -ENOMEM;
value[n_value++] = '"';
} else if (!strchr(NEWLINE, c)) {
if (!GREEDY_REALLOC(value, value_alloc, n_value+3))
return -ENOMEM;
value[n_value++] = '\\';
value[n_value++] = c;
}
break;
case COMMENT:
@@ -253,7 +244,6 @@ static int parse_env_file_internal(
VALUE,
VALUE_ESCAPE,
SINGLE_QUOTE_VALUE,
SINGLE_QUOTE_VALUE_ESCAPE,
DOUBLE_QUOTE_VALUE,
DOUBLE_QUOTE_VALUE_ESCAPE)) {