[quake3-commits] r1899 - in trunk/code: qcommon sys

DONOTREPLY at icculus.org DONOTREPLY at icculus.org
Wed Feb 23 11:17:09 EST 2011


Author: thilo
Date: 2011-02-23 11:17:09 -0500 (Wed, 23 Feb 2011)
New Revision: 1899

Modified:
   trunk/code/qcommon/q_shared.c
   trunk/code/qcommon/q_shared.h
   trunk/code/sys/sys_main.c
Log:
- Fix unterminated string errors in Q_vsnprintf() on windows. Thanks to Eugene C. for reporting (#4907)
- Get rid of bigbuffer in Com_sprintf()
- Get rid of Q_snprintf and replace with Com_sprintf()


Modified: trunk/code/qcommon/q_shared.c
===================================================================
--- trunk/code/qcommon/q_shared.c	2011-02-18 23:46:02 UTC (rev 1898)
+++ trunk/code/qcommon/q_shared.c	2011-02-23 16:17:09 UTC (rev 1899)
@@ -726,8 +726,41 @@
 	return (int)f == f;
 }
 
+#ifdef _MSC_VER
 /*
 =============
+Q_vsnprintf
+ 
+Special wrapper function for Microsoft's broken _vsnprintf() function.
+MinGW comes with its own snprintf() which is not broken.
+=============
+*/
+
+int Q_vsnprintf(char *str, size_t size, const char *format, va_list ap);
+{
+	int retval;
+	
+	retval = _vsnprintf(str, size, format, ap);
+
+	if(retval < 0 || retval == size)
+	{
+		// Microsoft doesn't adhere to the C99 standard of vsnprintf,
+		// which states that the return value must be the number of
+		// bytes written if the output string had sufficient length.
+		//
+		// Obviously we cannot determine that value from Microsoft's
+		// implementation, so we have no choice but to return size.
+		
+		str[size - 1] = '\0';
+		return size;
+	}
+	
+	return retval;
+}
+#endif
+
+/*
+=============
 Q_strncpyz
  
 Safe strncpy that ensures a trailing zero
@@ -934,29 +967,19 @@
 	return count;
 }
 
-void QDECL Com_sprintf( char *dest, int size, const char *fmt, ...) {
+void QDECL Com_sprintf(char *dest, int size, const char *fmt, ...)
+{
 	int		len;
 	va_list		argptr;
-	char	bigbuffer[32000];	// big, but small enough to fit in PPC stack
 
 	va_start (argptr,fmt);
-	len = Q_vsnprintf (bigbuffer, sizeof(bigbuffer), fmt,argptr);
+	len = Q_vsnprintf(dest, size, fmt, argptr);
 	va_end (argptr);
-	if ( len >= sizeof( bigbuffer ) ) {
-		Com_Error( ERR_FATAL, "Com_sprintf: overflowed bigbuffer" );
-	}
-	if (len >= size) {
-		Com_Printf ("Com_sprintf: overflow of %i in %i\n", len, size);
-#ifdef	_DEBUG
-		__asm {
-			int 3;
-		}
-#endif
-	}
-	Q_strncpyz (dest, bigbuffer, size );
+
+	if(len >= size)
+		Com_Printf("Com_sprintf: Output length %d too short, require %d bytes.\n", size, len);
 }
 
-
 /*
 ============
 va

Modified: trunk/code/qcommon/q_shared.h
===================================================================
--- trunk/code/qcommon/q_shared.h	2011-02-18 23:46:02 UTC (rev 1898)
+++ trunk/code/qcommon/q_shared.h	2011-02-23 16:17:09 UTC (rev 1899)
@@ -132,16 +132,6 @@
 #include <ctype.h>
 #include <limits.h>
 
-// vsnprintf is ISO/IEC 9899:1999
-// abstracting this to make it portable
-#ifdef _WIN32
-  #define Q_vsnprintf _vsnprintf
-  #define Q_snprintf _snprintf
-#else
-  #define Q_vsnprintf vsnprintf
-  #define Q_snprintf snprintf
-#endif
-
 #ifdef _MSC_VER
   #include <io.h>
 
@@ -153,8 +143,14 @@
   typedef unsigned __int32 uint32_t;
   typedef unsigned __int16 uint16_t;
   typedef unsigned __int8 uint8_t;
+
+  // vsnprintf is ISO/IEC 9899:1999
+  // abstracting this to make it portable
+  int Q_vsnprintf(char *str, size_t size, const char *format, va_list ap);
 #else
   #include <stdint.h>
+
+  #define Q_vsnprintf vsnprintf
 #endif
 
 #endif

Modified: trunk/code/sys/sys_main.c
===================================================================
--- trunk/code/sys/sys_main.c	2011-02-18 23:46:02 UTC (rev 1898)
+++ trunk/code/sys/sys_main.c	2011-02-23 16:17:09 UTC (rev 1899)
@@ -425,7 +425,7 @@
 
 	assert( name );
 
-	Q_snprintf (fname, sizeof(fname), "%s" ARCH_STRING DLL_EXT, name);
+	Com_sprintf(fname, sizeof(fname), "%s" ARCH_STRING DLL_EXT, name);
 
 	netpath = FS_FindDll(fname);
 



More information about the quake3-commits mailing list