* [PATCH] test: aes: fix memleak
@ 2020-02-06 16:12 Philippe Reynes
2020-02-13 22:12 ` Tom Rini
0 siblings, 1 reply; 2+ messages in thread
From: Philippe Reynes @ 2020-02-06 16:12 UTC (permalink / raw)
To: u-boot
In the first version, the result of malloc is checked
with ut_assertnonnull. But on a fail, this macro exit
the function, so previously malloc are not freed.
So to avoid a memleak, we don't use ut_assertnonnull,
but simply check the return of malloc. If one has failed,
we freed all the allocated memory and quit the function.
Reported-by: Coverity (CID: 284403)
Reported-by: Coverity (CID: 284404)
Reported-by: Coverity (CID: 284405)
Reported-by: Coverity (CID: 284406)
Reported-by: Coverity (CID: 284407)
Signed-off-by: Philippe Reynes <philippe.reynes@softathome.com>
---
test/lib/test_aes.c | 13 +++++++------
1 file changed, 7 insertions(+), 6 deletions(-)
diff --git a/test/lib/test_aes.c b/test/lib/test_aes.c
index b7b4b77..fb8a0b1 100644
--- a/test/lib/test_aes.c
+++ b/test/lib/test_aes.c
@@ -88,17 +88,17 @@ static int _lib_test_aes_run(struct unit_test_state *uts, int key_len,
/* Allocate all the buffer */
key = malloc(key_len);
- ut_assertnonnull(key);
key_exp = malloc(key_exp_len);
- ut_assertnonnull(key_exp);
iv = malloc(AES_BLOCK_LENGTH);
- ut_assertnonnull(iv);
nocipher = malloc(num_block * AES_BLOCK_LENGTH);
- ut_assertnonnull(nocipher);
ciphered = malloc((num_block + 1) * AES_BLOCK_LENGTH);
- ut_assertnonnull(ciphered);
uncipher = malloc((num_block + 1) * AES_BLOCK_LENGTH);
- ut_assertnonnull(uncipher);
+
+ if (!key || !key_exp || !iv || !nocipher || !ciphered || !uncipher) {
+ printf("%s: can't allocate memory\n", __func__);
+ ret = -1;
+ goto out;
+ }
/* Initialize all buffer */
rand_buf(key, key_len);
@@ -127,6 +127,7 @@ static int _lib_test_aes_run(struct unit_test_state *uts, int key_len,
ret = -1;
};
+ out:
/* Free all the data */
free(key);
free(key_exp);
--
2.7.4
^ permalink raw reply related [flat|nested] 2+ messages in thread
* [PATCH] test: aes: fix memleak
2020-02-06 16:12 [PATCH] test: aes: fix memleak Philippe Reynes
@ 2020-02-13 22:12 ` Tom Rini
0 siblings, 0 replies; 2+ messages in thread
From: Tom Rini @ 2020-02-13 22:12 UTC (permalink / raw)
To: u-boot
On Thu, Feb 06, 2020 at 05:12:59PM +0100, Philippe Reynes wrote:
> In the first version, the result of malloc is checked
> with ut_assertnonnull. But on a fail, this macro exit
> the function, so previously malloc are not freed.
>
> So to avoid a memleak, we don't use ut_assertnonnull,
> but simply check the return of malloc. If one has failed,
> we freed all the allocated memory and quit the function.
>
> Reported-by: Coverity (CID: 284403)
> Reported-by: Coverity (CID: 284404)
> Reported-by: Coverity (CID: 284405)
> Reported-by: Coverity (CID: 284406)
> Reported-by: Coverity (CID: 284407)
> Signed-off-by: Philippe Reynes <philippe.reynes@softathome.com>
Applied to u-boot/master, thanks!
--
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20200213/b89fd9dd/attachment.sig>
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2020-02-13 22:12 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-02-06 16:12 [PATCH] test: aes: fix memleak Philippe Reynes
2020-02-13 22:12 ` Tom Rini
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.