diff options
author | Edward Tomasz Napierala <trasz@FreeBSD.org> | 2015-11-29 12:21:02 +0000 |
---|---|---|
committer | Edward Tomasz Napierala <trasz@FreeBSD.org> | 2015-11-29 12:21:02 +0000 |
commit | 5e7a25553c4e4d585e09220da2b28f9d4eca6b3a (patch) | |
tree | fb8fe62a646a89821b4baa71bfe681b488069983 /usr.bin/rctl | |
parent | f4e361a725f4178bd564eab0fc68433845063205 (diff) | |
download | src-5e7a25553c4e4d585e09220da2b28f9d4eca6b3a.tar.gz src-5e7a25553c4e4d585e09220da2b28f9d4eca6b3a.zip |
Fix some memory management problems.
MFC after: 1 month
Sponsored by: The FreeBSD Foundation
Notes
Notes:
svn path=/head/; revision=291449
Diffstat (limited to 'usr.bin/rctl')
-rw-r--r-- | usr.bin/rctl/rctl.c | 30 |
1 files changed, 17 insertions, 13 deletions
diff --git a/usr.bin/rctl/rctl.c b/usr.bin/rctl/rctl.c index 377046bfa8ea..cf69e0b4bf5f 100644 --- a/usr.bin/rctl/rctl.c +++ b/usr.bin/rctl/rctl.c @@ -111,10 +111,10 @@ expand_amount(char *rule) { uint64_t num; const char *subject, *subject_id, *resource, *action, *amount, *per; - char *copy, *expanded; + char *copy, *expanded, *tofree; int ret; - copy = strdup(rule); + tofree = copy = strdup(rule); if (copy == NULL) { warn("strdup"); return (NULL); @@ -128,7 +128,7 @@ expand_amount(char *rule) per = copy; if (amount == NULL || strlen(amount) == 0) { - free(copy); + free(tofree); return (rule); } @@ -139,7 +139,7 @@ expand_amount(char *rule) if (expand_number(amount, &num)) { warnx("invalid numeric value '%s'", amount); - free(copy); + free(tofree); return (NULL); } @@ -153,10 +153,11 @@ expand_amount(char *rule) if (ret <= 0) { warn("asprintf"); - free(copy); + free(tofree); return (NULL); } + free(tofree); return (expanded); } @@ -280,10 +281,10 @@ humanize_amount(char *rule) { int64_t num; const char *subject, *subject_id, *resource, *action, *amount, *per; - char *copy, *humanized, buf[6]; + char *copy, *humanized, buf[6], *tofree; int ret; - copy = strdup(rule); + tofree = copy = strdup(rule); if (copy == NULL) err(1, "strdup"); @@ -296,7 +297,7 @@ humanize_amount(char *rule) if (amount == NULL || strlen(amount) == 0 || str2int64(amount, &num) != 0) { - free(copy); + free(tofree); return (rule); } @@ -320,6 +321,7 @@ humanize_amount(char *rule) if (ret <= 0) err(1, "asprintf"); + free(tofree); return (humanized); } @@ -426,10 +428,10 @@ humanize_usage_amount(char *usage) { int64_t num; const char *resource, *amount; - char *copy, *humanized, buf[6]; + char *copy, *humanized, buf[6], *tofree; int ret; - copy = strdup(usage); + tofree = copy = strdup(usage); if (copy == NULL) err(1, "strdup"); @@ -442,7 +444,7 @@ humanize_usage_amount(char *usage) if (str2int64(amount, &num) != 0 || humanize_number(buf, sizeof(buf), num, "", HN_AUTOSCALE, HN_DECIMAL | HN_NOSPACE) == -1) { - free(copy); + free(tofree); return (usage); } @@ -450,6 +452,7 @@ humanize_usage_amount(char *usage) if (ret <= 0) err(1, "asprintf"); + free(tofree); return (humanized); } @@ -460,7 +463,7 @@ static int show_usage(const char *filter, int hflag) { int error; - char *outbuf = NULL, *tmp; + char *copy, *outbuf = NULL, *tmp; size_t outbuflen = RCTL_DEFAULT_BUFSIZE / 4; do { @@ -478,7 +481,8 @@ show_usage(const char *filter, int hflag) } } while (error && errno == ERANGE); - while ((tmp = strsep(&outbuf, ",")) != NULL) { + copy = outbuf; + while ((tmp = strsep(©, ",")) != NULL) { if (tmp[0] == '\0') break; /* XXX */ |