aboutsummaryrefslogtreecommitdiff
path: root/bin/kenv
diff options
context:
space:
mode:
authorXin LI <delphij@FreeBSD.org>2016-11-28 07:19:45 +0000
committerXin LI <delphij@FreeBSD.org>2016-11-28 07:19:45 +0000
commit305de00ec4889ff97353218601540bc9713b9001 (patch)
treeeb7a13d4bc4433fcb9dbb67564d6f1ba1b9f3366 /bin/kenv
parent6555f01eec0729408ff6cde9124c4efd1831dd57 (diff)
downloadsrc-305de00ec4889ff97353218601540bc9713b9001.tar.gz
src-305de00ec4889ff97353218601540bc9713b9001.zip
Plug a potential memory leak.
MFC after: 2 weeks
Notes
Notes: svn path=/head/; revision=309238
Diffstat (limited to 'bin/kenv')
-rw-r--r--bin/kenv/kenv.c20
1 files changed, 10 insertions, 10 deletions
diff --git a/bin/kenv/kenv.c b/bin/kenv/kenv.c
index dad8a91fb6d9..8db6c9e0e569 100644
--- a/bin/kenv/kenv.c
+++ b/bin/kenv/kenv.c
@@ -63,7 +63,6 @@ main(int argc, char **argv)
char *env, *eq, *val;
int ch, error;
- error = 0;
val = NULL;
env = NULL;
while ((ch = getopt(argc, argv, "hNquv")) != -1) {
@@ -128,7 +127,7 @@ main(int argc, char **argv)
static int
kdumpenv(void)
{
- char *buf, *cp;
+ char *buf, *bp, *cp;
int buflen, envlen;
envlen = kenv(KENV_DUMP, NULL, NULL, 0);
@@ -136,10 +135,9 @@ kdumpenv(void)
return (-1);
for (;;) {
buflen = envlen * 120 / 100;
- buf = malloc(buflen + 1);
+ buf = calloc(1, buflen + 1);
if (buf == NULL)
return (-1);
- memset(buf, 0, buflen + 1); /* Be defensive */
envlen = kenv(KENV_DUMP, NULL, buf, buflen);
if (envlen < 0) {
free(buf);
@@ -151,21 +149,23 @@ kdumpenv(void)
break;
}
- for (; *buf != '\0'; buf += strlen(buf) + 1) {
+ for (bp = buf; *bp != '\0'; bp += strlen(bp) + 1) {
if (hflag) {
- if (strncmp(buf, "hint.", 5) != 0)
+ if (strncmp(bp, "hint.", 5) != 0)
continue;
}
- cp = strchr(buf, '=');
+ cp = strchr(bp, '=');
if (cp == NULL)
continue;
*cp++ = '\0';
if (Nflag)
- printf("%s\n", buf);
+ printf("%s\n", bp);
else
- printf("%s=\"%s\"\n", buf, cp);
- buf = cp;
+ printf("%s=\"%s\"\n", bp, cp);
+ bp = cp;
}
+
+ free(buf);
return (0);
}