diff mbox series

secvar/secvar_util: Properly free memory on zalloc fail

Message ID 20210421200545.31921-1-nick.child@ibm.com
State Accepted
Headers show
Series secvar/secvar_util: Properly free memory on zalloc fail | expand

Commit Message

Nick Child April 21, 2021, 8:05 p.m. UTC
If allocating the secure variable name of a secure variable struct,
`secvar->key`, fails then the secvar struct should be freed before
returning NULL. Previously, if this allocation fails, then only the
`secvar->key` is freed (which is likely a typo) leaving the allocated
`secvar` struct allocated and returning NULL. This memory leak can be
seen with the static analysis tool `cppcheck`. After running valgrind
tests, this commit ensures that memory is properly freed if an error
occurs when allocating the `key` field of the `secvar` struct.

Signed-off-by: Nick Child <nick.child@ibm.com>
---
 libstb/secvar/secvar_util.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Vasant Hegde May 13, 2021, 1:51 p.m. UTC | #1
On 4/22/21 1:35 AM, Nick Child wrote:
> If allocating the secure variable name of a secure variable struct,
> `secvar->key`, fails then the secvar struct should be freed before
> returning NULL. Previously, if this allocation fails, then only the
> `secvar->key` is freed (which is likely a typo) leaving the allocated
> `secvar` struct allocated and returning NULL. This memory leak can be
> seen with the static analysis tool `cppcheck`. After running valgrind
> tests, this commit ensures that memory is properly freed if an error
> occurs when allocating the `key` field of the `secvar` struct.
> 
> Signed-off-by: Nick Child <nick.child@ibm.com>

Thanks! Merged to master as e964b78d5.

-Vasant
diff mbox series

Patch

diff --git a/libstb/secvar/secvar_util.c b/libstb/secvar/secvar_util.c
index 533170a3..d14a9a53 100644
--- a/libstb/secvar/secvar_util.c
+++ b/libstb/secvar/secvar_util.c
@@ -49,7 +49,7 @@  struct secvar *alloc_secvar(uint64_t key_len, uint64_t data_size)
 
 	ret->key = zalloc(key_len);
 	if (!ret->key) {
-		free(ret->key);
+		free(ret);
 		return NULL;
 	}