ktime: Don't leak token list

ktime_ParsePeriodic generates a malloc'd token list when it parses
the time string passed to it. Make sure that we free this list before
exiting from the function.

Change-Id: I6edacc6504aeec06ede5b5ae36c22f69b0d6cea0
Reviewed-on: http://gerrit.openafs.org/7106
Tested-by: BuildBot <buildbot@rampaginggeek.com>
Reviewed-by: Derrick Brashear <shadow@dementix.org>
This commit is contained in:
Simon Wilkinson 2012-03-31 15:23:22 -04:00 committed by Derrick Brashear
parent 63f7b0a205
commit 4dc40bade4

View File

@ -37,10 +37,8 @@ static char *day[] = {
}; };
/* free token list returned by parseLine */ /* free token list returned by parseLine */
#ifdef undef static void
static LocalFreeTokens(struct token *alist)
LocalFreeTokens(alist)
struct token *alist;
{ {
struct token *nlist; struct token *nlist;
for (; alist; alist = nlist) { for (; alist; alist = nlist) {
@ -48,9 +46,8 @@ LocalFreeTokens(alist)
free(alist->key); free(alist->key);
free(alist); free(alist);
} }
return 0; return;
} }
#endif
static int static int
space(int x) space(int x)
@ -80,9 +77,9 @@ LocalParseLine(char *aline, struct token **alist)
if (inToken) { if (inToken) {
inToken = 0; /* end of this token */ inToken = 0; /* end of this token */
*tptr++ = 0; *tptr++ = 0;
ttok = (struct token *)malloc(sizeof(struct token)); ttok = malloc(sizeof(struct token));
ttok->next = NULL; ttok->next = NULL;
ttok->key = (char *)malloc(strlen(tbuffer) + 1); ttok->key = malloc(strlen(tbuffer) + 1);
strcpy(ttok->key, tbuffer); strcpy(ttok->key, tbuffer);
if (last) { if (last) {
last->next = ttok; last->next = ttok;
@ -251,11 +248,11 @@ ktime_ParsePeriodic(char *adate, struct ktime *ak)
/* look at each token */ /* look at each token */
if (strcmp(tt->key, "now") == 0) { if (strcmp(tt->key, "now") == 0) {
ak->mask |= KTIME_NOW; ak->mask |= KTIME_NOW;
return 0; goto out;
} }
if (strcmp(tt->key, "never") == 0) { if (strcmp(tt->key, "never") == 0) {
ak->mask |= KTIME_NEVER; ak->mask |= KTIME_NEVER;
return 0; goto out;
} }
if (strcmp(tt->key, "at") == 0) if (strcmp(tt->key, "at") == 0)
continue; continue;
@ -264,14 +261,17 @@ ktime_ParsePeriodic(char *adate, struct ktime *ak)
if (isdigit(tt->key[0])) { if (isdigit(tt->key[0])) {
/* parse a time */ /* parse a time */
code = ParseTime(ak, tt->key); code = ParseTime(ak, tt->key);
if (code) if (code) {
return -1; code = -1;
goto out;
}
continue; continue;
} }
/* otherwise use keyword table */ /* otherwise use keyword table */
for (tp = ptkeys;; tp++) { for (tp = ptkeys;; tp++) {
if (tp->key == NULL) { if (tp->key == NULL) {
return -1; code = -1;
goto out;
} }
if (strcmp(tp->key, tt->key) == 0) if (strcmp(tp->key, tt->key) == 0)
break; break;
@ -286,23 +286,31 @@ ktime_ParsePeriodic(char *adate, struct ktime *ak)
/* am or pm token */ /* am or pm token */
if ((tp->value & 0xff) == 1) { if ((tp->value & 0xff) == 1) {
/* pm */ /* pm */
if (!(ak->mask & KTIME_HOUR)) if (!(ak->mask & KTIME_HOUR)) {
return -1; code = -1;
if (ak->hour < 12) goto out;
}
if (ak->hour < 12) {
ak->hour += 12; ak->hour += 12;
/* 12 is 12 PM */ /* 12 is 12 PM */
else if (ak->hour != 12) } else if (ak->hour != 12) {
return -1; code = -1;
goto out;
}
} else { } else {
/* am is almost a noop, except that we map 12:01 am to 0:01 */ /* am is almost a noop, except that we map 12:01 am to 0:01 */
if (ak->hour > 12) if (ak->hour > 12) {
return -1; code = -1;
goto out;
}
if (ak->hour == 12) if (ak->hour == 12)
ak->hour = 0; ak->hour = 0;
} }
} }
} }
return 0; out:
LocalFreeTokens(tt);
return code;
} }
/* ktime_DisplayString /* ktime_DisplayString