Skip to contentSkip to conversation

common: fix: add NULL checks for xrealloc in make_string

PATCH In review Anton Moryakov 2 participants 2 messages Feb 6, 2025 – Feb 11, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 common: fix: add NULL checks for xrealloc in make_string

- Check return value of xrealloc for NULL.
- Free allocated memory and return NULL if xrealloc fails.
- Prevent NULL pointer dereference in strlen and strcat.

Triggers found by static analyzer Svace.

---
 common/cli_hush.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
common/cli_hush.c +7 -1
index a6a8edce1f..701ceaf080 100644
@@ -3626,7 +3626,13 @@ static char *make_string(char **inp, int *nonnull)
 		noeval = 1;
 	for (n = 0; inp[n]; n++) {
 		p = insert_var_value_sub(inp[n], noeval);
-		str = xrealloc(str, (len + strlen(p) + (2 * nonnull[n])));
+		char *new_str = xrealloc(str, (len + strlen(p) + (2 * nonnull[n])));
+		if (!new_str) {
+			free(str);  
+			if (p != inp[n]) free(p);  
+			return NULL;  
+		}
+		str = new_str;
 		if (n) {
 			strcat(str, " ");
 		} else {
Signature · 1 line
2.30.2
#1 lore raw
Quoted 8 lines · Anton Moryakov

- Check return value of xrealloc for NULL.
- Free allocated memory and return NULL if xrealloc fails.
- Prevent NULL pointer dereference in strlen and strcat.

Triggers found by static analyzer Svace.

[...]

Applied to u-boot/next, thanks!

Signature · 1 line
Tom