From 1dd11af4c121c3749e28670315de9ed32b9ffc7b Mon Sep 17 00:00:00 2001 From: Jim Warner Date: Sat, 21 Jul 2018 00:00:00 -0500 Subject: [PATCH] top: avoid unrecognized 'Inspect' entries memory leaks Upon startup there exists the potential for some minor memory leakage should some rcfile 'Inspect' entries be invalid. By delaying any malloc/strdup until after the entries are completely validated we will prevent that. Signed-off-by: Jim Warner --- top/top.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/top/top.c b/top/top.c index 81ec54b8..ab373770 100644 --- a/top/top.c +++ b/top/top.c @@ -3763,7 +3763,7 @@ static int config_insp (FILE *fp, char *buf, size_t size) { #define nxtLINE { buf[0] = '\0'; continue; } size_t lraw = strlen(Inspect.raw) +1; int n, x; - char *s; + char *s1, *s2, *s3; if (i < 0 || (size_t)i >= INT_MAX / sizeof(struct I_ent)) break; if (lraw >= INT_MAX - size) break; @@ -3784,12 +3784,9 @@ static int config_insp (FILE *fp, char *buf, size_t size) { Rc_questions = 1; } } - if (!(s = strtok(buf, "\t\n"))) { Rc_questions = 1; nxtLINE; } - iT(type) = alloc_s(s); - if (!(s = strtok(NULL, "\t\n"))) { Rc_questions = 1; nxtLINE; } - iT(name) = alloc_s(s); - if (!(s = strtok(NULL, "\t\n"))) { Rc_questions = 1; nxtLINE; } - iT(fmts) = alloc_s(s); + if (!(s1 = strtok(buf, "\t\n"))) { Rc_questions = 1; nxtLINE; } + if (!(s2 = strtok(NULL, "\t\n"))) { Rc_questions = 1; nxtLINE; } + if (!(s3 = strtok(NULL, "\t\n"))) { Rc_questions = 1; nxtLINE; } switch (toupper(buf[0])) { case 'F': @@ -3802,6 +3799,9 @@ static int config_insp (FILE *fp, char *buf, size_t size) { Rc_questions = 1; nxtLINE; } + iT(type) = alloc_s(s1); + iT(name) = alloc_s(s2); + iT(fmts) = alloc_s(s3); iT(farg) = (strstr(iT(fmts), "%d")) ? 1 : 0; iT(fstr) = alloc_c(FNDBUFSIZ); iT(flen) = 0;