From b97a110721646e5a2a2a3f01837c5cdb4e9a6cc3 Mon Sep 17 00:00:00 2001 From: Vaclav Dolezal Date: Tue, 18 Feb 2020 09:40:44 +0100 Subject: [PATCH] fru: fix memory leak in ipmi_spd_print_fru MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixed by using centralised exiting. Signed-off-by: Václav Doležal --- lib/dimm_spd.c | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/lib/dimm_spd.c b/lib/dimm_spd.c index d559cb4..d496184 100644 --- a/lib/dimm_spd.c +++ b/lib/dimm_spd.c @@ -1620,8 +1620,10 @@ ipmi_spd_print_fru(struct ipmi_intf * intf, uint8_t id) struct ipmi_rs * rsp; struct ipmi_rq req; struct fru_info fru; - uint8_t *spd_data, msg_data[4]; + uint8_t *spd_data = NULL; + uint8_t msg_data[4]; uint32_t len, offset; + int rc = -1; msg_data[0] = id; @@ -1634,12 +1636,12 @@ ipmi_spd_print_fru(struct ipmi_intf * intf, uint8_t id) rsp = intf->sendrecv(intf, &req); if (!rsp) { printf(" Device not present (No Response)\n"); - return -1; + goto end; } if (rsp->ccode) { printf(" Device not present (%s)\n", val2str(rsp->ccode, completion_code_vals)); - return -1; + goto end; } fru.size = (rsp->data[1] << 8) | rsp->data[0]; @@ -1651,7 +1653,7 @@ ipmi_spd_print_fru(struct ipmi_intf * intf, uint8_t id) if (fru.size < 1) { lprintf(LOG_ERR, " Invalid FRU size %d", fru.size); - return -1; + goto end; } spd_data = malloc(fru.size); @@ -1659,7 +1661,7 @@ ipmi_spd_print_fru(struct ipmi_intf * intf, uint8_t id) if (!spd_data) { printf(" Unable to malloc memory for spd array of size=%d\n", fru.size); - return -1; + goto end; } memset(&req, 0, sizeof(req)); @@ -1679,21 +1681,17 @@ ipmi_spd_print_fru(struct ipmi_intf * intf, uint8_t id) rsp = intf->sendrecv(intf, &req); if (!rsp) { printf(" Device not present (No Response)\n"); - free(spd_data); - spd_data = NULL; - return -1; + goto end; } if (rsp->ccode) { printf(" Device not present (%s)\n", val2str(rsp->ccode, completion_code_vals)); - free(spd_data); - spd_data = NULL; /* Timeouts are acceptable. No DIMM in the socket */ if (rsp->ccode == 0xc3) - return 1; + rc = 1; - return -1; + goto end; } len = rsp->data[0]; @@ -1702,7 +1700,7 @@ ipmi_spd_print_fru(struct ipmi_intf * intf, uint8_t id) || len > fru.size - offset) { printf(" Not enough buffer size"); - return -1; + goto end; } memcpy(&spd_data[offset], rsp->data + 1, len); offset += len; @@ -1710,8 +1708,10 @@ ipmi_spd_print_fru(struct ipmi_intf * intf, uint8_t id) /* now print spd info */ ipmi_spd_print(spd_data, offset); - free(spd_data); - spd_data = NULL; + rc = 0; - return 0; +end: + free_n(&spd_data); + + return rc; }