From c2d06848ea34814fc6b227ce97015474c7b71d10 Mon Sep 17 00:00:00 2001 From: Tobias Stoeckmann Date: Sat, 17 May 2025 00:01:53 +0200 Subject: [PATCH] tar: Make safe_fprintf more platform independent If vsnprintf fails with errno EOVERFLOW, the results are very platform dependent but never useful. The implementation in glibc fills bytes with blanks, FreeBSD fills them with zeros, OpenBSD and Windows set first byte to '\0'. Just stop processing and don't print anything, which makes it follow the OpenBSD and Windows approach. Signed-off-by: Tobias Stoeckmann --- tar/util.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tar/util.c b/tar/util.c index 1321a6839..c99f67797 100644 --- a/tar/util.c +++ b/tar/util.c @@ -96,6 +96,10 @@ safe_fprintf(FILE * restrict f, const char * restrict fmt, ...) length = vsnprintf(fmtbuff, fmtbuff_length, fmt, ap); va_end(ap); + /* If vsnprintf will always fail, stop early. */ + if (length < 0 && errno == EOVERFLOW) + return; + /* If the result was too large, allocate a buffer on the heap. */ while (length < 0 || (size_t)length >= fmtbuff_length) { if (length >= 0 && (size_t)length >= fmtbuff_length) -- 2.47.2