From: Arran Cudbard-Bell Date: Sat, 6 Mar 2021 21:43:31 +0000 (+0000) Subject: Have strftime extend the output buffer X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a961a670c49c35a8addb66092a2ff0f188fe6a7d;p=thirdparty%2Ffreeradius-server.git Have strftime extend the output buffer --- diff --git a/src/lib/util/time.c b/src/lib/util/time.c index 52d1d9e4a41..42989cb20e1 100644 --- a/src/lib/util/time.c +++ b/src/lib/util/time.c @@ -543,6 +543,11 @@ done: DIAG_OFF(format-nonliteral) /** Copy a time string (local timezone) to an sbuff + * + * @note This function will attempt to extend the sbuff by double the length of + * the fmt string. It is recommended to either pre-extend the sbuff before + * calling this function, or avoid using format specifiers that expand to + * character strings longer than 4 bytes. * * @param[in] out Where to write the formatted time string. * @param[in] time Internal server time to convert to wallclock @@ -560,13 +565,18 @@ size_t fr_time_strftime_local(fr_sbuff_t *out, fr_time_t time, char const *fmt) localtime_r(&utime, &tm); - len = strftime(fr_sbuff_current(out), fr_sbuff_remaining(out), fmt, &tm); + len = strftime(fr_sbuff_current(out), fr_sbuff_extend_lowat(NULL, out, strlen(fmt) * 2), fmt, &tm); if (len == 0) return 0; return fr_sbuff_advance(out, len); } /** Copy a time string (UTC) to an sbuff + * + * @note This function will attempt to extend the sbuff by double the length of + * the fmt string. It is recommended to either pre-extend the sbuff before + * calling this function, or avoid using format specifiers that expand to + * character strings longer than 4 bytes. * * @param[in] out Where to write the formatted time string. * @param[in] time Internal server time to convert to wallclock @@ -584,7 +594,7 @@ size_t fr_time_strftime_utc(fr_sbuff_t *out, fr_time_t time, char const *fmt) gmtime_r(&utime, &tm); - len = strftime(fr_sbuff_current(out), fr_sbuff_remaining(out), fmt, &tm); + len = strftime(fr_sbuff_current(out), fr_sbuff_extend_lowat(NULL, out, strlen(fmt) * 2), fmt, &tm); if (len == 0) return 0; return fr_sbuff_advance(out, len);