summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJim Warner <james.warner@comcast.net>2021-10-27 00:00:00 -0500
committerCraig Small <csmall@dropbear.xyz>2021-10-28 07:06:57 +1100
commit3e43e6273ef2e56780cd5f70c32dc769a58742bc (patch)
treec6d6c490da15c8b215b31440ba5881de4fe79326
parentdfcdc0c2c05c6e2f9f7fb605c923d27684219612 (diff)
downloadprocps-ng-3e43e6273ef2e56780cd5f70c32dc769a58742bc.tar.gz
library: remedy several 'coverity scan' resource leaks
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>
-rw-r--r--proc/readproc.c16
1 files changed, 13 insertions, 3 deletions
diff --git a/proc/readproc.c b/proc/readproc.c
index 27e4e38..b4127b4 100644
--- a/proc/readproc.c
+++ b/proc/readproc.c
@@ -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;
}