From e96ff94936991672991da93953cad4dc0318e6e0 Mon Sep 17 00:00:00 2001 From: P. J. McDermott Date: Tue, 22 Mar 2022 16:37:55 -0400 Subject: datetime: Copy time-only & misc formats to buffer --- (limited to 'src/datetime.c') diff --git a/src/datetime.c b/src/datetime.c index 57bae20..ffc3329 100644 --- a/src/datetime.c +++ b/src/datetime.c @@ -115,6 +115,8 @@ datetime_parse(struct tm *now_tm, const char *input, time_t *arg_sec) { int date_fmt_len; int time_fmt_len; + int misc_fmt_len; + int fmt_len; char *fmt_buf; size_t d; size_t t; @@ -125,7 +127,12 @@ datetime_parse(struct tm *now_tm, const char *input, time_t *arg_sec) date_fmt_len = strlen(FORMATS_DATE[0]); time_fmt_len = strlen(FORMATS_TIME[0]); - fmt_buf = calloc(date_fmt_len + time_fmt_len + 1, sizeof(*fmt_buf)); + misc_fmt_len = strlen(FORMATS_MISC[0]); + fmt_len = date_fmt_len + time_fmt_len; + if (misc_fmt_len > fmt_len) { + fmt_len = misc_fmt_len; + } + fmt_buf = calloc(fmt_len + 1, sizeof(*fmt_buf)); if (fmt_buf == NULL) { fprintf(stderr, "Failed to allocate buffer: %s\n", strerror(errno)); @@ -135,8 +142,9 @@ datetime_parse(struct tm *now_tm, const char *input, time_t *arg_sec) sec = *arg_sec; /* GCC is dumb. */ got = false; for (t = 0; t < sizeof(FORMATS_TIME) / sizeof(FORMATS_TIME[0]) ; ++t) { + memcpy(fmt_buf, FORMATS_TIME[t], time_fmt_len); _datetime_reset_tm(&arg_tm); - end = strptime(input, FORMATS_TIME[t], &arg_tm); + end = strptime(input, fmt_buf, &arg_tm); if (end != NULL && *end == '\0') { _datetime_normalize(now_tm, &arg_tm, arg_sec); if (datetime_diff_epoch(*arg_sec) >= 0) { @@ -192,8 +200,9 @@ datetime_parse(struct tm *now_tm, const char *input, time_t *arg_sec) } } for (d = 0; d < sizeof(FORMATS_MISC) / sizeof(FORMATS_MISC[0]) ; ++d) { + memcpy(fmt_buf, FORMATS_MISC[d], misc_fmt_len); _datetime_reset_tm(&arg_tm); - end = strptime(input, FORMATS_MISC[d], &arg_tm); + end = strptime(input, fmt_buf, &arg_tm); if (end != NULL && *end == '\0') { _datetime_normalize(now_tm, &arg_tm, arg_sec); if (datetime_diff_epoch(*arg_sec) >= 0) { -- cgit v0.9.1