]> www.infradead.org Git - users/sagi/libnvme.git/commitdiff
libnvme: fix a memory leak when calling read_ssns()
authorMaurizio Lombardi <mlombard@redhat.com>
Tue, 7 Nov 2023 14:51:55 +0000 (15:51 +0100)
committerDaniel Wagner <wagi@monom.org>
Wed, 8 Nov 2023 13:00:46 +0000 (14:00 +0100)
If the check fails, the verify() macro executes "return -EINVAL"
without freeing the allocated memory.

Fix the bug by moving verify() before the point where we call calloc().

Signed-off-by: Maurizio Lombardi <mlombard@redhat.com>
src/nvme/nbft.c

index fa61154e2ef645e511a450d22a8e1bad256eb175..2c87088028dad9fe1bdc7e9633113d38ebc39481 100644 (file)
@@ -197,15 +197,15 @@ static int read_ssns(struct nbft_info *nbft,
        verify(raw_ssns->structure_id == NBFT_DESC_SSNS,
               "invalid ID in SSNS descriptor");
 
+       /* verify transport type */
+       verify(raw_ssns->trtype == NBFT_TRTYPE_TCP,
+              "invalid transport type in SSNS descriptor");
+
        ssns = calloc(1, sizeof(*ssns));
        if (!ssns)
                return -ENOMEM;
 
        ssns->index = le16_to_cpu(raw_ssns->index);
-
-       /* transport type */
-       verify(raw_ssns->trtype == NBFT_TRTYPE_TCP,
-              "invalid transport type in SSNS descriptor");
        strncpy(ssns->transport, trtype_to_string(raw_ssns->trtype), sizeof(ssns->transport));
 
        /* transport specific flags */