mirror of
https://github.com/troglobit/finit.git
synced 2026-11-20 19:41:51 +00:00
helpers: fix fgetval() crash on empty value
getty crashes reading /etc/os-release when a key it looks for has no
value, e.g.:
VERSION=
initctl crashes the same way reading .initrc, and so does the
modules-load plugin on a bare 'set' or 'set index=', which takes down
PID 1.
The loop skipping separators calls strchr(sep, str[0]), which matches
the NUL terminator at the end of the string. strsep() then sets str to
NULL and the next iteration dereferences it. A line with no separator
at all dereferences NULL right away.
Return NULL when there is no separator, and skip separators with
strspn() instead.
Reported-by: Tobias Waldekranz <tobias@waldekranz.com>
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
1 file changed
+3
-12
+3
-12
@@ -123,22 +123,13 @@ static inline char *fgetval(const char *line, const char *key, char *sep)
|
|||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
ptr = strsep(&str, sep);
|
ptr = strsep(&str, sep);
|
||||||
if (ptr && strcmp(ptr, key)) {
|
if (!str || strcmp(ptr, key)) {
|
||||||
fail:
|
|
||||||
free(copy);
|
free(copy);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/* strip optional separators, e.g., 'foo = bar' => 'bar' */
|
||||||
* Check if we should strip optional separator, e.g.,
|
ptr = str + strspn(str, sep);
|
||||||
* 'foo = bar' => '= bar' should be stripped to 'bar'
|
|
||||||
*/
|
|
||||||
while (strchr(sep, str[0])) {
|
|
||||||
ptr = strsep(&str, sep);
|
|
||||||
if (!ptr)
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
ptr = str;
|
|
||||||
|
|
||||||
if (*ptr == '"' || *ptr == '\'') {
|
if (*ptr == '"' || *ptr == '\'') {
|
||||||
char *end = &ptr[strlen(ptr) - 1];
|
char *end = &ptr[strlen(ptr) - 1];
|
||||||
|
|||||||
Reference in new issue
Block a user