diff options
author | truckman <truckman@FreeBSD.org> | 2016-05-25 05:12:56 +0000 |
---|---|---|
committer | truckman <truckman@FreeBSD.org> | 2016-05-25 05:12:56 +0000 |
commit | 01033af8a17669d80647004092e350d756fef029 (patch) | |
tree | f20d70c47de4845e91ee8b78687b5b3a061f2f46 /bin/ps/ps.c | |
parent | 2a3e3576d0fa51c3ead7a13253e68e84849a1bf9 (diff) | |
download | FreeBSD-src-01033af8a17669d80647004092e350d756fef029.zip FreeBSD-src-01033af8a17669d80647004092e350d756fef029.tar.gz |
Fix CID 1011370 (Resource leak) in ps.
There is no need to to call strdup() on the value returned by fmt().
The latter calls fmt_argv() which always returns a dynamically
allocated string, and calling strdup() on that leaks the memory
allocated by fmt_argv(). Wave some const magic on ki_args and
ki_env to make the direct assignment happy. This requires a tweak
to the asprintf() case to avoid a const vs. non-const mismatch.
Reported by: Coverity
CID: 1011370
MFC after: 1 week
Diffstat (limited to 'bin/ps/ps.c')
-rw-r--r-- | bin/ps/ps.c | 15 |
1 files changed, 9 insertions, 6 deletions
diff --git a/bin/ps/ps.c b/bin/ps/ps.c index eeaa9c8..70b6db3 100644 --- a/bin/ps/ps.c +++ b/bin/ps/ps.c @@ -1235,6 +1235,7 @@ fmt(char **(*fn)(kvm_t *, const struct kinfo_proc *, int), KINFO *ki, static void saveuser(KINFO *ki) { + char *argsp; if (ki->ki_p->ki_flag & P_INMEM) { /* @@ -1253,10 +1254,12 @@ saveuser(KINFO *ki) if (ki->ki_p->ki_stat == SZOMB) ki->ki_args = strdup("<defunct>"); else if (UREADOK(ki) || (ki->ki_p->ki_args != NULL)) - ki->ki_args = strdup(fmt(kvm_getargv, ki, - ki->ki_p->ki_comm, ki->ki_p->ki_tdname, MAXCOMLEN)); - else - asprintf(&ki->ki_args, "(%s)", ki->ki_p->ki_comm); + ki->ki_args = fmt(kvm_getargv, ki, + ki->ki_p->ki_comm, ki->ki_p->ki_tdname, MAXCOMLEN); + else { + asprintf(&argsp, "(%s)", ki->ki_p->ki_comm); + ki->ki_args = argsp; + } if (ki->ki_args == NULL) xo_errx(1, "malloc failed"); } else { @@ -1264,8 +1267,8 @@ saveuser(KINFO *ki) } if (needenv) { if (UREADOK(ki)) - ki->ki_env = strdup(fmt(kvm_getenvv, ki, - (char *)NULL, (char *)NULL, 0)); + ki->ki_env = fmt(kvm_getenvv, ki, + (char *)NULL, (char *)NULL, 0); else ki->ki_env = strdup("()"); if (ki->ki_env == NULL) |