]> granicus.if.org Git - procps-ng/commitdiff
library: remedy several 'coverity scan' resource leaks
authorJim Warner <james.warner@comcast.net>
Wed, 27 Oct 2021 05:00:00 +0000 (00:00 -0500)
committerCraig Small <csmall@dropbear.xyz>
Wed, 27 Oct 2021 20:06:57 +0000 (07:06 +1100)
This commit will place the 'file2strvec' function on a
par with the 'file2str' function if ENOMEM was raised.

Plus, just to keep coverity happy, we'll also clean up
after potential failures with the 'openproc' function.

Signed-off-by: Jim Warner <james.warner@comcast.net>
proc/readproc.c

index 27e4e381acb4a16e8bc6b11521898f806dc80166..b4127b411e67699a55c2441b567305a012f7b693 100644 (file)
@@ -784,7 +784,10 @@ static char **file2strvec(const char *directory, const char *what) {
 
         if (n <= 0) break;         /* unneeded (end_of_file = 1) but avoid realloc */
         rbuf = realloc(rbuf, tot + n);          /* allocate more memory */
-        if (!rbuf) return NULL;
+        if (!rbuf) {
+            close(fd);
+            return NULL;
+        }
         memcpy(rbuf + tot, buf, n);             /* copy buffer into it */
         tot += n;                               /* increment total byte ctr */
         if (end_of_file)
@@ -1546,11 +1549,18 @@ PROCTAB *openproc(unsigned flags, ...) {
     va_end(ap);
 
     if (!src_buffer
-    && !(src_buffer = malloc(MAX_BUFSZ)))
+    && !(src_buffer = malloc(MAX_BUFSZ))) {
+        closedir(PT->procfs);
+        free(PT);
         return NULL;
+    }
     if (!dst_buffer
-    && !(dst_buffer = malloc(MAX_BUFSZ)))
+    && !(dst_buffer = malloc(MAX_BUFSZ))) {
+        closedir(PT->procfs);
+        free(src_buffer);
+        free(PT);
         return NULL;
+    }
 
     return PT;
 }