ソースを参照

libcompat: Fix vsnprintf broken behaviour

Change size and nr types from unsigled long to size_t. Always return
the amount that would be written regardless of any truncation. Do not
read one byte less from the temporary file.
Guillem Jover 18 年 前
コミット
808341c8be
共有2 個のファイルを変更した16 個の追加13 個の削除を含む
  1. 7 0
      ChangeLog
  2. 9 13
      libcompat/vsnprintf.c

+ 7 - 0
ChangeLog

@@ -1,3 +1,10 @@
+2008-09-14  Guillem Jover  <guillem@debian.org>
+
+	* libcompat/vsnprintf.c (vsnprintf): Change size and nr types from
+	unsigled long to size_t. Always return the amount that would be
+	written regardless of any truncation. Do not read one byte less from
+	the temporary file.
+
 2008-09-14  Guillem Jover  <guillem@debian.org>
 
 	* libcompat/vsnprintf.c (vsnprintf): Use the return value from

+ 9 - 13
libcompat/vsnprintf.c

@@ -30,8 +30,8 @@ vsnprintf(char *buf, size_t maxsize, const char *fmt, va_list al)
 {
 	static FILE *file = NULL;
 
-	unsigned long want, nr;
-	int total, retval;
+	size_t want, nr;
+	int total;
 
 	if (maxsize == 0)
 		return -1;
@@ -50,25 +50,21 @@ vsnprintf(char *buf, size_t maxsize, const char *fmt, va_list al)
 	total = vfprintf(file, fmt, al);
 	if (total < 0)
 		return -1;
+	if (total >= (int)maxsize)
+		want = maxsize - 1;
+	else
+		want = total;
 	if (fflush(file))
 		return -1;
 	if (fseek(file, 0, 0))
 		return -1;
 
-	want = total;
-	if (want >= maxsize) {
-		want = maxsize - 1;
-		retval = -1;
-	} else {
-		retval = want;
-	}
-
-	nr = fread(buf, 1, want - 1, file);
-	if (nr != want - 1)
+	nr = fread(buf, 1, want, file);
+	if (nr != want)
 		return -1;
 	buf[want] = '\0';
 
-	return retval;
+	return total;
 }
 #endif