fileio: port parse_env_file_internal() to use _cleanup_free_ (#8511)
This commit is contained in:
parent
cf3fabacaa
commit
d56fced9e0
|
@ -363,11 +363,11 @@ static int parse_env_file_internal(
|
||||||
void *userdata,
|
void *userdata,
|
||||||
int *n_pushed) {
|
int *n_pushed) {
|
||||||
|
|
||||||
_cleanup_free_ char *contents = NULL, *key = NULL;
|
|
||||||
size_t key_alloc = 0, n_key = 0, value_alloc = 0, n_value = 0, last_value_whitespace = (size_t) -1, last_key_whitespace = (size_t) -1;
|
size_t key_alloc = 0, n_key = 0, value_alloc = 0, n_value = 0, last_value_whitespace = (size_t) -1, last_key_whitespace = (size_t) -1;
|
||||||
char *p, *value = NULL;
|
_cleanup_free_ char *contents = NULL, *key = NULL, *value = NULL;
|
||||||
int r;
|
|
||||||
unsigned line = 1;
|
unsigned line = 1;
|
||||||
|
char *p;
|
||||||
|
int r;
|
||||||
|
|
||||||
enum {
|
enum {
|
||||||
PRE_KEY,
|
PRE_KEY,
|
||||||
|
@ -404,10 +404,8 @@ static int parse_env_file_internal(
|
||||||
state = KEY;
|
state = KEY;
|
||||||
last_key_whitespace = (size_t) -1;
|
last_key_whitespace = (size_t) -1;
|
||||||
|
|
||||||
if (!GREEDY_REALLOC(key, key_alloc, n_key+2)) {
|
if (!GREEDY_REALLOC(key, key_alloc, n_key+2))
|
||||||
r = -ENOMEM;
|
return -ENOMEM;
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
key[n_key++] = c;
|
key[n_key++] = c;
|
||||||
}
|
}
|
||||||
|
@ -427,10 +425,8 @@ static int parse_env_file_internal(
|
||||||
else if (last_key_whitespace == (size_t) -1)
|
else if (last_key_whitespace == (size_t) -1)
|
||||||
last_key_whitespace = n_key;
|
last_key_whitespace = n_key;
|
||||||
|
|
||||||
if (!GREEDY_REALLOC(key, key_alloc, n_key+2)) {
|
if (!GREEDY_REALLOC(key, key_alloc, n_key+2))
|
||||||
r = -ENOMEM;
|
return -ENOMEM;
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
key[n_key++] = c;
|
key[n_key++] = c;
|
||||||
}
|
}
|
||||||
|
@ -452,7 +448,7 @@ static int parse_env_file_internal(
|
||||||
|
|
||||||
r = push(fname, line, key, value, userdata, n_pushed);
|
r = push(fname, line, key, value, userdata, n_pushed);
|
||||||
if (r < 0)
|
if (r < 0)
|
||||||
goto fail;
|
return r;
|
||||||
|
|
||||||
n_key = 0;
|
n_key = 0;
|
||||||
value = NULL;
|
value = NULL;
|
||||||
|
@ -467,10 +463,8 @@ static int parse_env_file_internal(
|
||||||
else if (!strchr(WHITESPACE, c)) {
|
else if (!strchr(WHITESPACE, c)) {
|
||||||
state = VALUE;
|
state = VALUE;
|
||||||
|
|
||||||
if (!GREEDY_REALLOC(value, value_alloc, n_value+2)) {
|
if (!GREEDY_REALLOC(value, value_alloc, n_value+2))
|
||||||
r = -ENOMEM;
|
return -ENOMEM;
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
value[n_value++] = c;
|
value[n_value++] = c;
|
||||||
}
|
}
|
||||||
|
@ -497,7 +491,7 @@ static int parse_env_file_internal(
|
||||||
|
|
||||||
r = push(fname, line, key, value, userdata, n_pushed);
|
r = push(fname, line, key, value, userdata, n_pushed);
|
||||||
if (r < 0)
|
if (r < 0)
|
||||||
goto fail;
|
return r;
|
||||||
|
|
||||||
n_key = 0;
|
n_key = 0;
|
||||||
value = NULL;
|
value = NULL;
|
||||||
|
@ -512,10 +506,8 @@ static int parse_env_file_internal(
|
||||||
else if (last_value_whitespace == (size_t) -1)
|
else if (last_value_whitespace == (size_t) -1)
|
||||||
last_value_whitespace = n_value;
|
last_value_whitespace = n_value;
|
||||||
|
|
||||||
if (!GREEDY_REALLOC(value, value_alloc, n_value+2)) {
|
if (!GREEDY_REALLOC(value, value_alloc, n_value+2))
|
||||||
r = -ENOMEM;
|
return -ENOMEM;
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
value[n_value++] = c;
|
value[n_value++] = c;
|
||||||
}
|
}
|
||||||
|
@ -527,10 +519,8 @@ static int parse_env_file_internal(
|
||||||
|
|
||||||
if (!strchr(newline, c)) {
|
if (!strchr(newline, c)) {
|
||||||
/* Escaped newlines we eat up entirely */
|
/* Escaped newlines we eat up entirely */
|
||||||
if (!GREEDY_REALLOC(value, value_alloc, n_value+2)) {
|
if (!GREEDY_REALLOC(value, value_alloc, n_value+2))
|
||||||
r = -ENOMEM;
|
return -ENOMEM;
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
value[n_value++] = c;
|
value[n_value++] = c;
|
||||||
}
|
}
|
||||||
|
@ -542,10 +532,8 @@ static int parse_env_file_internal(
|
||||||
else if (c == '\\')
|
else if (c == '\\')
|
||||||
state = SINGLE_QUOTE_VALUE_ESCAPE;
|
state = SINGLE_QUOTE_VALUE_ESCAPE;
|
||||||
else {
|
else {
|
||||||
if (!GREEDY_REALLOC(value, value_alloc, n_value+2)) {
|
if (!GREEDY_REALLOC(value, value_alloc, n_value+2))
|
||||||
r = -ENOMEM;
|
return -ENOMEM;
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
value[n_value++] = c;
|
value[n_value++] = c;
|
||||||
}
|
}
|
||||||
|
@ -556,10 +544,8 @@ static int parse_env_file_internal(
|
||||||
state = SINGLE_QUOTE_VALUE;
|
state = SINGLE_QUOTE_VALUE;
|
||||||
|
|
||||||
if (!strchr(newline, c)) {
|
if (!strchr(newline, c)) {
|
||||||
if (!GREEDY_REALLOC(value, value_alloc, n_value+2)) {
|
if (!GREEDY_REALLOC(value, value_alloc, n_value+2))
|
||||||
r = -ENOMEM;
|
return -ENOMEM;
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
value[n_value++] = c;
|
value[n_value++] = c;
|
||||||
}
|
}
|
||||||
|
@ -571,10 +557,8 @@ static int parse_env_file_internal(
|
||||||
else if (c == '\\')
|
else if (c == '\\')
|
||||||
state = DOUBLE_QUOTE_VALUE_ESCAPE;
|
state = DOUBLE_QUOTE_VALUE_ESCAPE;
|
||||||
else {
|
else {
|
||||||
if (!GREEDY_REALLOC(value, value_alloc, n_value+2)) {
|
if (!GREEDY_REALLOC(value, value_alloc, n_value+2))
|
||||||
r = -ENOMEM;
|
return -ENOMEM;
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
value[n_value++] = c;
|
value[n_value++] = c;
|
||||||
}
|
}
|
||||||
|
@ -585,10 +569,8 @@ static int parse_env_file_internal(
|
||||||
state = DOUBLE_QUOTE_VALUE;
|
state = DOUBLE_QUOTE_VALUE;
|
||||||
|
|
||||||
if (!strchr(newline, c)) {
|
if (!strchr(newline, c)) {
|
||||||
if (!GREEDY_REALLOC(value, value_alloc, n_value+2)) {
|
if (!GREEDY_REALLOC(value, value_alloc, n_value+2))
|
||||||
r = -ENOMEM;
|
return -ENOMEM;
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
value[n_value++] = c;
|
value[n_value++] = c;
|
||||||
}
|
}
|
||||||
|
@ -633,14 +615,12 @@ static int parse_env_file_internal(
|
||||||
|
|
||||||
r = push(fname, line, key, value, userdata, n_pushed);
|
r = push(fname, line, key, value, userdata, n_pushed);
|
||||||
if (r < 0)
|
if (r < 0)
|
||||||
goto fail;
|
return r;
|
||||||
|
|
||||||
|
value = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
fail:
|
|
||||||
free(value);
|
|
||||||
return r;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int check_utf8ness_and_warn(
|
static int check_utf8ness_and_warn(
|
||||||
|
|
Loading…
Reference in a new issue