[FFmpeg-devel] [PATCH] bprint: reimplement vsnprintf for win32 without _vscprintf.

Nicolas George nicolas.george at normalesup.org
Mon Jul 23 00:41:04 CEST 2012


_vscprintf is not available on older windows versions
that we want to support.

The problem with windows' vsnprintf is that, on overflow,
it returns -1 instead of the length that would have been written.

This version prints the data into a large local buffer,
and copy it back. As bprint is designed for small strings that
accumulate into something possibly big, this should work.
If the single print still overflows the large buffer,
it truncates it silently.

Signed-off-by: Nicolas George <nicolas.george at normalesup.org>
---
 libavutil/bprint.c |   29 +++++++++++++++++++++--------
 1 file changed, 21 insertions(+), 8 deletions(-)

diff --git a/libavutil/bprint.c b/libavutil/bprint.c
index 6e59f6b..0da3906 100644
--- a/libavutil/bprint.c
+++ b/libavutil/bprint.c
@@ -30,14 +30,27 @@
 
 static int vsnprintf_fixed(char *s, size_t n, const char *format, va_list va)
 {
-    va_list va2;
-    int r;
-
-    va_copy(va2, va);
-    r = vsnprintf(s, n, format, va2);
-    va_end(va2);
-    if (r == -1)
-        r = _vscprintf(format, va);
+    /* Since _vscprintf is not available on old versions, print into a large
+       buffer and copy to the target. */
+    int r, c;
+    char buf[16384]; /* 16k ought to be enough for everybody */
+
+    if (n > sizeof(buf)) {
+        r = vsnprintf(s, n, format, va);
+        if (r < 0) {
+            r = n - 1;
+            s[r] = 0;
+        }
+    } else {
+        r = vsnprintf(buf, sizeof(buf), format, va);
+        if (r < 0)
+            r = sizeof(buf);
+        if (n > 0) {
+            c = FFMIN(r, n - 1);
+            memcpy(s, buf, c);
+            s[c] = 0;
+        }
+    }
     return r;
 }
 
-- 
1.7.10.4



More information about the ffmpeg-devel mailing list