1
0
mirror of https://git.ffmpeg.org/ffmpeg.git synced 2026-10-24 02:09:03 +03:00

avutil/bprint: fix av_bprint_strftime with %p format string reporting truncated output

strftime returns 0 in case of an empty output (e.g. %p format string with some
locales), there is no way to distinguish this from a buffer-too-small error
condition. So we must use some heuristics to handle this case, and not consume
INT_MAX RAM and falsely report a truncated output.

Signed-off-by: Marton Balint <cus@passwd.hu>
This commit is contained in:
Marton Balint
2025-08-24 21:35:41 +02:00
parent 50e94aadb2
commit 2296a9c1bc

View File

@@ -167,6 +167,7 @@ void av_bprint_strftime(AVBPrint *buf, const char *fmt, const struct tm *tm)
{
unsigned room;
size_t l;
size_t fmt_len = strlen(fmt);
if (!*fmt)
return;
@@ -174,9 +175,18 @@ void av_bprint_strftime(AVBPrint *buf, const char *fmt, const struct tm *tm)
room = av_bprint_room(buf);
if (room && (l = strftime(buf->str + buf->len, room, fmt, tm)))
break;
/* Due to the limitations of strftime() it is not possible to know if
* the output buffer is too small or the output is empty.
* However, a 256x output space requirement compared to the format
* string length is so unlikely we can safely assume empty output. This
* allows supporting possibly empty format strings like "%p". */
if (room >> 8 > fmt_len)
break;
/* strftime does not tell us how much room it would need: let us
retry with twice as much until the buffer is large enough */
room = !room ? strlen(fmt) + 1 :
room = !room ? fmt_len + 1 :
room <= INT_MAX / 2 ? room * 2 : INT_MAX;
if (av_bprint_alloc(buf, room)) {
/* impossible to grow, try to manage something useful anyway */