diff options
author | delphij <delphij@FreeBSD.org> | 2016-12-12 02:13:35 +0000 |
---|---|---|
committer | delphij <delphij@FreeBSD.org> | 2016-12-12 02:13:35 +0000 |
commit | 45f401bf31a1a715d063c30e7757d13cd6421717 (patch) | |
tree | b79a84d904392f3802ae171b680861adf0616e88 /bin/kenv/kenv.c | |
parent | 7680dd3837d1664bab03fea9c624c34ad5a2de87 (diff) | |
download | FreeBSD-src-45f401bf31a1a715d063c30e7757d13cd6421717.zip FreeBSD-src-45f401bf31a1a715d063c30e7757d13cd6421717.tar.gz |
MFC r309238,309239:
r309238: Plug a potential memory leak.
r309239: style(9).
Diffstat (limited to 'bin/kenv/kenv.c')
-rw-r--r-- | bin/kenv/kenv.c | 24 |
1 files changed, 12 insertions, 12 deletions
diff --git a/bin/kenv/kenv.c b/bin/kenv/kenv.c index dad8a91..77caeaf 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); } @@ -190,7 +190,7 @@ ksetenv(const char *env, char *val) { int ret; - ret = kenv(KENV_SET, env, val, strlen(val)+1); + ret = kenv(KENV_SET, env, val, strlen(val) + 1); if (ret == 0) printf("%s=\"%s\"\n", env, val); return (ret); @@ -200,7 +200,7 @@ static int kunsetenv(const char *env) { int ret; - + ret = kenv(KENV_UNSET, env, NULL, 0); return (ret); } |