]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
Have strftime extend the output buffer
authorArran Cudbard-Bell <a.cudbardb@freeradius.org>
Sat, 6 Mar 2021 21:43:31 +0000 (21:43 +0000)
committerArran Cudbard-Bell <a.cudbardb@freeradius.org>
Sun, 7 Mar 2021 23:01:35 +0000 (23:01 +0000)
src/lib/util/time.c

index 52d1d9e4a41b68edf4342cbc76d018893003c6fb..42989cb20e18c0023d3322c8f16d82a19fdb98e2 100644 (file)
@@ -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);