]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
s3:lib: Fix memory leak in netapi examples
authorAndreas Schneider <asn@samba.org>
Fri, 10 Dec 2021 13:56:08 +0000 (14:56 +0100)
committerJeremy Allison <jra@samba.org>
Wed, 15 Dec 2021 19:32:30 +0000 (19:32 +0000)
Found by covscan.

Signed-off-by: Andreas Schneider <asn@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
source3/lib/netapi/examples/common.c

index a1a491e60c2b9580a82d29bb4ba1f87753bbf4fe..997144a34e21be44dda59d9b974fcd93d799f518 100644 (file)
@@ -126,6 +126,7 @@ char *netapi_read_file(const char *filename, uint32_t *psize)
        }
 
        while (size < maxsize) {
+               char *tmp = NULL;
                size_t newbufsize;
                size_t nread;
 
@@ -136,10 +137,12 @@ char *netapi_read_file(const char *filename, uint32_t *psize)
                        goto fail; /* overflow */
                }
 
-               p = realloc(p, sizeof(char)*newbufsize);
-               if (p == NULL) {
+               tmp = realloc(p, sizeof(char) * newbufsize);
+               if (tmp == NULL) {
+                       free(p);
                        goto fail;
                }
+               p = tmp;
 
                nread = fread(p+size, 1, chunk, file);
                size += nread;
@@ -151,6 +154,7 @@ char *netapi_read_file(const char *filename, uint32_t *psize)
 
        err = ferror(file);
        if (err != 0) {
+               free(p);
                goto fail;
        }