diff mbox series

[5/5] kpartx: Don't leak memory when getblock returns NULL

Message ID 1620775324-23984-6-git-send-email-bmarzins@redhat.com (mailing list archive)
State Not Applicable, archived
Delegated to: christophe varoqui
Headers show
Series Memory issues found by coverity | expand

Commit Message

Benjamin Marzinski May 11, 2021, 11:22 p.m. UTC
If a new block was allocated, but couldn't be filled, getblock will
discard it. When it does so, it needs to free the block to avoid leaking
memory. Found by coverity.

Signed-off-by: Benjamin Marzinski <bmarzins@redhat.com>
---
 kpartx/kpartx.c | 2 ++
 1 file changed, 2 insertions(+)

Comments

Martin Wilck May 12, 2021, 12:39 p.m. UTC | #1
On Tue, 2021-05-11 at 18:22 -0500, Benjamin Marzinski wrote:
> If a new block was allocated, but couldn't be filled, getblock will
> discard it. When it does so, it needs to free the block to avoid
> leaking
> memory. Found by coverity.
> 
> Signed-off-by: Benjamin Marzinski <bmarzins@redhat.com>

Reviewed-by: Martin Wilck <mwilck@suse.com>


--
dm-devel mailing list
dm-devel@redhat.com
https://listman.redhat.com/mailman/listinfo/dm-devel
diff mbox series

Patch

diff --git a/kpartx/kpartx.c b/kpartx/kpartx.c
index 8ff116b8..7bc64543 100644
--- a/kpartx/kpartx.c
+++ b/kpartx/kpartx.c
@@ -766,6 +766,8 @@  getblock (int fd, unsigned int blknr) {
 	if (read(fd, bp->block, secsz) != secsz) {
 		fprintf(stderr, "read error, sector %d\n", secnr);
 		blockhead = bp->next;
+		free(bp->block);
+		free(bp);
 		return NULL;
 	}