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.6 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 <stdarg.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <stddef.h>
  18. #include <limits.h>
  19. #include <string.h>
  20. #include <ctype.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 <windows.h>
  41. # include <io.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 "printbuf.h"
  49. #include "json_inttypes.h"
  50. #include "json_object.h"
  51. #include "json_tokener.h"
  52. #include "json_util.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. _json_c_set_last_err("json_object_from_file: printbuf_new failed\n");
  83. return NULL;
  84. }
  85. if (in_depth != -1)
  86. depth = in_depth;
  87. tok = json_tokener_new_ex(depth);
  88. if (!tok)
  89. {
  90. _json_c_set_last_err("json_object_from_fd: unable to allocate json_tokener(depth=%d): %s\n", depth, strerror(errno));
  91. printbuf_free(pb);
  92. return NULL;
  93. }
  94. while((ret = read(fd, buf, JSON_FILE_BUF_SIZE)) > 0) {
  95. printbuf_memappend(pb, buf, ret);
  96. }
  97. if(ret < 0) {
  98. _json_c_set_last_err("json_object_from_fd: error reading fd %d: %s\n", fd, strerror(errno));
  99. json_tokener_free(tok);
  100. printbuf_free(pb);
  101. return NULL;
  102. }
  103. obj = json_tokener_parse_ex(tok, pb->buf, printbuf_length(pb));
  104. if (obj == NULL)
  105. _json_c_set_last_err("json_tokener_parse_ex failed: %s\n", json_tokener_error_desc(json_tokener_get_error(tok)));
  106. json_tokener_free(tok);
  107. printbuf_free(pb);
  108. return obj;
  109. }
  110. struct json_object* json_object_from_file(const char *filename)
  111. {
  112. struct json_object *obj;
  113. int fd;
  114. if((fd = open(filename, O_RDONLY)) < 0) {
  115. _json_c_set_last_err("json_object_from_file: error opening file %s: %s\n",
  116. filename, strerror(errno));
  117. return NULL;
  118. }
  119. obj = json_object_from_fd(fd);
  120. close(fd);
  121. return obj;
  122. }
  123. /* extended "format and write to file" function */
  124. int json_object_to_file_ext(const char *filename, struct json_object *obj, int flags)
  125. {
  126. int fd, ret;
  127. int saved_errno;
  128. if (!obj) {
  129. _json_c_set_last_err("json_object_to_file: object is null\n");
  130. return -1;
  131. }
  132. if ((fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, 0644)) < 0) {
  133. _json_c_set_last_err("json_object_to_file: error opening file %s: %s\n",
  134. filename, strerror(errno));
  135. return -1;
  136. }
  137. ret = _json_object_to_fd(fd, obj, flags, filename);
  138. saved_errno = errno;
  139. close(fd);
  140. errno = saved_errno;
  141. return ret;
  142. }
  143. int json_object_to_fd(int fd, struct json_object *obj, int flags)
  144. {
  145. if (!obj) {
  146. _json_c_set_last_err("json_object_to_fd: object is null\n");
  147. return -1;
  148. }
  149. return _json_object_to_fd(fd, obj, flags, NULL);
  150. }
  151. static int _json_object_to_fd(int fd, struct json_object *obj, int flags, const char *filename)
  152. {
  153. int ret;
  154. const char *json_str;
  155. unsigned int wpos, wsize;
  156. filename = filename ? filename : "(fd)";
  157. if (!(json_str = json_object_to_json_string_ext(obj,flags))) {
  158. return -1;
  159. }
  160. wsize = (unsigned int)(strlen(json_str) & UINT_MAX); /* CAW: probably unnecessary, but the most 64bit safe */
  161. wpos = 0;
  162. while(wpos < wsize) {
  163. if((ret = write(fd, json_str + wpos, wsize-wpos)) < 0) {
  164. _json_c_set_last_err("json_object_to_file: error writing file %s: %s\n",
  165. filename, strerror(errno));
  166. return -1;
  167. }
  168. /* because of the above check for ret < 0, we can safely cast and add */
  169. wpos += (unsigned int)ret;
  170. }
  171. return 0;
  172. }
  173. // backwards compatible "format and write to file" function
  174. int json_object_to_file(const char *filename, struct json_object *obj)
  175. {
  176. return json_object_to_file_ext(filename, obj, JSON_C_TO_STRING_PLAIN);
  177. }
  178. int json_parse_double(const char *buf, double *retval)
  179. {
  180. char *end;
  181. *retval = strtod(buf, &end);
  182. return end == buf ? 1 : 0;
  183. }
  184. int json_parse_int64(const char *buf, int64_t *retval)
  185. {
  186. char *end = NULL;
  187. int64_t val;
  188. errno = 0;
  189. val = strtoll(buf, &end, 10);
  190. if (end != buf)
  191. *retval = val;
  192. return ((val == 0 && errno != 0) || (end == buf)) ? 1 : 0;
  193. }
  194. int json_parse_uint64(const char *buf, uint64_t *retval)
  195. {
  196. char *end = NULL;
  197. uint64_t val;
  198. errno = 1;
  199. while (*buf == ' ') {
  200. buf++;
  201. }
  202. if (*buf == '-') errno = 0;
  203. val = strtoull(buf, &end, 10);
  204. if (end != buf)
  205. *retval = val;
  206. return ((errno == 0) || (end == buf)) ? 1 : 0;
  207. }
  208. #ifndef HAVE_REALLOC
  209. void* rpl_realloc(void* p, size_t n)
  210. {
  211. if (n == 0)
  212. n = 1;
  213. if (p == 0)
  214. return malloc(n);
  215. return realloc(p, n);
  216. }
  217. #endif
  218. #define NELEM(a) (sizeof(a) / sizeof(a[0]))
  219. static const char* json_type_name[] = {
  220. /* If you change this, be sure to update the enum json_type definition too */
  221. "null",
  222. "boolean",
  223. "double",
  224. "int",
  225. "object",
  226. "array",
  227. "string",
  228. };
  229. const char *json_type_to_name(enum json_type o_type)
  230. {
  231. int o_type_int = (int)o_type;
  232. if (o_type_int < 0 || o_type_int >= (int)NELEM(json_type_name))
  233. {
  234. _json_c_set_last_err("json_type_to_name: type %d is out of range [0,%d]\n", o_type, NELEM(json_type_name));
  235. return NULL;
  236. }
  237. return json_type_name[o_type];
  238. }
  239. void json_abort(const char *message)
  240. {
  241. if (message != NULL)
  242. fprintf (stderr, "json-c aborts with error: %s\n", message);
  243. abort();
  244. }