You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

vasprintf_compat.h 1.3 kB

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