Skip to contentSkip to conversation

settings: fix segfault if when settings has no group

PATCH Superseded James Prestwood 1 participant 1 message Feb 21, 2019

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 settings: fix segfault if when settings has no group

A settings file containing a key/value but no group caused l_settings_load_from_data to segfault. This was due to not checking that a group was actually found before parsing a key/value pair.

A new flag was added, 'has_group', which gets set after a group has been parsed successfully. This flag also must be true for the load to be successful.

Also, the return of parse_key was being checked against false, when it actually is returning a unsigned int. This was changed to just check !parse_key.

---
 ell/settings.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
ell/settings.c +7 -4
index e3e8303..a5856bd 100644
@@ -347,7 +347,7 @@ static bool parse_keyvalue(struct l_settings *settings, const char *data,
 		return false;
 	}
 
-	if (parse_key(settings, data, equal - data, line) == false)
+	if (!parse_key(settings, data, equal - data, line))
 		return false;
 
 	equal += 1;
@@ -362,6 +362,7 @@ LIB_EXPORT bool l_settings_load_from_data(struct l_settings *settings,
 {
 	size_t pos = 0;
 	bool r = true;
+	bool has_group = false;
 	const char *eol;
 	size_t line = 1;
 	size_t line_len;
@@ -387,16 +388,18 @@ LIB_EXPORT bool l_settings_load_from_data(struct l_settings *settings,
 
 		line_len = eol - data - pos;
 
-		if (data[pos] == '[')
+		if (data[pos] == '[') {
 			r = parse_group(settings, data + pos, line_len, line);
-		else if (data[pos] != '#')
+			if (r)
+				has_group = true;
+		} else if (data[pos] != '#' && has_group)
 			r = parse_keyvalue(settings, data + pos, line_len,
 						line);
 
 		pos += line_len;
 	}
 
-	return r;
+	return r && has_group;
 }
 
 LIB_EXPORT char *l_settings_to_data(const struct l_settings *settings,
Signature · 1 line
2.17.1