Skip to content
This repository was archived by the owner on Jun 14, 2019. It is now read-only.

Commit 150bffd

Browse files
am11mgreter
authored andcommitted
Windows: Add guard for snprintf.
Also fixes the warning for unsigned char*.
1 parent c757e9c commit 150bffd

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

json.cpp

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,36 @@
2929
#include <stdlib.h>
3030
#include <string.h>
3131

32+
#ifdef _MSC_VER
33+
34+
#include <stdarg.h>
35+
#define snprintf c99_snprintf
36+
37+
inline int c99_vsnprintf(char* str, size_t size, const char* format, va_list ap)
38+
{
39+
int count = -1;
40+
41+
if (size != 0)
42+
count = _vsnprintf_s(str, size, _TRUNCATE, format, ap);
43+
if (count == -1)
44+
count = _vscprintf(format, ap);
45+
46+
return count;
47+
}
48+
49+
inline int c99_snprintf(char* str, size_t size, const char* format, ...)
50+
{
51+
int count;
52+
va_list ap;
53+
54+
va_start(ap, format);
55+
count = c99_vsnprintf(str, size, format, ap);
56+
va_end(ap);
57+
58+
return count;
59+
}
60+
#endif // _MSC_VER
61+
3262
#define out_of_memory() do { \
3363
fprintf(stderr, "Out of memory.\n"); \
3464
exit(EXIT_FAILURE); \
@@ -1172,9 +1202,9 @@ void emit_string(SB *out, const char *str)
11721202
strcpy(b, "\\uFFFD");
11731203
b += 6;
11741204
} else {
1175-
*b++ = 0xEF;
1176-
*b++ = 0xBF;
1177-
*b++ = 0xBD;
1205+
*b++ = 0xEFu;
1206+
*b++ = 0xBFu;
1207+
*b++ = 0xBDu;
11781208
}
11791209
s++;
11801210
} else if (c < 0x1F || (c >= 0x80 && escape_unicode)) {
@@ -1378,4 +1408,4 @@ bool json_check(const JsonNode *node, char errmsg[256])
13781408
return true;
13791409

13801410
#undef problem
1381-
}
1411+
}

0 commit comments

Comments
 (0)