From: Arshad Hussain Date: Mon, 20 Nov 2023 07:43:07 +0000 (+0530) Subject: LU-17000 utils: fix leak in 'lfs find' error handling X-Git-Tag: 2.15.60~35 X-Git-Url: https://git.whamcloud.com/?a=commitdiff_plain;h=refs%2Fchanges%2F82%2F53182%2F4;p=fs%2Flustre-release.git LU-17000 utils: fix leak in 'lfs find' error handling Fix memory leak reported by Coverity in setup_indexes() in case of errors during OST UUID initialization. CoverityID: 397693 ("Resource leak") Test-Parameters: trivial testlist=sanity,conf-sanity Signed-off-by: Arshad Hussain Fixes: 05334b90a5d ("LU-16331 utils: fix 'lfs find -O ' with gaps") Change-Id: Ibfd10cebaf3198ae2e9bb35686be420e4cd0050b Reviewed-on: https://review.whamcloud.com/c/fs/lustre-release/+/53182 Tested-by: jenkins Tested-by: Maloo Reviewed-by: Andreas Dilger Reviewed-by: Timothy Day Reviewed-by: Anjus George Reviewed-by: James Simmons Reviewed-by: Rick Mohr Reviewed-by: Oleg Drokin --- diff --git a/lustre/utils/liblustreapi.c b/lustre/utils/liblustreapi.c index bc16eb3..2bfdfa8 100644 --- a/lustre/utils/liblustreapi.c +++ b/lustre/utils/liblustreapi.c @@ -2371,11 +2371,11 @@ static int setup_indexes(int d, char *path, struct obd_uuid *obduuids, enum tgt_type type) { int ret, obdcount, maxidx, obd_valid = 0, obdnum; - long i; - struct obd_uuid *uuids = NULL; int *indices = NULL; - char buf[16]; + struct obd_uuid *uuids = NULL; int *indexes; + char buf[16]; + long i; if (type == LOV_TYPE) ret = get_param_lov(path, "numobd", buf, sizeof(buf)); @@ -2390,8 +2390,8 @@ static int setup_indexes(int d, char *path, struct obd_uuid *obduuids, return -ENOMEM; indices = malloc(obdcount * sizeof(int)); if (indices == NULL) { - free(uuids); - return -ENOMEM; + ret = -ENOMEM; + goto out_uuids; } maxidx = obdcount; @@ -2400,20 +2400,22 @@ retry_get_uuids: if (ret) { if (ret == -EOVERFLOW) { struct obd_uuid *uuids_temp; - int *indices_temp; + int *indices_temp = NULL; uuids_temp = realloc(uuids, obdcount * sizeof(struct obd_uuid)); - indices_temp = realloc(indices, obdcount * sizeof(int)); - if (uuids_temp != NULL && indices_temp != NULL) { + if (uuids_temp) uuids = uuids_temp; + indices_temp = realloc(indices, obdcount * sizeof(int)); + if (indices_temp) indices = indices_temp; + if (uuids_temp && indices_temp) goto retry_get_uuids; - } ret = -ENOMEM; } - llapi_error(LLAPI_MSG_ERROR, ret, "cannot get ost uuid"); + llapi_error(LLAPI_MSG_ERROR, ret, "cannot fetch %u OST UUIDs", + obdcount); goto out_free; } @@ -2458,10 +2460,11 @@ retry_get_uuids: *obdindexes = indexes; out_free: - if (uuids) - free(uuids); if (indices) free(indices); +out_uuids: + if (uuids) + free(uuids); return ret; }