diff options
| author | Andreas Schneider <asn@samba.org> | 2018-11-12 15:31:09 +0100 |
|---|---|---|
| committer | Andreas Schneider <asn@cryptomilk.org> | 2018-11-13 07:37:25 +0100 |
| commit | 7a9d003d01adebaf089ca975b04b9d8d4e7640a2 (patch) | |
| tree | c3c7d682937416b0c232a1e3e298c66251db0e11 /lib/replace/replace.c | |
| parent | 716715496c4744ffe2565f0c1c99335ea1f53867 (diff) | |
| download | samba-7a9d003d01adebaf089ca975b04b9d8d4e7640a2.tar.gz samba-7a9d003d01adebaf089ca975b04b9d8d4e7640a2.tar.bz2 samba-7a9d003d01adebaf089ca975b04b9d8d4e7640a2.zip | |
lib:replace: Do not leak the file pointer in rep_getprogname()
And return NULL on error.
Signed-off-by: Andreas Schneider <asn@samba.org>
Reviewed-by: Ralph Boehme <slow@samba.org>
Diffstat (limited to 'lib/replace/replace.c')
| -rw-r--r-- | lib/replace/replace.c | 17 |
1 files changed, 12 insertions, 5 deletions
diff --git a/lib/replace/replace.c b/lib/replace/replace.c index e38df98ea3a..9b1df3dc3bd 100644 --- a/lib/replace/replace.c +++ b/lib/replace/replace.c @@ -996,6 +996,7 @@ const char *rep_getprogname(void) pid_t pid; size_t nread; int len; + int rc; if (rep_progname[0] != '\0') { return rep_progname; @@ -1003,12 +1004,12 @@ const char *rep_getprogname(void) len = snprintf(rep_progname, sizeof(rep_progname), "%s", "<unknown>"); if (len <= 0) { - return "<unknown>"; + return NULL; } pid = getpid(); if (pid <= 1 || pid == (pid_t)-1) { - return rep_progname; + return NULL; } len = snprintf(cmdline, @@ -1016,17 +1017,23 @@ const char *rep_getprogname(void) "/proc/%u/cmdline", (unsigned int)pid); if (len <= 0 || len == sizeof(cmdline)) { - return rep_progname; + return NULL; } fp = fopen(cmdline, "r"); if (fp == NULL) { - return rep_progname; + return NULL; } nread = fread(cmdline, 1, sizeof(cmdline) - 1, fp); + + rc = fclose(fp); + if (rc != 0) { + return NULL; + } + if (nread == 0) { - return rep_progname; + return NULL; } cmdline[nread] = '\0'; |
