Skip to contentSkip to conversation

common: fix: add NULL checks for malloc_cache_aligned in

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 malloc_cache_aligned in

- Check return value of malloc_cache_aligned for presskey and sha.
- Return -ENOMEM if memory allocation fails.
- Free allocated memory in error paths."

Triggers found by static analyzer Svace.

---
 common/autoboot.c | 5 +++++
 1 file changed, 5 insertions(+)
common/autoboot.c +5 -0
index 898a57bc92..caf54b01cd 100644
@@ -186,10 +186,15 @@ static int passwd_abort_sha256(uint64_t etime)
 	ret = hash_parse_string(algo_name, sha_env_str, sha_env);
 	if (ret) {
 		printf("Hash %s not supported!\n", algo_name);
+		free(presskey);
 		return 0;
 	}
 
 	sha = malloc_cache_aligned(SHA256_SUM_LEN);
+	if (!sha) {
+		free(presskey);  
+		return -ENOMEM;  
+	}
 	size = SHA256_SUM_LEN;
 	/*
 	 * We don't know how long the stop-string is, so we need to
Signature · 1 line
2.30.2
#1 lore raw
Quoted 8 lines · Anton Moryakov

- Check return value of malloc_cache_aligned for presskey and sha.
- Return -ENOMEM if memory allocation fails.
- Free allocated memory in error paths."

Triggers found by static analyzer Svace.

[...]

Applied to u-boot/next, thanks!

Signature · 1 line
Tom