From e99bef4a271eb2afbd279bea51c43747aee2ca45 Mon Sep 17 00:00:00 2001
From: divverent <divverent@d7cf8633-e32d-0410-b094-e92efae38249>
Date: Fri, 20 Apr 2012 08:46:33 +0000
Subject: [PATCH] VM_sprintf: use intmax_t for integer types

git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@11810 d7cf8633-e32d-0410-b094-e92efae38249
::stable-branch::merge=08798068fc28f075e3c8b6abb5bee2bbda3c25a8
---
 host.c      |  1 +
 prvm_cmds.c | 15 +++++++++++----
 quakedef.h  | 10 ++++++++++
 3 files changed, 22 insertions(+), 4 deletions(-)

diff --git a/host.c b/host.c
index ef7ee408..72ed8b98 100644
--- a/host.c
+++ b/host.c
@@ -97,6 +97,7 @@ Host_AbortCurrentFrame
 aborts the current host frame and goes on with the next one
 ================
 */
+void Host_AbortCurrentFrame(void) DP_FUNC_NORETURN;
 void Host_AbortCurrentFrame(void)
 {
 	// in case we were previously nice, make us mean again
diff --git a/prvm_cmds.c b/prvm_cmds.c
index f6f40ad5..f18bff24 100644
--- a/prvm_cmds.c
+++ b/prvm_cmds.c
@@ -6433,6 +6433,7 @@ void VM_sprintf(prvm_prog_t *prog)
 	const char *s, *s0;
 	char outbuf[MAX_INPUTLINE];
 	char *o = outbuf, *end = outbuf + sizeof(outbuf), *err;
+	const char *p;
 	int argpos = 1;
 	int width, precision, thisarg, flags;
 	char formatbuf[16];
@@ -6639,6 +6640,12 @@ nolength:
 					if(flags & PRINTF_LEFT) *f++ = '-';
 					if(flags & PRINTF_SPACEPOSITIVE) *f++ = ' ';
 					if(flags & PRINTF_SIGNPOSITIVE) *f++ = '+';
+					if(*s == 'd' || *s == 'i' || *s == 'o' || *s == 'u' || *s == 'x' || *s == 'X')
+					{
+						// make it use a good integer type
+						for(p = INT_LOSSLESS_FORMAT_SIZE; *p; )
+							*f++ = *p++;
+					}
 					*f++ = '*';
 					if(precision >= 0)
 					{
@@ -6655,15 +6662,15 @@ nolength:
 					{
 						case 'd': case 'i':
 							if(precision < 0) // not set
-								o += dpsnprintf(o, end - o, formatbuf, width, (isfloat ? (int) GETARG_FLOAT(thisarg) : (int) GETARG_INT(thisarg)));
+								o += dpsnprintf(o, end - o, formatbuf, width, (isfloat ? INT_LOSSLESS_FORMAT_CONVERT_S(GETARG_FLOAT(thisarg)) : INT_LOSSLESS_FORMAT_CONVERT_S(GETARG_INT(thisarg))));
 							else
-								o += dpsnprintf(o, end - o, formatbuf, width, precision, (isfloat ? (int) GETARG_FLOAT(thisarg) : (int) GETARG_INT(thisarg)));
+								o += dpsnprintf(o, end - o, formatbuf, width, precision, (isfloat ? INT_LOSSLESS_FORMAT_CONVERT_S(GETARG_FLOAT(thisarg)) : INT_LOSSLESS_FORMAT_CONVERT_S(GETARG_INT(thisarg))));
 							break;
 						case 'o': case 'u': case 'x': case 'X':
 							if(precision < 0) // not set
-								o += dpsnprintf(o, end - o, formatbuf, width, (isfloat ? (unsigned int) GETARG_FLOAT(thisarg) : (unsigned int) GETARG_INT(thisarg)));
+								o += dpsnprintf(o, end - o, formatbuf, width, (isfloat ? INT_LOSSLESS_FORMAT_CONVERT_U(GETARG_FLOAT(thisarg)) : INT_LOSSLESS_FORMAT_CONVERT_U(GETARG_INT(thisarg))));
 							else
-								o += dpsnprintf(o, end - o, formatbuf, width, precision, (isfloat ? (unsigned int) GETARG_FLOAT(thisarg) : (unsigned int) GETARG_INT(thisarg)));
+								o += dpsnprintf(o, end - o, formatbuf, width, precision, (isfloat ? INT_LOSSLESS_FORMAT_CONVERT_U(GETARG_FLOAT(thisarg)) : INT_LOSSLESS_FORMAT_CONVERT_U(GETARG_INT(thisarg))));
 							break;
 						case 'e': case 'E': case 'f': case 'F': case 'g': case 'G':
 							if(precision < 0) // not set
diff --git a/quakedef.h b/quakedef.h
index fd0f253e..afa8aaa7 100644
--- a/quakedef.h
+++ b/quakedef.h
@@ -563,5 +563,15 @@ void Sys_Shared_Init(void);
 #define VECTOR_LOSSLESS_FORMAT "%.9g %.9g %.9g"
 #endif
 
+#ifdef _MSC_VER
+#define INT_LOSSLESS_FORMAT_SIZE "I64"
+#define INT_LOSSLESS_FORMAT_CONVERT_S(x) ((__int64)(x))
+#define INT_LOSSLESS_FORMAT_CONVERT_U(x) ((unsigned __int64)(x))
+#else
+#define INT_LOSSLESS_FORMAT_SIZE "j"
+#define INT_LOSSLESS_FORMAT_CONVERT_S(x) ((intmax_t)(x))
+#define INT_LOSSLESS_FORMAT_CONVERT_U(x) ((uintmax_t)(x))
+#endif
+
 #endif
 
-- 
2.39.5