From b6446650771c36a414f36b5417b3587579a807f4 Mon Sep 17 00:00:00 2001 From: Arne Redlich Date: Tue, 18 Feb 2014 21:07:46 +0100 Subject: [PATCH] nfs_opendir2_cb: plug potential memory leak Spotted by clang analyzer. Signed-off-by: Arne Redlich --- lib/libnfs.c | 68 +++++++++++++++++++++++++++------------------------- 1 file changed, 35 insertions(+), 33 deletions(-) diff --git a/lib/libnfs.c b/lib/libnfs.c index 30c3757..a237f60 100644 --- a/lib/libnfs.c +++ b/lib/libnfs.c @@ -2794,46 +2794,48 @@ static void nfs_opendir2_cb(struct rpc_context *rpc, int status, void *command_d /* steal the dirhandle */ nfsdir->current = nfsdir->entries; - rdpe_cb_data = malloc(sizeof(struct rdpe_cb_data)); - rdpe_cb_data->getattrcount = 0; - rdpe_cb_data->status = RPC_STATUS_SUCCESS; - rdpe_cb_data->data = data; - for (nfsdirent = nfsdir->entries; nfsdirent; nfsdirent = nfsdirent->next) { - struct rdpe_lookup_cb_data *rdpe_lookup_cb_data; - LOOKUP3args args; - - rdpe_lookup_cb_data = malloc(sizeof(struct rdpe_lookup_cb_data)); - rdpe_lookup_cb_data->rdpe_cb_data = rdpe_cb_data; - rdpe_lookup_cb_data->nfsdirent = nfsdirent; - - memset(&args, 0, sizeof(LOOKUP3args)); - args.what.dir = data->fh; - args.what.name = nfsdirent->name; - - if (rpc_nfs3_lookup_async(nfs->rpc, nfs_opendir3_cb, &args, rdpe_lookup_cb_data) != 0) { - rpc_set_error(nfs->rpc, "RPC error: Failed to send READDIR LOOKUP call"); - - /* if we have already commands in flight, we cant just stop, we have to wait for the - * commands in flight to complete - */ - if (rdpe_cb_data->getattrcount > 0) { + if (nfsdir->entries) { + rdpe_cb_data = malloc(sizeof(struct rdpe_cb_data)); + rdpe_cb_data->getattrcount = 0; + rdpe_cb_data->status = RPC_STATUS_SUCCESS; + rdpe_cb_data->data = data; + for (nfsdirent = nfsdir->entries; nfsdirent; nfsdirent = nfsdirent->next) { + struct rdpe_lookup_cb_data *rdpe_lookup_cb_data; + LOOKUP3args args; + + rdpe_lookup_cb_data = malloc(sizeof(struct rdpe_lookup_cb_data)); + rdpe_lookup_cb_data->rdpe_cb_data = rdpe_cb_data; + rdpe_lookup_cb_data->nfsdirent = nfsdirent; + + memset(&args, 0, sizeof(LOOKUP3args)); + args.what.dir = data->fh; + args.what.name = nfsdirent->name; + + if (rpc_nfs3_lookup_async(nfs->rpc, nfs_opendir3_cb, &args, rdpe_lookup_cb_data) != 0) { + rpc_set_error(nfs->rpc, "RPC error: Failed to send READDIR LOOKUP call"); + + /* if we have already commands in flight, we cant just stop, we have to wait for the + * commands in flight to complete + */ + if (rdpe_cb_data->getattrcount > 0) { + nfs_free_nfsdir(nfsdir); + data->continue_data = NULL; + free_nfs_cb_data(data); + rdpe_cb_data->status = RPC_STATUS_ERROR; + free(rdpe_lookup_cb_data); + return; + } + + data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); nfs_free_nfsdir(nfsdir); data->continue_data = NULL; free_nfs_cb_data(data); - rdpe_cb_data->status = RPC_STATUS_ERROR; free(rdpe_lookup_cb_data); + free(rdpe_cb_data); return; } - - data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); - nfs_free_nfsdir(nfsdir); - data->continue_data = NULL; - free_nfs_cb_data(data); - free(rdpe_lookup_cb_data); - free(rdpe_cb_data); - return; + rdpe_cb_data->getattrcount++; } - rdpe_cb_data->getattrcount++; } } -- 2.34.1