From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.ffmpeg.org (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTPS id 2AA864BB73 for ; Sun, 24 Aug 2025 20:37:05 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.ffmpeg.org (Postfix) with ESMTP id 7E55668D48E; Sun, 24 Aug 2025 23:36:54 +0300 (EEST) Received: from 0f4167fb2350 (code.ffmpeg.org [188.245.149.3]) by ffbox0-bg.ffmpeg.org (Postfix) with ESMTPS id 1F9B768CFC1 for ; Sun, 24 Aug 2025 23:36:52 +0300 (EEST) MIME-Version: 1.0 To: ffmpeg-devel@ffmpeg.org Subject: [FFmpeg-devel] =?utf-8?q?=5BPATCH=5D_avutil/bprint=3A_fix_av=5Fb?= =?utf-8?q?print=5Fstrftime_with_=25p_format_string_reporting_truncated_ou?= =?utf-8?q?tput_=28PR_=2320330=29?= X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Marton Balint via ffmpeg-devel Reply-To: FFmpeg development discussions and patches Cc: Marton Balint Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Message-Id: <20250824203654.7E55668D48E@ffbox0-bg.ffmpeg.org> Date: Sun, 24 Aug 2025 23:36:54 +0300 (EEST) Archived-At: List-Archive: List-Post: PR #20330 opened by Marton Balint (cus) URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/20330 Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/20330.patch And a minor factorization. >From f8e83bce6269c95fbad90f34434ceb641bf753d5 Mon Sep 17 00:00:00 2001 From: Marton Balint Date: Sun, 24 Aug 2025 21:42:54 +0200 Subject: [PATCH 1/2] avutil/bprint: make av_bprintf use av_vbprintf No reason to duplicate the code. Signed-off-by: Marton Balint --- libavutil/bprint.c | 33 +++++++++------------------------ 1 file changed, 9 insertions(+), 24 deletions(-) diff --git a/libavutil/bprint.c b/libavutil/bprint.c index 4e9571715c..932c03ce50 100644 --- a/libavutil/bprint.c +++ b/libavutil/bprint.c @@ -96,35 +96,12 @@ void av_bprint_init_for_buffer(AVBPrint *buf, char *buffer, unsigned size) *buf->str = 0; } -void av_bprintf(AVBPrint *buf, const char *fmt, ...) -{ - unsigned room; - char *dst; - va_list vl; - int extra_len; - - while (1) { - room = av_bprint_room(buf); - dst = room ? buf->str + buf->len : NULL; - va_start(vl, fmt); - extra_len = vsnprintf(dst, room, fmt, vl); - va_end(vl); - if (extra_len <= 0) - return; - if (extra_len < room) - break; - if (av_bprint_alloc(buf, extra_len)) - break; - } - av_bprint_grow(buf, extra_len); -} - void av_vbprintf(AVBPrint *buf, const char *fmt, va_list vl_arg) { unsigned room; char *dst; - int extra_len; va_list vl; + int extra_len; while (1) { room = av_bprint_room(buf); @@ -142,6 +119,14 @@ void av_vbprintf(AVBPrint *buf, const char *fmt, va_list vl_arg) av_bprint_grow(buf, extra_len); } +void av_bprintf(AVBPrint *buf, const char *fmt, ...) +{ + va_list vl; + va_start(vl, fmt); + av_vbprintf(buf, fmt, vl); + va_end(vl); +} + void av_bprint_chars(AVBPrint *buf, char c, unsigned n) { unsigned room, real_n; -- 2.49.1 >From 4532caf820dcb2bc2cbac3d4e782ad27cf9fd76b Mon Sep 17 00:00:00 2001 From: Marton Balint Date: Sun, 24 Aug 2025 21:35:41 +0200 Subject: [PATCH 2/2] 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 --- libavutil/bprint.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/libavutil/bprint.c b/libavutil/bprint.c index 932c03ce50..fa244b2e04 100644 --- a/libavutil/bprint.c +++ b/libavutil/bprint.c @@ -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 = 0; if (!*fmt) return; @@ -174,9 +175,16 @@ 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; + + if (!fmt_len) + fmt_len = strlen(fmt); + /* A 256x space requirement for output is unlikely, so it is likely empty */ + 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 */ -- 2.49.1 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".