From: Jim Warner Date: Wed, 27 Oct 2021 05:00:00 +0000 (-0500) Subject: library: remedy several 'coverity scan' resource leaks X-Git-Tag: v4.0.0~91 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=3e43e6273ef2e56780cd5f70c32dc769a58742bc;p=procps-ng 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 --- diff --git a/proc/readproc.c b/proc/readproc.c index 27e4e381..b4127b41 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; }