w.deng | e87b500 | 2025-08-20 10:43:03 +0800 | [diff] [blame] | 1 | #ifndef __vasprintf_compat_h |
| 2 | #define __vasprintf_compat_h |
| 3 | |
| 4 | /** |
| 5 | * @file |
| 6 | * @brief Do not use, json-c internal, may be changed or removed at any time. |
| 7 | */ |
| 8 | |
| 9 | #include "snprintf_compat.h" |
| 10 | |
| 11 | #ifndef WIN32 |
| 12 | #include <stdarg.h> |
| 13 | #endif /* !defined(WIN32) */ |
| 14 | #include <stdint.h> |
| 15 | #include <stdlib.h> |
| 16 | |
| 17 | #if !defined(HAVE_VASPRINTF) |
| 18 | /* CAW: compliant version of vasprintf */ |
| 19 | static int vasprintf(char **buf, const char *fmt, va_list ap) |
| 20 | { |
| 21 | #ifndef WIN32 |
| 22 | static char _T_emptybuffer = '\0'; |
| 23 | va_list ap2; |
| 24 | #endif /* !defined(WIN32) */ |
| 25 | int chars; |
| 26 | char *b; |
| 27 | |
| 28 | if (!buf) |
| 29 | { |
| 30 | return -1; |
| 31 | } |
| 32 | |
| 33 | #ifdef WIN32 |
| 34 | chars = _vscprintf(fmt, ap); |
| 35 | #else /* !defined(WIN32) */ |
| 36 | /* CAW: RAWR! We have to hope to god here that vsnprintf doesn't overwrite |
| 37 | * our buffer like on some 64bit sun systems... but hey, it's time to move on |
| 38 | */ |
| 39 | va_copy(ap2, ap); |
| 40 | chars = vsnprintf(&_T_emptybuffer, 0, fmt, ap2); |
| 41 | va_end(ap2); |
| 42 | #endif /* defined(WIN32) */ |
| 43 | if (chars < 0 || (size_t)chars + 1 > SIZE_MAX / sizeof(char)) |
| 44 | { |
| 45 | return -1; |
| 46 | } |
| 47 | |
| 48 | b = (char *)malloc(sizeof(char) * ((size_t)chars + 1)); |
| 49 | if (!b) |
| 50 | { |
| 51 | return -1; |
| 52 | } |
| 53 | |
| 54 | if ((chars = vsprintf(b, fmt, ap)) < 0) |
| 55 | { |
| 56 | free(b); |
| 57 | } |
| 58 | else |
| 59 | { |
| 60 | *buf = b; |
| 61 | } |
| 62 | |
| 63 | return chars; |
| 64 | } |
| 65 | #endif /* !HAVE_VASPRINTF */ |
| 66 | |
| 67 | #endif /* __vasprintf_compat_h */ |