git: 408572a24e55 - main - libnvmf: avoid resource leak

From: Warner Losh <imp_at_FreeBSD.org>
Date: Tue, 21 May 2024 23:42:57 UTC
The branch main has been updated by imp:

URL: https://cgit.FreeBSD.org/src/commit/?id=408572a24e55a4c9ff9f81d1403ec0e055841264

commit 408572a24e55a4c9ff9f81d1403ec0e055841264
Author:     Pierre Pronchery <pierre@freebsdfoundation.org>
AuthorDate: 2024-05-16 14:34:34 +0000
Commit:     Warner Losh <imp@FreeBSD.org>
CommitDate: 2024-05-21 23:41:04 +0000

    libnvmf: avoid resource leak
    
    In nvmf_host_fetch_discovery_log_page(), the log variable may have been
    allocated on the heap during the first loop cycle, and should be
    free()'d before exiting upon errors.
    
    Reported by:    Coverity
    CID:            1545034
    Sponsored by:   The FreeBSD Foundation
    
    Reviewed by: imp,jhb
    Pull Request: https://github.com/freebsd/freebsd-src/pull/1239
---
 lib/libnvmf/nvmf_host.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/lib/libnvmf/nvmf_host.c b/lib/libnvmf/nvmf_host.c
index b78e2af65897..a0d95470d8ee 100644
--- a/lib/libnvmf/nvmf_host.c
+++ b/lib/libnvmf/nvmf_host.c
@@ -653,19 +653,23 @@ nvmf_host_fetch_discovery_log_page(struct nvmf_qpair *qp,
 	log = NULL;
 	for (;;) {
 		error = nvmf_get_discovery_log_page(qp, 0, &hdr, sizeof(hdr));
-		if (error != 0)
+		if (error != 0) {
+			free(log);
 			return (error);
+		}
 		nvme_discovery_log_swapbytes(&hdr);
 
 		if (hdr.recfmt != 0) {
 			printf("NVMF: Unsupported discovery log format: %d\n",
 			    hdr.recfmt);
+			free(log);
 			return (EINVAL);
 		}
 
 		if (hdr.numrec > 1024) {
 			printf("NVMF: Too many discovery log entries: %ju\n",
 			    (uintmax_t)hdr.numrec);
+			free(log);
 			return (EFBIG);
 		}