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.

json_util.c 6.7 kB

Fixes various Wreturn-type and Wimplicit-fallthrough errors on Mingw-w64 This is a recent regression since commit 6359b798479d379a3202e02c6a938d9b40c0d856 which added various assert(0) calls (often replacing return-s). With Ming-W64 compiler, json-c build was failing with various errors of the sort: > /home/jehan/dev/src/json-c/json_object.c: In function 'json_object_int_inc': > /home/jehan/dev/src/json-c/json_object.c:841:1: error: control reaches end of non-void function [-Werror=return-type] > 841 | } > | ^ > In file included from /home/jehan/dev/src/json-c/json_object.c:17: > /home/jehan/dev/src/json-c/json_object.c: In function 'json_object_get_double': > /home/jehan/.local/share/crossroad/roads/w64/json-c/include/assert.h:76:4: error: this statement may fall through [-Werror=implicit-fallthrough=] > 76 | (_assert(#_Expression,__FILE__,__LINE__),0)) > | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > /home/jehan/dev/src/json-c/json_object.c:1070:7: note: in expansion of macro 'assert' > 1070 | assert(0); > | ^~~~~~ > /home/jehan/dev/src/json-c/json_object.c:1072:3: note: here > 1072 | case json_type_boolean: > | ^~~~ The problem is that Mingw-w64 does not consider assert() as a noreturn (even assert(0)), because it has to be compatible by Microsoft libraries. See the discussion here: https://sourceforge.net/p/mingw-w64/bugs/306/ Instead let's create a new json_abort() function which is basically just an abort() function with an optional message, for such cases where abortion was non-conditional (using assert() and using the assertion condition as a message here was clearly a misuse of the function). And mark json_abort() as 'noreturn', as well as 'cold' for optimization purpose (this is code we expect to never run, unless there is a bug, that is). Finally let's use this json_abort() instead of previous misused assert() calls.
5 years ago
Fixes various Wreturn-type and Wimplicit-fallthrough errors on Mingw-w64 This is a recent regression since commit 6359b798479d379a3202e02c6a938d9b40c0d856 which added various assert(0) calls (often replacing return-s). With Ming-W64 compiler, json-c build was failing with various errors of the sort: > /home/jehan/dev/src/json-c/json_object.c: In function 'json_object_int_inc': > /home/jehan/dev/src/json-c/json_object.c:841:1: error: control reaches end of non-void function [-Werror=return-type] > 841 | } > | ^ > In file included from /home/jehan/dev/src/json-c/json_object.c:17: > /home/jehan/dev/src/json-c/json_object.c: In function 'json_object_get_double': > /home/jehan/.local/share/crossroad/roads/w64/json-c/include/assert.h:76:4: error: this statement may fall through [-Werror=implicit-fallthrough=] > 76 | (_assert(#_Expression,__FILE__,__LINE__),0)) > | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > /home/jehan/dev/src/json-c/json_object.c:1070:7: note: in expansion of macro 'assert' > 1070 | assert(0); > | ^~~~~~ > /home/jehan/dev/src/json-c/json_object.c:1072:3: note: here > 1072 | case json_type_boolean: > | ^~~~ The problem is that Mingw-w64 does not consider assert() as a noreturn (even assert(0)), because it has to be compatible by Microsoft libraries. See the discussion here: https://sourceforge.net/p/mingw-w64/bugs/306/ Instead let's create a new json_abort() function which is basically just an abort() function with an optional message, for such cases where abortion was non-conditional (using assert() and using the assertion condition as a message here was clearly a misuse of the function). And mark json_abort() as 'noreturn', as well as 'cold' for optimization purpose (this is code we expect to never run, unless there is a bug, that is). Finally let's use this json_abort() instead of previous misused assert() calls.
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. * $Id: json_util.c,v 1.4 2006/01/30 23:07:57 mclark Exp $
  3. *
  4. * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
  5. * Michael Clark <michael@metaparadigm.com>
  6. *
  7. * This library is free software; you can redistribute it and/or modify
  8. * it under the terms of the MIT license. See COPYING for details.
  9. *
  10. */
  11. #include "config.h"
  12. #undef realloc
  13. #include "strerror_override.h"
  14. #include <ctype.h>
  15. #include <limits.h>
  16. #include <stdarg.h>
  17. #include <stddef.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #ifdef HAVE_SYS_TYPES_H
  22. #include <sys/types.h>
  23. #endif /* HAVE_SYS_TYPES_H */
  24. #ifdef HAVE_SYS_STAT_H
  25. #include <sys/stat.h>
  26. #endif /* HAVE_SYS_STAT_H */
  27. #ifdef HAVE_FCNTL_H
  28. #include <fcntl.h>
  29. #endif /* HAVE_FCNTL_H */
  30. #ifdef HAVE_UNISTD_H
  31. #include <unistd.h>
  32. #endif /* HAVE_UNISTD_H */
  33. #ifdef WIN32
  34. #if MSC_VER < 1800
  35. /* strtoll/strtoull is available only since Visual Studio 2013 */
  36. #define strtoll _strtoi64
  37. #define strtoull _strtoui64
  38. #endif
  39. #define WIN32_LEAN_AND_MEAN
  40. #include <io.h>
  41. #include <windows.h>
  42. #endif /* defined(WIN32) */
  43. #if !defined(HAVE_OPEN) && defined(WIN32)
  44. #define open _open
  45. #endif
  46. #include "snprintf_compat.h"
  47. #include "debug.h"
  48. #include "json_inttypes.h"
  49. #include "json_object.h"
  50. #include "json_tokener.h"
  51. #include "json_util.h"
  52. #include "printbuf.h"
  53. static int _json_object_to_fd(int fd, struct json_object *obj, int flags, const char *filename);
  54. static char _last_err[256] = "";
  55. const char *json_util_get_last_err()
  56. {
  57. if (_last_err[0] == '\0')
  58. return NULL;
  59. return _last_err;
  60. }
  61. void _json_c_set_last_err(const char *err_fmt, ...)
  62. {
  63. va_list ap;
  64. va_start(ap, err_fmt);
  65. // Ignore (attempted) overruns from snprintf
  66. (void)vsnprintf(_last_err, sizeof(_last_err), err_fmt, ap);
  67. va_end(ap);
  68. }
  69. struct json_object *json_object_from_fd(int fd)
  70. {
  71. return json_object_from_fd_ex(fd, -1);
  72. }
  73. struct json_object *json_object_from_fd_ex(int fd, int in_depth)
  74. {
  75. struct printbuf *pb;
  76. struct json_object *obj;
  77. char buf[JSON_FILE_BUF_SIZE];
  78. int ret;
  79. int depth = JSON_TOKENER_DEFAULT_DEPTH;
  80. json_tokener *tok;
  81. if (!(pb = printbuf_new()))
  82. {
  83. _json_c_set_last_err("json_object_from_file: printbuf_new failed\n");
  84. return NULL;
  85. }
  86. if (in_depth != -1)
  87. depth = in_depth;
  88. tok = json_tokener_new_ex(depth);
  89. if (!tok)
  90. {
  91. _json_c_set_last_err(
  92. "json_object_from_fd: unable to allocate json_tokener(depth=%d): %s\n", depth,
  93. strerror(errno));
  94. printbuf_free(pb);
  95. return NULL;
  96. }
  97. while ((ret = read(fd, buf, JSON_FILE_BUF_SIZE)) > 0)
  98. {
  99. printbuf_memappend(pb, buf, ret);
  100. }
  101. if (ret < 0)
  102. {
  103. _json_c_set_last_err("json_object_from_fd: error reading fd %d: %s\n", fd,
  104. strerror(errno));
  105. json_tokener_free(tok);
  106. printbuf_free(pb);
  107. return NULL;
  108. }
  109. obj = json_tokener_parse_ex(tok, pb->buf, printbuf_length(pb));
  110. if (obj == NULL)
  111. _json_c_set_last_err("json_tokener_parse_ex failed: %s\n",
  112. json_tokener_error_desc(json_tokener_get_error(tok)));
  113. json_tokener_free(tok);
  114. printbuf_free(pb);
  115. return obj;
  116. }
  117. struct json_object *json_object_from_file(const char *filename)
  118. {
  119. struct json_object *obj;
  120. int fd;
  121. if ((fd = open(filename, O_RDONLY)) < 0)
  122. {
  123. _json_c_set_last_err("json_object_from_file: error opening file %s: %s\n", filename,
  124. strerror(errno));
  125. return NULL;
  126. }
  127. obj = json_object_from_fd(fd);
  128. close(fd);
  129. return obj;
  130. }
  131. /* extended "format and write to file" function */
  132. int json_object_to_file_ext(const char *filename, struct json_object *obj, int flags)
  133. {
  134. int fd, ret;
  135. int saved_errno;
  136. if (!obj)
  137. {
  138. _json_c_set_last_err("json_object_to_file: object is null\n");
  139. return -1;
  140. }
  141. if ((fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, 0644)) < 0)
  142. {
  143. _json_c_set_last_err("json_object_to_file: error opening file %s: %s\n", filename,
  144. strerror(errno));
  145. return -1;
  146. }
  147. ret = _json_object_to_fd(fd, obj, flags, filename);
  148. saved_errno = errno;
  149. close(fd);
  150. errno = saved_errno;
  151. return ret;
  152. }
  153. int json_object_to_fd(int fd, struct json_object *obj, int flags)
  154. {
  155. if (!obj)
  156. {
  157. _json_c_set_last_err("json_object_to_fd: object is null\n");
  158. return -1;
  159. }
  160. return _json_object_to_fd(fd, obj, flags, NULL);
  161. }
  162. static int _json_object_to_fd(int fd, struct json_object *obj, int flags, const char *filename)
  163. {
  164. int ret;
  165. const char *json_str;
  166. unsigned int wpos, wsize;
  167. filename = filename ? filename : "(fd)";
  168. if (!(json_str = json_object_to_json_string_ext(obj, flags)))
  169. {
  170. return -1;
  171. }
  172. /* CAW: probably unnecessary, but the most 64bit safe */
  173. wsize = (unsigned int)(strlen(json_str) & UINT_MAX);
  174. wpos = 0;
  175. while (wpos < wsize)
  176. {
  177. if ((ret = write(fd, json_str + wpos, wsize - wpos)) < 0)
  178. {
  179. _json_c_set_last_err("json_object_to_file: error writing file %s: %s\n",
  180. filename, strerror(errno));
  181. return -1;
  182. }
  183. /* because of the above check for ret < 0, we can safely cast and add */
  184. wpos += (unsigned int)ret;
  185. }
  186. return 0;
  187. }
  188. // backwards compatible "format and write to file" function
  189. int json_object_to_file(const char *filename, struct json_object *obj)
  190. {
  191. return json_object_to_file_ext(filename, obj, JSON_C_TO_STRING_PLAIN);
  192. }
  193. int json_parse_double(const char *buf, double *retval)
  194. {
  195. char *end;
  196. *retval = strtod(buf, &end);
  197. return end == buf ? 1 : 0;
  198. }
  199. int json_parse_int64(const char *buf, int64_t *retval)
  200. {
  201. char *end = NULL;
  202. int64_t val;
  203. errno = 0;
  204. val = strtoll(buf, &end, 10);
  205. if (end != buf)
  206. *retval = val;
  207. return ((val == 0 && errno != 0) || (end == buf)) ? 1 : 0;
  208. }
  209. int json_parse_uint64(const char *buf, uint64_t *retval)
  210. {
  211. char *end = NULL;
  212. uint64_t val;
  213. errno = 1;
  214. while (*buf == ' ')
  215. {
  216. buf++;
  217. }
  218. if (*buf == '-')
  219. errno = 0;
  220. val = strtoull(buf, &end, 10);
  221. if (end != buf)
  222. *retval = val;
  223. return ((errno == 0) || (end == buf)) ? 1 : 0;
  224. }
  225. #ifndef HAVE_REALLOC
  226. void *rpl_realloc(void *p, size_t n)
  227. {
  228. if (n == 0)
  229. n = 1;
  230. if (p == 0)
  231. return malloc(n);
  232. return realloc(p, n);
  233. }
  234. #endif
  235. #define NELEM(a) (sizeof(a) / sizeof(a[0]))
  236. /* clang-format off */
  237. static const char *json_type_name[] = {
  238. /* If you change this, be sure to update the enum json_type definition too */
  239. "null",
  240. "boolean",
  241. "double",
  242. "int",
  243. "object",
  244. "array",
  245. "string",
  246. };
  247. /* clang-format on */
  248. const char *json_type_to_name(enum json_type o_type)
  249. {
  250. int o_type_int = (int)o_type;
  251. if (o_type_int < 0 || o_type_int >= (int)NELEM(json_type_name))
  252. {
  253. _json_c_set_last_err("json_type_to_name: type %d is out of range [0,%d]\n", o_type,
  254. NELEM(json_type_name));
  255. return NULL;
  256. }
  257. return json_type_name[o_type];
  258. }
  259. void json_abort(const char *message)
  260. {
  261. if (message != NULL)
  262. fprintf(stderr, "json-c aborts with error: %s\n", message);
  263. abort();
  264. }