From: Tobias Stoeckmann Date: Fri, 16 May 2025 22:01:53 +0000 (+0200) Subject: tar: Make safe_fprintf more platform independent X-Git-Tag: v3.8.0~15^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F2609%2Fhead;p=thirdparty%2Flibarchive.git 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 --- 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)