Skip to contentSkip to conversation

test_power: don't access invalid memory on empty map keys

PATCH New Sasha Levin 1 participant 1 message Nov 10, 2014

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 test_power: don't access invalid memory on empty map keys

When providing an empty key to map_get_value() we'd access invalid memory because the function assumed there would at least be one character total in the string.

Passing an empty string would access and could possibly overwrite the byte right before the key string.

---
 drivers/power/test_power.c |    2 ++
 1 file changed, 2 insertions(+)
drivers/power/test_power.c +2 -0
index 0152f35..b47bd17 100644
@@ -275,6 +275,8 @@ static int map_get_value(struct battery_property_map *map, const char *key,
 	buf[MAX_KEYLENGTH-1] = '\0';
 
 	cr = strnlen(buf, MAX_KEYLENGTH) - 1;
+	if (cr < 0)
+		return def_val;
 	if (buf[cr] == '\n')
 		buf[cr] = '\0';
 
Signature · 1 line
1.7.10.4