If realloc() == NULL we lost the pointer to old memory 56/13156/5
authorPat Riehecky <riehecky@fnal.gov>
Fri, 1 Jun 2018 20:59:37 +0000 (15:59 -0500)
committerBenjamin Kaduk <kaduk@mit.edu>
Mon, 15 Feb 2021 01:03:46 +0000 (20:03 -0500)
Systems under memory pressure may fail to realloc().  If so, the pointer
to the old memory is lost, but not released.  This code catches the
pointer before hand to ensure the memory isn't leaked. (via cppcheck)

Change-Id: I4c5a11c1daf4e78f7ffde71af0175d9106f6c3cd
Reviewed-on: https://gerrit.openafs.org/13156
Reviewed-by: Joe Gorse <jhgorse@gmail.com>
Tested-by: BuildBot <buildbot@rampaginggeek.com>
Reviewed-by: Benjamin Kaduk <kaduk@mit.edu>

src/tools/dumpscan/parsevnode.c
src/tools/dumpscan/pathname.c
src/tools/dumpscan/primitive.c

index f4e5e4b..fafcd83 100644 (file)
@@ -453,9 +453,8 @@ parse_vdata(XFILE * X, unsigned char *tag, tagged_field * field,
        case vSymlink:
            if (v->size > symlink_size) {
                if (symlink_buf)
-                   symlink_buf = realloc(symlink_buf, v->size + 1);
-               else
-                   symlink_buf = (char *)malloc(v->size + 1);
+                   free(symlink_buf);
+               symlink_buf = (char *)malloc(v->size + 1);
                symlink_size = symlink_buf ? v->size : 0;
            }
            if (symlink_buf) {
index b4d4e5c..56b4280 100644 (file)
@@ -271,7 +271,7 @@ Path_Build(XFILE * X, path_hashinfo * phi, afs_uint32 vnode, char **his_path,
           int fast)
 {
     vhash_ent *vhe;
-    char *name, *path = 0, fastbuf[12];
+    char *name, *path = 0, *oldpath = 0, fastbuf[12];
     char *x, *y;
     afs_uint32 parent, r;
     int nl, pl = 0;
@@ -362,8 +362,10 @@ Path_Build(XFILE * X, path_hashinfo * phi, afs_uint32 vnode, char **his_path,
 
        nl = strlen(name);
        if (path) {
+           oldpath = path;
            path = realloc(path, nl + pl + 2);
            if (!path) {
+               free(oldpath);
                if (phi->p->cb_error)
                    (phi->p->cb_error) (ENOMEM, 1, phi->p->err_refcon,
                                        "No memory for pathname of vnode 1");
index d11fbef..4e5514f 100644 (file)
@@ -75,7 +75,7 @@ afs_uint32
 ReadString(XFILE * X, unsigned char **val)
 {
     static unsigned char buf[BUFSIZE];
-    unsigned char *result = 0;
+    unsigned char *result = NULL, *old_result = NULL;
     afs_uint32 r;
     int i, l = 0;
 
@@ -92,9 +92,12 @@ ReadString(XFILE * X, unsigned char **val)
                break;
        }
        /* iff we found a null, i < BUFSIZE and buf[i] holds the NUL */
-       if (result)
-           result = realloc(result, l + i + 1);
-       else
+       if (result) {
+           old_result = result;
+           result = realloc(old_result, l + i + 1);
+           if (!result) /* realloc failed, manually free old mem */
+               free(old_result);
+       } else
            result = (unsigned char *)malloc(i + 1);
        if (!result)
            return ENOMEM;