Skip to content

Commit

Permalink
tests/9p: fix potential leak in v9fs_rreaddir()
Browse files Browse the repository at this point in the history
Free allocated directory entries in v9fs_rreaddir() if argument
`entries` was passed as NULL, to avoid a memory leak. It is
explicitly allowed by design for `entries` to be NULL. [1]

[1] https://lore.kernel.org/all/1690923.g4PEXVpXuU@silver

Reported-by: Coverity (CID 1487558)
Signed-off-by: Christian Schoenebeck <qemu_oss@crudebyte.com>
Reviewed-by: Greg Kurz <groug@kaod.org>
Message-Id: <E1psh5T-0002XN-1C@lizzy.crudebyte.com>
  • Loading branch information
cschoenebeck committed May 16, 2023
1 parent 1a67e07 commit f91ce58
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions tests/qtest/libqos/virtio-9p-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,8 @@ void v9fs_rreaddir(P9Req *req, uint32_t *count, uint32_t *nentries,
{
uint32_t local_count;
struct V9fsDirent *e = NULL;
/* only used to avoid a leak if entries was NULL */
struct V9fsDirent *unused_entries = NULL;
uint16_t slen;
uint32_t n = 0;

Expand All @@ -612,6 +614,8 @@ void v9fs_rreaddir(P9Req *req, uint32_t *count, uint32_t *nentries,
e = g_new(struct V9fsDirent, 1);
if (entries) {
*entries = e;
} else {
unused_entries = e;
}
} else {
e = e->next = g_new(struct V9fsDirent, 1);
Expand All @@ -628,6 +632,7 @@ void v9fs_rreaddir(P9Req *req, uint32_t *count, uint32_t *nentries,
*nentries = n;
}

v9fs_free_dirents(unused_entries);
v9fs_req_free(req);
}

Expand Down

0 comments on commit f91ce58

Please sign in to comment.