diff options
author | Wenchao Hao <haowenchao@huawei.com> | 2020-12-10 17:01:32 +0800 |
---|---|---|
committer | Wenchao Hao <haowenchao@huawei.com> | 2020-12-29 20:41:24 +0800 |
commit | 66961454632ce954b902b8a41b48c453b656973a (patch) | |
tree | 702654d6a4665fffceb346740b402513f274bd42 /libopeniscsiusr | |
parent | fda5a009c74433233b882b67e8039916b57b26ab (diff) | |
download | open-iscsi-66961454632ce954b902b8a41b48c453b656973a.tar.gz |
libopeniscsiusr: Fix memory leak in iscsi_nodes_get()
If _scandir() gets 0 node, *node_count is 0, while
calloc(*node_count, ...) might return a valid pointer although
*node_count is 0.
The memory allocated by calloc() would be freed in
iscsi_nodes_free() where did not perform free operation
if node_count is zero.
So memory leak might occur if _scandir() get 0 node.
Signed-off-by: Wenchao Hao <haowenchao@huawei.com>
Diffstat (limited to 'libopeniscsiusr')
-rw-r--r-- | libopeniscsiusr/node.c | 9 |
1 files changed, 9 insertions, 0 deletions
diff --git a/libopeniscsiusr/node.c b/libopeniscsiusr/node.c index 6bec201..0bf357b 100644 --- a/libopeniscsiusr/node.c +++ b/libopeniscsiusr/node.c @@ -109,6 +109,15 @@ int iscsi_nodes_get(struct iscsi_context *ctx, struct iscsi_node ***nodes, _good(_scandir(ctx, NODE_CONFIG_DIR, &namelist, &n), rc, out); _debug(ctx, "Got %d target from %s nodes folder", n, NODE_CONFIG_DIR); + /* + * If continue with n == 0, calloc() might return a memory which failed + * to be freed in iscsi_nodes_free() + * + * So here just goto out to exit if n == 0 + */ + if (n == 0) + goto out; + *node_count = n & UINT32_MAX; *nodes = (struct iscsi_node **) calloc(*node_count, sizeof(struct iscsi_node *)); |