Skip to contentSkip to conversation

settings: fix segfault if when settings has no group

PATCH In review James Prestwood 2 participants 3 messages Feb 21, 2019

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/2 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 | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
ell/settings.c +10 -3
index e3e8303..0c06fe7 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,11 +388,17 @@ 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] != '#') {
+			if (!has_group)
+				return false;
+
 			r = parse_keyvalue(settings, data + pos, line_len,
 						line);
+		}
 
 		pos += line_len;
 	}
Signature · 1 line
2.17.1
#1 lore raw

PATCH v3 2/2 unit: settings: add another invalid data case

This case has an ungrouped value come before a group, which is invalid and should not load.

---
 unit/test-settings.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
unit/test-settings.c +9 -3
index 71f3341..4d4fb68 100644
@@ -291,17 +291,22 @@ static void test_to_data(const void *test_data)
 }
 
 static const char *no_group_data = "key_without_group=value\n";
+static const char *key_before_group_data = "key_without_group=value\n"
+						"[GROUP]\n"
+						"valid_key=value\n";
 
 static void test_invalid_data(const void *test_data)
 {
+	const char *data = test_data;
 	struct l_settings *settings;
 	bool r;
 
 	settings = l_settings_new();
 
-	r = l_settings_load_from_data(settings, no_group_data,
-					strlen(no_group_data));
+	r = l_settings_load_from_data(settings, data, strlen(data));
 	assert(r == false);
+
+	l_settings_free(settings);
 }
 
 int main(int argc, char *argv[])
@@ -312,7 +317,8 @@ int main(int argc, char *argv[])
 	l_test_add("Load from File", test_load_from_file, NULL);
 	l_test_add("Set Methods", test_set_methods, NULL);
 	l_test_add("Export to Data 1", test_to_data, data2);
-	l_test_add("Invalid Data (no group)", test_invalid_data, NULL);
+	l_test_add("Invalid Data 1", test_invalid_data, no_group_data);
+	l_test_add("Invalid Data 2", test_invalid_data, key_before_group_data);
 
 	return l_test_run();
 }
Signature · 1 line
2.17.1
#2 lore raw
In reply to James Prestwood

Hi James,

Quoted 8 lines · James Prestwood

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.

Quoted 7 lines

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 | 13 ++++++++++---
  1 file changed, 10 insertions(+), 3 deletions(-)

Both applied, thanks.

Regards,
-Denis