Coverage Report

Created: 2025-06-13 06:36

/src/json-c/json_object.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
3
 * Michael Clark <michael@metaparadigm.com>
4
 * Copyright (c) 2009 Hewlett-Packard Development Company, L.P.
5
 *
6
 * This library is free software; you can redistribute it and/or modify
7
 * it under the terms of the MIT license. See COPYING for details.
8
 *
9
 */
10
11
#include "config.h"
12
13
#include "strerror_override.h"
14
15
#include <assert.h>
16
#ifdef HAVE_LIMITS_H
17
#include <limits.h>
18
#endif
19
#include <math.h>
20
#include <stddef.h>
21
#include <stdio.h>
22
#include <stdlib.h>
23
#include <string.h>
24
25
#include "arraylist.h"
26
#include "debug.h"
27
#include "json_inttypes.h"
28
#include "json_object.h"
29
#include "json_object_private.h"
30
#include "json_util.h"
31
#include "linkhash.h"
32
#include "math_compat.h"
33
#include "printbuf.h"
34
#include "snprintf_compat.h"
35
#include "strdup_compat.h"
36
37
/* Avoid ctype.h and locale overhead */
38
0
#define is_plain_digit(c) ((c) >= '0' && (c) <= '9')
39
40
#if SIZEOF_LONG_LONG != SIZEOF_INT64_T
41
#error The long long type is not 64-bits
42
#endif
43
44
#ifndef SSIZE_T_MAX
45
#if SIZEOF_SSIZE_T == SIZEOF_INT
46
#define SSIZE_T_MAX INT_MAX
47
#elif SIZEOF_SSIZE_T == SIZEOF_LONG
48
111k
#define SSIZE_T_MAX LONG_MAX
49
#elif SIZEOF_SSIZE_T == SIZEOF_LONG_LONG
50
#define SSIZE_T_MAX LLONG_MAX
51
#else
52
#error Unable to determine size of ssize_t
53
#endif
54
#endif
55
56
const char *json_number_chars = "0123456789.+-eE"; /* Unused, but part of public API, drop for 1.0 */
57
const char *json_hex_chars = "0123456789abcdefABCDEF";
58
59
static void json_object_generic_delete(struct json_object *jso);
60
61
#if defined(_MSC_VER) && (_MSC_VER <= 1800)
62
/* VS2013 doesn't know about "inline" */
63
#define inline __inline
64
#elif defined(AIX_CC)
65
#define inline
66
#endif
67
68
/* define colors */
69
#define ANSI_COLOR_RESET "\033[0m"
70
#define ANSI_COLOR_FG_GREEN "\033[0;32m"
71
#define ANSI_COLOR_FG_BLUE "\033[0;34m"
72
#define ANSI_COLOR_FG_MAGENTA "\033[0;35m"
73
74
/*
75
 * Helper functions to more safely cast to a particular type of json_object
76
 */
77
static inline struct json_object_object *JC_OBJECT(struct json_object *jso)
78
530k
{
79
530k
  return (void *)jso;
80
530k
}
81
static inline const struct json_object_object *JC_OBJECT_C(const struct json_object *jso)
82
418k
{
83
418k
  return (const void *)jso;
84
418k
}
85
static inline struct json_object_array *JC_ARRAY(struct json_object *jso)
86
74.5k
{
87
74.5k
  return (void *)jso;
88
74.5k
}
89
static inline const struct json_object_array *JC_ARRAY_C(const struct json_object *jso)
90
209k
{
91
209k
  return (const void *)jso;
92
209k
}
93
static inline struct json_object_boolean *JC_BOOL(struct json_object *jso)
94
61
{
95
61
  return (void *)jso;
96
61
}
97
static inline const struct json_object_boolean *JC_BOOL_C(const struct json_object *jso)
98
2
{
99
2
  return (const void *)jso;
100
2
}
101
static inline struct json_object_double *JC_DOUBLE(struct json_object *jso)
102
104
{
103
104
  return (void *)jso;
104
104
}
105
static inline const struct json_object_double *JC_DOUBLE_C(const struct json_object *jso)
106
4
{
107
4
  return (const void *)jso;
108
4
}
109
static inline struct json_object_int *JC_INT(struct json_object *jso)
110
4.95k
{
111
4.95k
  return (void *)jso;
112
4.95k
}
113
static inline const struct json_object_int *JC_INT_C(const struct json_object *jso)
114
444
{
115
444
  return (const void *)jso;
116
444
}
117
static inline struct json_object_string *JC_STRING(struct json_object *jso)
118
471k
{
119
471k
  return (void *)jso;
120
471k
}
121
static inline const struct json_object_string *JC_STRING_C(const struct json_object *jso)
122
274k
{
123
274k
  return (const void *)jso;
124
274k
}
125
126
101k
#define JC_CONCAT(a, b) a##b
127
101k
#define JC_CONCAT3(a, b, c) a##b##c
128
129
#define JSON_OBJECT_NEW(jtype)                                                           \
130
101k
  (struct JC_CONCAT(json_object_, jtype) *)json_object_new(                        \
131
101k
      JC_CONCAT(json_type_, jtype), sizeof(struct JC_CONCAT(json_object_, jtype)), \
132
101k
      &JC_CONCAT3(json_object_, jtype, _to_json_string))
133
134
static inline struct json_object *json_object_new(enum json_type o_type, size_t alloc_size,
135
                                                  json_object_to_json_string_fn *to_json_string);
136
137
static void json_object_object_delete(struct json_object *jso_base);
138
static void json_object_string_delete(struct json_object *jso);
139
static void json_object_array_delete(struct json_object *jso);
140
141
static json_object_to_json_string_fn json_object_object_to_json_string;
142
static json_object_to_json_string_fn json_object_boolean_to_json_string;
143
static json_object_to_json_string_fn json_object_double_to_json_string_default;
144
static json_object_to_json_string_fn json_object_int_to_json_string;
145
static json_object_to_json_string_fn json_object_string_to_json_string;
146
static json_object_to_json_string_fn json_object_array_to_json_string;
147
static json_object_to_json_string_fn _json_object_userdata_to_json_string;
148
149
#ifndef JSON_NORETURN
150
#if defined(_MSC_VER)
151
#define JSON_NORETURN __declspec(noreturn)
152
#elif defined(__OS400__)
153
#define JSON_NORETURN
154
#else
155
/* 'cold' attribute is for optimization, telling the computer this code
156
 * path is unlikely.
157
 */
158
#define JSON_NORETURN __attribute__((noreturn, cold))
159
#endif
160
#endif
161
/**
162
 * Abort and optionally print a message on standard error.
163
 * This should be used rather than assert() for unconditional abortion
164
 * (in particular for code paths which are never supposed to be run).
165
 * */
166
JSON_NORETURN static void json_abort(const char *message);
167
168
/* helper for accessing the optimized string data component in json_object
169
 */
170
static inline char *get_string_component_mutable(struct json_object *jso)
171
274k
{
172
274k
  if (JC_STRING_C(jso)->len < 0)
173
0
  {
174
    /* Due to json_object_set_string(), we might have a pointer */
175
0
    return JC_STRING(jso)->c_string.pdata;
176
0
  }
177
274k
  return JC_STRING(jso)->c_string.idata;
178
274k
}
179
static inline const char *get_string_component(const struct json_object *jso)
180
274k
{
181
274k
  return get_string_component_mutable((void *)(uintptr_t)(const void *)jso);
182
274k
}
183
184
/* string escaping */
185
186
static int json_escape_str(struct printbuf *pb, const char *str, size_t len, int flags)
187
154k
{
188
154k
  size_t pos = 0, start_offset = 0;
189
154k
  unsigned char c;
190
2.45M
  while (len)
191
2.30M
  {
192
2.30M
    --len;
193
2.30M
    c = str[pos];
194
2.30M
    switch (c)
195
2.30M
    {
196
589
    case '\b':
197
22.5k
    case '\n':
198
23.3k
    case '\r':
199
27.7k
    case '\t':
200
28.8k
    case '\f':
201
36.0k
    case '"':
202
52.2k
    case '\\':
203
90.5k
    case '/':
204
90.5k
      if ((flags & JSON_C_TO_STRING_NOSLASHESCAPE) && c == '/')
205
38.2k
      {
206
38.2k
        pos++;
207
38.2k
        break;
208
38.2k
      }
209
210
52.2k
      if (pos > start_offset)
211
33.4k
        printbuf_memappend(pb, str + start_offset, pos - start_offset);
212
213
52.2k
      if (c == '\b')
214
589
        printbuf_memappend(pb, "\\b", 2);
215
51.6k
      else if (c == '\n')
216
21.9k
        printbuf_memappend(pb, "\\n", 2);
217
29.7k
      else if (c == '\r')
218
838
        printbuf_memappend(pb, "\\r", 2);
219
28.8k
      else if (c == '\t')
220
4.39k
        printbuf_memappend(pb, "\\t", 2);
221
24.4k
      else if (c == '\f')
222
1.06k
        printbuf_memappend(pb, "\\f", 2);
223
23.4k
      else if (c == '"')
224
7.18k
        printbuf_memappend(pb, "\\\"", 2);
225
16.2k
      else if (c == '\\')
226
16.2k
        printbuf_memappend(pb, "\\\\", 2);
227
0
      else if (c == '/')
228
0
        printbuf_memappend(pb, "\\/", 2);
229
230
52.2k
      start_offset = ++pos;
231
52.2k
      break;
232
2.20M
    default:
233
2.20M
      if (c < ' ')
234
194k
      {
235
194k
        char sbuf[7];
236
194k
        if (pos > start_offset)
237
2.39k
          printbuf_memappend(pb, str + start_offset,
238
2.39k
                             pos - start_offset);
239
194k
        snprintf(sbuf, sizeof(sbuf), "\\u00%c%c", json_hex_chars[c >> 4],
240
194k
                 json_hex_chars[c & 0xf]);
241
194k
        printbuf_memappend_fast(pb, sbuf, (int)sizeof(sbuf) - 1);
242
194k
        start_offset = ++pos;
243
194k
      }
244
2.01M
      else
245
2.01M
        pos++;
246
2.30M
    }
247
2.30M
  }
248
154k
  if (pos > start_offset)
249
152k
    printbuf_memappend(pb, str + start_offset, pos - start_offset);
250
154k
  return 0;
251
154k
}
252
253
/* reference counting */
254
255
struct json_object *json_object_get(struct json_object *jso)
256
168k
{
257
168k
  if (!jso)
258
236
    return jso;
259
260
  // Don't overflow the refcounter.
261
168k
  assert(jso->_ref_count < UINT32_MAX);
262
263
#if defined(HAVE_ATOMIC_BUILTINS) && defined(ENABLE_THREADING)
264
  __sync_add_and_fetch(&jso->_ref_count, 1);
265
#else
266
168k
  ++jso->_ref_count;
267
168k
#endif
268
269
168k
  return jso;
270
168k
}
271
272
int json_object_put(struct json_object *jso)
273
572k
{
274
572k
  if (!jso)
275
191k
    return 0;
276
277
  /* Avoid invalid free and crash explicitly instead of (silently)
278
   * segfaulting.
279
   */
280
381k
  assert(jso->_ref_count > 0);
281
282
#if defined(HAVE_ATOMIC_BUILTINS) && defined(ENABLE_THREADING)
283
  /* Note: this only allow the refcount to remain correct
284
   * when multiple threads are adjusting it.  It is still an error
285
   * for a thread to decrement the refcount if it doesn't "own" it,
286
   * as that can result in the thread that loses the race to 0
287
   * operating on an already-freed object.
288
   */
289
  if (__sync_sub_and_fetch(&jso->_ref_count, 1) > 0)
290
    return 0;
291
#else
292
381k
  if (--jso->_ref_count > 0)
293
168k
    return 0;
294
213k
#endif
295
296
213k
  if (jso->_user_delete)
297
549
    jso->_user_delete(jso, jso->_userdata);
298
213k
  switch (jso->o_type)
299
213k
  {
300
70.9k
  case json_type_object: json_object_object_delete(jso); break;
301
15.0k
  case json_type_array: json_object_array_delete(jso); break;
302
111k
  case json_type_string: json_object_string_delete(jso); break;
303
15.7k
  default: json_object_generic_delete(jso); break;
304
213k
  }
305
213k
  return 1;
306
213k
}
307
308
/* generic object construction and destruction parts */
309
310
static void json_object_generic_delete(struct json_object *jso)
311
213k
{
312
213k
  printbuf_free(jso->_pb);
313
213k
  free(jso);
314
213k
}
315
316
static inline struct json_object *json_object_new(enum json_type o_type, size_t alloc_size,
317
                                                  json_object_to_json_string_fn *to_json_string)
318
213k
{
319
213k
  struct json_object *jso;
320
321
213k
  jso = (struct json_object *)malloc(alloc_size);
322
213k
  if (!jso)
323
0
    return NULL;
324
325
213k
  jso->o_type = o_type;
326
213k
  jso->_ref_count = 1;
327
213k
  jso->_to_json_string = to_json_string;
328
213k
  jso->_pb = NULL;
329
213k
  jso->_user_delete = NULL;
330
213k
  jso->_userdata = NULL;
331
  //jso->...   // Type-specific fields must be set by caller
332
333
213k
  return jso;
334
213k
}
335
336
/* type checking functions */
337
338
int json_object_is_type(const struct json_object *jso, enum json_type type)
339
132k
{
340
132k
  if (!jso)
341
0
    return (type == json_type_null);
342
132k
  return (jso->o_type == type);
343
132k
}
344
345
enum json_type json_object_get_type(const struct json_object *jso)
346
427k
{
347
427k
  if (!jso)
348
0
    return json_type_null;
349
427k
  return jso->o_type;
350
427k
}
351
352
void *json_object_get_userdata(json_object *jso)
353
0
{
354
0
  return jso ? jso->_userdata : NULL;
355
0
}
356
357
void json_object_set_userdata(json_object *jso, void *userdata, json_object_delete_fn *user_delete)
358
493
{
359
  // Can't return failure, so abort if we can't perform the operation.
360
493
  assert(jso != NULL);
361
362
  // First, clean up any previously existing user info
363
493
  if (jso->_user_delete)
364
0
    jso->_user_delete(jso, jso->_userdata);
365
366
493
  jso->_userdata = userdata;
367
493
  jso->_user_delete = user_delete;
368
493
}
369
370
/* set a custom conversion to string */
371
372
void json_object_set_serializer(json_object *jso, json_object_to_json_string_fn *to_string_func,
373
                                void *userdata, json_object_delete_fn *user_delete)
374
493
{
375
493
  json_object_set_userdata(jso, userdata, user_delete);
376
377
493
  if (to_string_func == NULL)
378
0
  {
379
    // Reset to the standard serialization function
380
0
    switch (jso->o_type)
381
0
    {
382
0
    case json_type_null: jso->_to_json_string = NULL; break;
383
0
    case json_type_boolean:
384
0
      jso->_to_json_string = &json_object_boolean_to_json_string;
385
0
      break;
386
0
    case json_type_double:
387
0
      jso->_to_json_string = &json_object_double_to_json_string_default;
388
0
      break;
389
0
    case json_type_int: jso->_to_json_string = &json_object_int_to_json_string; break;
390
0
    case json_type_object:
391
0
      jso->_to_json_string = &json_object_object_to_json_string;
392
0
      break;
393
0
    case json_type_array:
394
0
      jso->_to_json_string = &json_object_array_to_json_string;
395
0
      break;
396
0
    case json_type_string:
397
0
      jso->_to_json_string = &json_object_string_to_json_string;
398
0
      break;
399
0
    }
400
0
    return;
401
0
  }
402
403
493
  jso->_to_json_string = to_string_func;
404
493
}
405
406
/* extended conversion to string */
407
408
const char *json_object_to_json_string_length(struct json_object *jso, int flags, size_t *length)
409
6.67k
{
410
6.67k
  const char *r = NULL;
411
6.67k
  size_t s = 0;
412
413
6.67k
  if (!jso)
414
0
  {
415
0
    s = 4;
416
0
    r = "null";
417
0
  }
418
6.67k
  else if ((jso->_pb) || (jso->_pb = printbuf_new()))
419
6.67k
  {
420
6.67k
    printbuf_reset(jso->_pb);
421
422
6.67k
    if (jso->_to_json_string(jso, jso->_pb, 0, flags) >= 0)
423
6.67k
    {
424
6.67k
      s = (size_t)jso->_pb->bpos;
425
6.67k
      r = jso->_pb->buf;
426
6.67k
    }
427
6.67k
  }
428
429
6.67k
  if (length)
430
0
    *length = s;
431
6.67k
  return r;
432
6.67k
}
433
434
const char *json_object_to_json_string_ext(struct json_object *jso, int flags)
435
6.67k
{
436
6.67k
  return json_object_to_json_string_length(jso, flags, NULL);
437
6.67k
}
438
439
/* backwards-compatible conversion to string */
440
441
const char *json_object_to_json_string(struct json_object *jso)
442
34
{
443
34
  return json_object_to_json_string_ext(jso, JSON_C_TO_STRING_SPACED);
444
34
}
445
446
static void indent(struct printbuf *pb, int level, int flags)
447
114k
{
448
114k
  if (flags & JSON_C_TO_STRING_PRETTY)
449
0
  {
450
0
    if (flags & JSON_C_TO_STRING_PRETTY_TAB)
451
0
    {
452
0
      printbuf_memset(pb, -1, '\t', level);
453
0
    }
454
0
    else
455
0
    {
456
0
      printbuf_memset(pb, -1, ' ', level * 2);
457
0
    }
458
0
  }
459
114k
}
460
461
/* json_object_object */
462
463
static int json_object_object_to_json_string(struct json_object *jso, struct printbuf *pb,
464
                                             int level, int flags)
465
47.2k
{
466
47.2k
  int had_children = 0;
467
47.2k
  struct json_object_iter iter;
468
469
47.2k
  printbuf_strappend(pb, "{" /*}*/);
470
47.2k
  json_object_object_foreachC(jso, iter)
471
91.2k
  {
472
91.2k
    if (had_children)
473
63.9k
    {
474
63.9k
      printbuf_strappend(pb, ",");
475
63.9k
    }
476
91.2k
    if (flags & JSON_C_TO_STRING_PRETTY)
477
0
      printbuf_strappend(pb, "\n");
478
91.2k
    had_children = 1;
479
91.2k
    if (flags & JSON_C_TO_STRING_SPACED && !(flags & JSON_C_TO_STRING_PRETTY))
480
0
      printbuf_strappend(pb, " ");
481
91.2k
    indent(pb, level + 1, flags);
482
91.2k
    if (flags & JSON_C_TO_STRING_COLOR)
483
0
      printbuf_strappend(pb, ANSI_COLOR_FG_BLUE);
484
485
91.2k
    printbuf_strappend(pb, "\"");
486
91.2k
    json_escape_str(pb, iter.key, strlen(iter.key), flags);
487
91.2k
    printbuf_strappend(pb, "\"");
488
489
91.2k
    if (flags & JSON_C_TO_STRING_COLOR)
490
0
      printbuf_strappend(pb, ANSI_COLOR_RESET);
491
492
91.2k
    if (flags & JSON_C_TO_STRING_SPACED)
493
0
      printbuf_strappend(pb, ": ");
494
91.2k
    else
495
91.2k
      printbuf_strappend(pb, ":");
496
497
91.2k
    if (iter.val == NULL) {
498
60
      if (flags & JSON_C_TO_STRING_COLOR)
499
0
        printbuf_strappend(pb, ANSI_COLOR_FG_MAGENTA);
500
60
      printbuf_strappend(pb, "null");
501
60
      if (flags & JSON_C_TO_STRING_COLOR)
502
0
        printbuf_strappend(pb, ANSI_COLOR_RESET);
503
91.2k
    } else if (iter.val->_to_json_string(iter.val, pb, level + 1, flags) < 0)
504
0
      return -1;
505
91.2k
  }
506
47.2k
  if ((flags & JSON_C_TO_STRING_PRETTY) && had_children)
507
0
  {
508
0
    printbuf_strappend(pb, "\n");
509
0
    indent(pb, level, flags);
510
0
  }
511
47.2k
  if (flags & JSON_C_TO_STRING_SPACED && !(flags & JSON_C_TO_STRING_PRETTY))
512
0
    return printbuf_strappend(pb, /*{*/ " }");
513
47.2k
  else
514
47.2k
    return printbuf_strappend(pb, /*{*/ "}");
515
47.2k
}
516
517
static void json_object_lh_entry_free(struct lh_entry *ent)
518
148k
{
519
148k
  if (!lh_entry_k_is_constant(ent))
520
148k
    free(lh_entry_k(ent));
521
148k
  json_object_put((struct json_object *)lh_entry_v(ent));
522
148k
}
523
524
static void json_object_object_delete(struct json_object *jso_base)
525
70.9k
{
526
70.9k
  lh_table_free(JC_OBJECT(jso_base)->c_object);
527
70.9k
  json_object_generic_delete(jso_base);
528
70.9k
}
529
530
struct json_object *json_object_new_object(void)
531
70.9k
{
532
70.9k
  struct json_object_object *jso = JSON_OBJECT_NEW(object);
533
70.9k
  if (!jso)
534
0
    return NULL;
535
70.9k
  jso->c_object =
536
70.9k
      lh_kchar_table_new(JSON_OBJECT_DEF_HASH_ENTRIES, &json_object_lh_entry_free);
537
70.9k
  if (!jso->c_object)
538
0
  {
539
0
    json_object_generic_delete(&jso->base);
540
0
    errno = ENOMEM;
541
0
    return NULL;
542
0
  }
543
70.9k
  return &jso->base;
544
70.9k
}
545
546
struct lh_table *json_object_get_object(const struct json_object *jso)
547
109k
{
548
109k
  if (!jso)
549
0
    return NULL;
550
109k
  switch (jso->o_type)
551
109k
  {
552
109k
  case json_type_object: return JC_OBJECT_C(jso)->c_object;
553
0
  default: return NULL;
554
109k
  }
555
109k
}
556
557
int json_object_object_add_ex(struct json_object *jso, const char *const key,
558
                              struct json_object *const val, const unsigned opts)
559
154k
{
560
154k
  struct json_object *existing_value = NULL;
561
154k
  struct lh_entry *existing_entry;
562
154k
  unsigned long hash;
563
564
154k
  assert(json_object_get_type(jso) == json_type_object);
565
566
  // We lookup the entry and replace the value, rather than just deleting
567
  // and re-adding it, so the existing key remains valid.
568
154k
  hash = lh_get_hash(JC_OBJECT(jso)->c_object, (const void *)key);
569
154k
  existing_entry =
570
154k
      (opts & JSON_C_OBJECT_ADD_KEY_IS_NEW)
571
154k
          ? NULL
572
154k
          : lh_table_lookup_entry_w_hash(JC_OBJECT(jso)->c_object, (const void *)key, hash);
573
574
  // The caller must avoid creating loops in the object tree, but do a
575
  // quick check anyway to make sure we're not creating a trivial loop.
576
154k
  if (jso == val)
577
0
    return -1;
578
579
154k
  if (!existing_entry)
580
148k
  {
581
148k
    const void *const k =
582
148k
        (opts & JSON_C_OBJECT_ADD_CONSTANT_KEY) ? (const void *)key : strdup(key);
583
148k
    if (k == NULL)
584
0
      return -1;
585
148k
    return lh_table_insert_w_hash(JC_OBJECT(jso)->c_object, k, val, hash, opts);
586
148k
  }
587
6.38k
  existing_value = (json_object *)lh_entry_v(existing_entry);
588
6.38k
  if (existing_value)
589
6.34k
    json_object_put(existing_value);
590
6.38k
  lh_entry_set_val(existing_entry, val);
591
6.38k
  return 0;
592
154k
}
593
594
int json_object_object_add(struct json_object *jso, const char *key, struct json_object *val)
595
154k
{
596
154k
  return json_object_object_add_ex(jso, key, val, 0);
597
154k
}
598
599
int json_object_object_length(const struct json_object *jso)
600
13.6k
{
601
13.6k
  assert(json_object_get_type(jso) == json_type_object);
602
13.6k
  return lh_table_length(JC_OBJECT_C(jso)->c_object);
603
13.6k
}
604
605
size_t json_c_object_sizeof(void)
606
0
{
607
0
  return sizeof(struct json_object);
608
0
}
609
610
struct json_object *json_object_object_get(const struct json_object *jso, const char *key)
611
0
{
612
0
  struct json_object *result = NULL;
613
0
  json_object_object_get_ex(jso, key, &result);
614
0
  return result;
615
0
}
616
617
json_bool json_object_object_get_ex(const struct json_object *jso, const char *key,
618
                                    struct json_object **value)
619
295k
{
620
295k
  if (value != NULL)
621
287k
    *value = NULL;
622
623
295k
  if (NULL == jso)
624
0
    return 0;
625
626
295k
  switch (jso->o_type)
627
295k
  {
628
295k
  case json_type_object:
629
295k
    return lh_table_lookup_ex(JC_OBJECT_C(jso)->c_object, (const void *)key,
630
295k
                              (void **)value);
631
0
  default:
632
0
    if (value != NULL)
633
0
      *value = NULL;
634
0
    return 0;
635
295k
  }
636
295k
}
637
638
void json_object_object_del(struct json_object *jso, const char *key)
639
577
{
640
577
  assert(json_object_get_type(jso) == json_type_object);
641
577
  lh_table_delete(JC_OBJECT(jso)->c_object, key);
642
577
}
643
644
/* json_object_boolean */
645
646
static int json_object_boolean_to_json_string(struct json_object *jso, struct printbuf *pb,
647
                                              int level, int flags)
648
41
{
649
41
  int ret;
650
651
41
  if (flags & JSON_C_TO_STRING_COLOR)
652
0
    printbuf_strappend(pb, ANSI_COLOR_FG_MAGENTA);
653
654
41
  if (JC_BOOL(jso)->c_boolean)
655
24
    ret = printbuf_strappend(pb, "true");
656
17
  else
657
17
    ret = printbuf_strappend(pb, "false");
658
41
  if (ret > -1 && flags & JSON_C_TO_STRING_COLOR)
659
0
    return printbuf_strappend(pb, ANSI_COLOR_RESET);
660
41
  return ret;
661
41
}
662
663
struct json_object *json_object_new_boolean(json_bool b)
664
348
{
665
348
  struct json_object_boolean *jso = JSON_OBJECT_NEW(boolean);
666
348
  if (!jso)
667
0
    return NULL;
668
348
  jso->c_boolean = b;
669
348
  return &jso->base;
670
348
}
671
672
json_bool json_object_get_boolean(const struct json_object *jso)
673
0
{
674
0
  if (!jso)
675
0
    return 0;
676
0
  switch (jso->o_type)
677
0
  {
678
0
  case json_type_boolean: return JC_BOOL_C(jso)->c_boolean;
679
0
  case json_type_int:
680
0
    switch (JC_INT_C(jso)->cint_type)
681
0
    {
682
0
    case json_object_int_type_int64: return (JC_INT_C(jso)->cint.c_int64 != 0);
683
0
    case json_object_int_type_uint64: return (JC_INT_C(jso)->cint.c_uint64 != 0);
684
0
    default: json_abort("invalid cint_type");
685
0
    }
686
0
  case json_type_double: return (JC_DOUBLE_C(jso)->c_double != 0);
687
0
  case json_type_string: return (JC_STRING_C(jso)->len != 0);
688
0
  default: return 0;
689
0
  }
690
0
}
691
692
int json_object_set_boolean(struct json_object *jso, json_bool new_value)
693
0
{
694
0
  if (!jso || jso->o_type != json_type_boolean)
695
0
    return 0;
696
0
  JC_BOOL(jso)->c_boolean = new_value;
697
0
  return 1;
698
0
}
699
700
/* json_object_int */
701
702
static int json_object_int_to_json_string(struct json_object *jso, struct printbuf *pb, int level,
703
                                          int flags)
704
1.73k
{
705
  /* room for 19 digits, the sign char, and a null term */
706
1.73k
  char sbuf[21];
707
1.73k
  if (JC_INT(jso)->cint_type == json_object_int_type_int64)
708
1.68k
    snprintf(sbuf, sizeof(sbuf), "%" PRId64, JC_INT(jso)->cint.c_int64);
709
50
  else
710
50
    snprintf(sbuf, sizeof(sbuf), "%" PRIu64, JC_INT(jso)->cint.c_uint64);
711
1.73k
  return printbuf_memappend(pb, sbuf, strlen(sbuf));
712
1.73k
}
713
714
struct json_object *json_object_new_int(int32_t i)
715
0
{
716
0
  return json_object_new_int64(i);
717
0
}
718
719
int32_t json_object_get_int(const struct json_object *jso)
720
310
{
721
310
  int64_t cint64 = 0;
722
310
  double cdouble;
723
310
  enum json_type o_type;
724
310
  errno = 0;
725
726
310
  if (!jso)
727
0
    return 0;
728
729
310
  o_type = jso->o_type;
730
310
  if (o_type == json_type_int)
731
304
  {
732
304
    const struct json_object_int *jsoint = JC_INT_C(jso);
733
304
    if (jsoint->cint_type == json_object_int_type_int64)
734
304
    {
735
304
      cint64 = jsoint->cint.c_int64;
736
304
    }
737
0
    else
738
0
    {
739
0
      if (jsoint->cint.c_uint64 >= INT64_MAX)
740
0
        cint64 = INT64_MAX;
741
0
      else
742
0
        cint64 = (int64_t)jsoint->cint.c_uint64;
743
0
    }
744
304
  }
745
6
  else if (o_type == json_type_string)
746
0
  {
747
    /*
748
     * Parse strings into 64-bit numbers, then use the
749
     * 64-to-32-bit number handling below.
750
     */
751
0
    if (json_parse_int64(get_string_component(jso), &cint64) != 0)
752
0
      return 0; /* whoops, it didn't work. */
753
0
    o_type = json_type_int;
754
0
  }
755
756
310
  switch (o_type)
757
310
  {
758
304
  case json_type_int:
759
    /* Make sure we return the correct values for out of range numbers. */
760
304
    if (cint64 < INT32_MIN)
761
6
    {
762
6
      errno = ERANGE;
763
6
      return INT32_MIN;
764
6
    }
765
298
    if (cint64 > INT32_MAX)
766
0
    {
767
0
      errno = ERANGE;
768
0
      return INT32_MAX;
769
0
    }
770
298
    return (int32_t)cint64;
771
4
  case json_type_double:
772
4
    cdouble = JC_DOUBLE_C(jso)->c_double;
773
4
    if (cdouble < INT32_MIN)
774
0
    {
775
0
      errno = ERANGE;
776
0
      return INT32_MIN;
777
0
    }
778
4
    if (cdouble > INT32_MAX)
779
4
    {
780
4
      errno = ERANGE;
781
4
      return INT32_MAX;
782
4
    }
783
0
    if (isnan(cdouble))
784
0
    {
785
0
      errno = EINVAL;
786
0
      return INT32_MIN;
787
0
    }
788
0
    return (int32_t)cdouble;
789
2
  case json_type_boolean: return JC_BOOL_C(jso)->c_boolean;
790
0
  default: return 0;
791
310
  }
792
310
}
793
794
int json_object_set_int(struct json_object *jso, int new_value)
795
0
{
796
0
  return json_object_set_int64(jso, (int64_t)new_value);
797
0
}
798
799
struct json_object *json_object_new_int64(int64_t i)
800
14.1k
{
801
14.1k
  struct json_object_int *jso = JSON_OBJECT_NEW(int);
802
14.1k
  if (!jso)
803
0
    return NULL;
804
14.1k
  jso->cint.c_int64 = i;
805
14.1k
  jso->cint_type = json_object_int_type_int64;
806
14.1k
  return &jso->base;
807
14.1k
}
808
809
struct json_object *json_object_new_uint64(uint64_t i)
810
559
{
811
559
  struct json_object_int *jso = JSON_OBJECT_NEW(int);
812
559
  if (!jso)
813
0
    return NULL;
814
559
  jso->cint.c_uint64 = i;
815
559
  jso->cint_type = json_object_int_type_uint64;
816
559
  return &jso->base;
817
559
}
818
819
int64_t json_object_get_int64(const struct json_object *jso)
820
140
{
821
140
  int64_t cint;
822
140
  errno = 0;
823
824
140
  if (!jso)
825
0
    return 0;
826
140
  switch (jso->o_type)
827
140
  {
828
140
  case json_type_int:
829
140
  {
830
140
    const struct json_object_int *jsoint = JC_INT_C(jso);
831
140
    switch (jsoint->cint_type)
832
140
    {
833
136
    case json_object_int_type_int64: return jsoint->cint.c_int64;
834
4
    case json_object_int_type_uint64:
835
4
      if (jsoint->cint.c_uint64 > INT64_MAX)
836
4
      {
837
4
        errno = ERANGE;
838
4
        return INT64_MAX;
839
4
      }
840
0
      return (int64_t)jsoint->cint.c_uint64;
841
0
    default: json_abort("invalid cint_type");
842
140
    }
843
140
  }
844
0
  case json_type_double:
845
    // INT64_MAX can't be exactly represented as a double
846
    // so cast to tell the compiler it's ok to round up.
847
0
    if (JC_DOUBLE_C(jso)->c_double > (double)INT64_MAX)
848
0
    {
849
0
      errno = ERANGE;
850
0
      return INT64_MAX;
851
0
    }
852
0
    if (JC_DOUBLE_C(jso)->c_double < (double)INT64_MIN)
853
0
    {
854
0
      errno = ERANGE;
855
0
      return INT64_MIN;
856
0
    }
857
0
    if (isnan(JC_DOUBLE_C(jso)->c_double))
858
0
    {
859
0
      errno = EINVAL;
860
0
      return INT64_MIN;
861
0
    }
862
0
    return (int64_t)JC_DOUBLE_C(jso)->c_double;
863
0
  case json_type_boolean: return JC_BOOL_C(jso)->c_boolean;
864
0
  case json_type_string:
865
0
    if (json_parse_int64(get_string_component(jso), &cint) == 0)
866
0
      return cint;
867
    /* FALLTHRU */
868
0
  default: return 0;
869
140
  }
870
140
}
871
872
uint64_t json_object_get_uint64(const struct json_object *jso)
873
0
{
874
0
  uint64_t cuint;
875
0
  errno = 0;
876
877
0
  if (!jso)
878
0
    return 0;
879
0
  switch (jso->o_type)
880
0
  {
881
0
  case json_type_int:
882
0
  {
883
0
    const struct json_object_int *jsoint = JC_INT_C(jso);
884
0
    switch (jsoint->cint_type)
885
0
    {
886
0
    case json_object_int_type_int64:
887
0
      if (jsoint->cint.c_int64 < 0)
888
0
      {
889
0
        errno = ERANGE;
890
0
        return 0;
891
0
      }
892
0
      return (uint64_t)jsoint->cint.c_int64;
893
0
    case json_object_int_type_uint64: return jsoint->cint.c_uint64;
894
0
    default: json_abort("invalid cint_type");
895
0
    }
896
0
  }
897
0
  case json_type_double:
898
    // UINT64_MAX can't be exactly represented as a double
899
    // so cast to tell the compiler it's ok to round up.
900
0
    if (JC_DOUBLE_C(jso)->c_double > (double)UINT64_MAX)
901
0
    {
902
0
      errno = ERANGE;
903
0
      return UINT64_MAX;
904
0
    }
905
0
    if (JC_DOUBLE_C(jso)->c_double < 0)
906
0
    {
907
0
      errno = ERANGE;
908
0
      return 0;
909
0
    }
910
0
    if (isnan(JC_DOUBLE_C(jso)->c_double))
911
0
    {
912
0
      errno = EINVAL;
913
0
      return 0;
914
0
    }
915
0
    return (uint64_t)JC_DOUBLE_C(jso)->c_double;
916
0
  case json_type_boolean: return JC_BOOL_C(jso)->c_boolean;
917
0
  case json_type_string:
918
0
    if (json_parse_uint64(get_string_component(jso), &cuint) == 0)
919
0
      return cuint;
920
    /* FALLTHRU */
921
0
  default: return 0;
922
0
  }
923
0
}
924
925
int json_object_set_int64(struct json_object *jso, int64_t new_value)
926
0
{
927
0
  if (!jso || jso->o_type != json_type_int)
928
0
    return 0;
929
0
  JC_INT(jso)->cint.c_int64 = new_value;
930
0
  JC_INT(jso)->cint_type = json_object_int_type_int64;
931
0
  return 1;
932
0
}
933
934
int json_object_set_uint64(struct json_object *jso, uint64_t new_value)
935
0
{
936
0
  if (!jso || jso->o_type != json_type_int)
937
0
    return 0;
938
0
  JC_INT(jso)->cint.c_uint64 = new_value;
939
0
  JC_INT(jso)->cint_type = json_object_int_type_uint64;
940
0
  return 1;
941
0
}
942
943
int json_object_int_inc(struct json_object *jso, int64_t val)
944
0
{
945
0
  struct json_object_int *jsoint;
946
0
  if (!jso || jso->o_type != json_type_int)
947
0
    return 0;
948
0
  jsoint = JC_INT(jso);
949
0
  switch (jsoint->cint_type)
950
0
  {
951
0
  case json_object_int_type_int64:
952
0
    if (val > 0 && jsoint->cint.c_int64 > INT64_MAX - val)
953
0
    {
954
0
      jsoint->cint.c_uint64 = (uint64_t)jsoint->cint.c_int64 + (uint64_t)val;
955
0
      jsoint->cint_type = json_object_int_type_uint64;
956
0
    }
957
0
    else if (val < 0 && jsoint->cint.c_int64 < INT64_MIN - val)
958
0
    {
959
0
      jsoint->cint.c_int64 = INT64_MIN;
960
0
    }
961
0
    else
962
0
    {
963
0
      jsoint->cint.c_int64 += val;
964
0
    }
965
0
    return 1;
966
0
  case json_object_int_type_uint64:
967
0
    if (val > 0 && jsoint->cint.c_uint64 > UINT64_MAX - (uint64_t)val)
968
0
    {
969
0
      jsoint->cint.c_uint64 = UINT64_MAX;
970
0
    }
971
0
    else if (val < 0 && jsoint->cint.c_uint64 < (uint64_t)(-val))
972
0
    {
973
0
      jsoint->cint.c_int64 = (int64_t)jsoint->cint.c_uint64 + val;
974
0
      jsoint->cint_type = json_object_int_type_int64;
975
0
    }
976
0
    else if (val < 0 && jsoint->cint.c_uint64 >= (uint64_t)(-val))
977
0
    {
978
0
      jsoint->cint.c_uint64 -= (uint64_t)(-val);
979
0
    }
980
0
    else
981
0
    {
982
0
      jsoint->cint.c_uint64 += val;
983
0
    }
984
0
    return 1;
985
0
  default: json_abort("invalid cint_type");
986
0
  }
987
0
}
988
989
/* json_object_double */
990
991
#if defined(HAVE___THREAD)
992
// i.e. __thread or __declspec(thread)
993
static SPEC___THREAD char *tls_serialization_float_format = NULL;
994
#endif
995
static char *global_serialization_float_format = NULL;
996
997
int json_c_set_serialization_double_format(const char *double_format, int global_or_thread)
998
0
{
999
0
  if (global_or_thread == JSON_C_OPTION_GLOBAL)
1000
0
  {
1001
0
#if defined(HAVE___THREAD)
1002
0
    if (tls_serialization_float_format)
1003
0
    {
1004
0
      free(tls_serialization_float_format);
1005
0
      tls_serialization_float_format = NULL;
1006
0
    }
1007
0
#endif
1008
0
    if (global_serialization_float_format)
1009
0
      free(global_serialization_float_format);
1010
0
    if (double_format)
1011
0
    {
1012
0
      char *p = strdup(double_format);
1013
0
      if (p == NULL)
1014
0
      {
1015
0
        _json_c_set_last_err("json_c_set_serialization_double_format: "
1016
0
                             "out of memory\n");
1017
0
        return -1;
1018
0
      }
1019
0
      global_serialization_float_format = p;
1020
0
    }
1021
0
    else
1022
0
    {
1023
0
      global_serialization_float_format = NULL;
1024
0
    }
1025
0
  }
1026
0
  else if (global_or_thread == JSON_C_OPTION_THREAD)
1027
0
  {
1028
0
#if defined(HAVE___THREAD)
1029
0
    if (tls_serialization_float_format)
1030
0
    {
1031
0
      free(tls_serialization_float_format);
1032
0
      tls_serialization_float_format = NULL;
1033
0
    }
1034
0
    if (double_format)
1035
0
    {
1036
0
      char *p = strdup(double_format);
1037
0
      if (p == NULL)
1038
0
      {
1039
0
        _json_c_set_last_err("json_c_set_serialization_double_format: "
1040
0
                             "out of memory\n");
1041
0
        return -1;
1042
0
      }
1043
0
      tls_serialization_float_format = p;
1044
0
    }
1045
0
    else
1046
0
    {
1047
0
      tls_serialization_float_format = NULL;
1048
0
    }
1049
#else
1050
    _json_c_set_last_err("json_c_set_serialization_double_format: not compiled "
1051
                         "with __thread support\n");
1052
    return -1;
1053
#endif
1054
0
  }
1055
0
  else
1056
0
  {
1057
0
    _json_c_set_last_err("json_c_set_serialization_double_format: invalid "
1058
0
                         "global_or_thread value: %d\n", global_or_thread);
1059
0
    return -1;
1060
0
  }
1061
0
  return 0;
1062
0
}
1063
1064
static int json_object_double_to_json_string_format(struct json_object *jso, struct printbuf *pb,
1065
                                                    int level, int flags, const char *format)
1066
32
{
1067
32
  struct json_object_double *jsodbl = JC_DOUBLE(jso);
1068
32
  char buf[128], *p, *q;
1069
32
  int size;
1070
  /* Although JSON RFC does not support
1071
   * NaN or Infinity as numeric values
1072
   * ECMA 262 section 9.8.1 defines
1073
   * how to handle these cases as strings
1074
   */
1075
32
  if (isnan(jsodbl->c_double))
1076
32
  {
1077
32
    size = snprintf(buf, sizeof(buf), "NaN");
1078
32
  }
1079
0
  else if (isinf(jsodbl->c_double))
1080
0
  {
1081
0
    if (jsodbl->c_double > 0)
1082
0
      size = snprintf(buf, sizeof(buf), "Infinity");
1083
0
    else
1084
0
      size = snprintf(buf, sizeof(buf), "-Infinity");
1085
0
  }
1086
0
  else
1087
0
  {
1088
0
    const char *std_format = "%.17g";
1089
0
    int format_drops_decimals = 0;
1090
0
    int looks_numeric = 0;
1091
1092
0
    if (!format)
1093
0
    {
1094
0
#if defined(HAVE___THREAD)
1095
0
      if (tls_serialization_float_format)
1096
0
        format = tls_serialization_float_format;
1097
0
      else
1098
0
#endif
1099
0
          if (global_serialization_float_format)
1100
0
        format = global_serialization_float_format;
1101
0
      else
1102
0
        format = std_format;
1103
0
    }
1104
0
    size = snprintf(buf, sizeof(buf), format, jsodbl->c_double);
1105
1106
0
    if (size < 0)
1107
0
      return -1;
1108
1109
0
    p = strchr(buf, ',');
1110
0
    if (p)
1111
0
      *p = '.';
1112
0
    else
1113
0
      p = strchr(buf, '.');
1114
1115
0
    if (format == std_format || strstr(format, ".0f") == NULL)
1116
0
      format_drops_decimals = 1;
1117
1118
0
    looks_numeric = /* Looks like *some* kind of number */
1119
0
        is_plain_digit(buf[0]) || (size > 1 && buf[0] == '-' && is_plain_digit(buf[1]));
1120
1121
0
    if (size < (int)sizeof(buf) - 2 && looks_numeric && !p && /* Has no decimal point */
1122
0
        strchr(buf, 'e') == NULL && /* Not scientific notation */
1123
0
        format_drops_decimals)
1124
0
    {
1125
      // Ensure it looks like a float, even if snprintf didn't,
1126
      //  unless a custom format is set to omit the decimal.
1127
0
      strcat(buf, ".0");
1128
0
      size += 2;
1129
0
    }
1130
0
    if (p && (flags & JSON_C_TO_STRING_NOZERO))
1131
0
    {
1132
      /* last useful digit, always keep 1 zero */
1133
0
      p++;
1134
0
      for (q = p; *q; q++)
1135
0
      {
1136
0
        if (*q != '0')
1137
0
          p = q;
1138
0
      }
1139
      /* drop trailing zeroes */
1140
0
      if (*p != 0)
1141
0
        *(++p) = 0;
1142
0
      size = p - buf;
1143
0
    }
1144
0
  }
1145
  // although unlikely, snprintf can fail
1146
32
  if (size < 0)
1147
0
    return -1;
1148
1149
32
  if (size >= (int)sizeof(buf))
1150
    // The standard formats are guaranteed not to overrun the buffer,
1151
    // but if a custom one happens to do so, just silently truncate.
1152
0
    size = sizeof(buf) - 1;
1153
32
  printbuf_memappend(pb, buf, size);
1154
32
  return size;
1155
32
}
1156
1157
static int json_object_double_to_json_string_default(struct json_object *jso, struct printbuf *pb,
1158
                                                     int level, int flags)
1159
32
{
1160
32
  return json_object_double_to_json_string_format(jso, pb, level, flags, NULL);
1161
32
}
1162
1163
int json_object_double_to_json_string(struct json_object *jso, struct printbuf *pb, int level,
1164
                                      int flags)
1165
0
{
1166
0
  return json_object_double_to_json_string_format(jso, pb, level, flags,
1167
0
                                                  (const char *)jso->_userdata);
1168
0
}
1169
1170
struct json_object *json_object_new_double(double d)
1171
709
{
1172
709
  struct json_object_double *jso = JSON_OBJECT_NEW(double);
1173
709
  if (!jso)
1174
0
    return NULL;
1175
709
  jso->base._to_json_string = &json_object_double_to_json_string_default;
1176
709
  jso->c_double = d;
1177
709
  return &jso->base;
1178
709
}
1179
1180
struct json_object *json_object_new_double_s(double d, const char *ds)
1181
493
{
1182
493
  char *new_ds;
1183
493
  struct json_object *jso = json_object_new_double(d);
1184
493
  if (!jso)
1185
0
    return NULL;
1186
1187
493
  new_ds = strdup(ds);
1188
493
  if (!new_ds)
1189
0
  {
1190
0
    json_object_generic_delete(jso);
1191
0
    errno = ENOMEM;
1192
0
    return NULL;
1193
0
  }
1194
493
  json_object_set_serializer(jso, _json_object_userdata_to_json_string, new_ds,
1195
493
                             json_object_free_userdata);
1196
493
  return jso;
1197
493
}
1198
1199
/*
1200
 * A wrapper around json_object_userdata_to_json_string() used only
1201
 * by json_object_new_double_s() just so json_object_set_double() can
1202
 * detect when it needs to reset the serializer to the default.
1203
 */
1204
static int _json_object_userdata_to_json_string(struct json_object *jso, struct printbuf *pb,
1205
                                                int level, int flags)
1206
120
{
1207
120
  return json_object_userdata_to_json_string(jso, pb, level, flags);
1208
120
}
1209
1210
int json_object_userdata_to_json_string(struct json_object *jso, struct printbuf *pb, int level,
1211
                                        int flags)
1212
120
{
1213
120
  int userdata_len = strlen((const char *)jso->_userdata);
1214
120
  printbuf_memappend(pb, (const char *)jso->_userdata, userdata_len);
1215
120
  return userdata_len;
1216
120
}
1217
1218
void json_object_free_userdata(struct json_object *jso, void *userdata)
1219
549
{
1220
549
  free(userdata);
1221
549
}
1222
1223
double json_object_get_double(const struct json_object *jso)
1224
0
{
1225
0
  double cdouble;
1226
0
  char *errPtr = NULL;
1227
1228
0
  if (!jso)
1229
0
    return 0.0;
1230
0
  switch (jso->o_type)
1231
0
  {
1232
0
  case json_type_double: return JC_DOUBLE_C(jso)->c_double;
1233
0
  case json_type_int:
1234
0
    switch (JC_INT_C(jso)->cint_type)
1235
0
    {
1236
0
    case json_object_int_type_int64: return JC_INT_C(jso)->cint.c_int64;
1237
0
    case json_object_int_type_uint64: return JC_INT_C(jso)->cint.c_uint64;
1238
0
    default: json_abort("invalid cint_type");
1239
0
    }
1240
0
  case json_type_boolean: return JC_BOOL_C(jso)->c_boolean;
1241
0
  case json_type_string:
1242
0
    errno = 0;
1243
0
    cdouble = strtod(get_string_component(jso), &errPtr);
1244
1245
    /* if conversion stopped at the first character, return 0.0 */
1246
0
    if (errPtr == get_string_component(jso))
1247
0
    {
1248
0
      errno = EINVAL;
1249
0
      return 0.0;
1250
0
    }
1251
1252
    /*
1253
     * Check that the conversion terminated on something sensible
1254
     *
1255
     * For example, { "pay" : 123AB } would parse as 123.
1256
     */
1257
0
    if (*errPtr != '\0')
1258
0
    {
1259
0
      errno = EINVAL;
1260
0
      return 0.0;
1261
0
    }
1262
1263
    /*
1264
     * If strtod encounters a string which would exceed the
1265
     * capacity of a double, it returns +/- HUGE_VAL and sets
1266
     * errno to ERANGE. But +/- HUGE_VAL is also a valid result
1267
     * from a conversion, so we need to check errno.
1268
     *
1269
     * Underflow also sets errno to ERANGE, but it returns 0 in
1270
     * that case, which is what we will return anyway.
1271
     *
1272
     * See CERT guideline ERR30-C
1273
     */
1274
0
    if ((HUGE_VAL == cdouble || -HUGE_VAL == cdouble) && (ERANGE == errno))
1275
0
      cdouble = 0.0;
1276
0
    return cdouble;
1277
0
  default: errno = EINVAL; return 0.0;
1278
0
  }
1279
0
}
1280
1281
int json_object_set_double(struct json_object *jso, double new_value)
1282
0
{
1283
0
  if (!jso || jso->o_type != json_type_double)
1284
0
    return 0;
1285
0
  JC_DOUBLE(jso)->c_double = new_value;
1286
0
  if (jso->_to_json_string == &_json_object_userdata_to_json_string)
1287
0
    json_object_set_serializer(jso, NULL, NULL, NULL);
1288
0
  return 1;
1289
0
}
1290
1291
/* json_object_string */
1292
1293
static int json_object_string_to_json_string(struct json_object *jso, struct printbuf *pb,
1294
                                             int level, int flags)
1295
62.9k
{
1296
62.9k
  ssize_t len = JC_STRING(jso)->len;
1297
62.9k
  if (flags & JSON_C_TO_STRING_COLOR)
1298
0
    printbuf_strappend(pb, ANSI_COLOR_FG_GREEN);
1299
62.9k
  printbuf_strappend(pb, "\"");
1300
62.9k
  json_escape_str(pb, get_string_component(jso), len < 0 ? -(ssize_t)len : len, flags);
1301
62.9k
  printbuf_strappend(pb, "\"");
1302
62.9k
  if (flags & JSON_C_TO_STRING_COLOR)
1303
0
    printbuf_strappend(pb, ANSI_COLOR_RESET);
1304
62.9k
  return 0;
1305
62.9k
}
1306
1307
static void json_object_string_delete(struct json_object *jso)
1308
111k
{
1309
111k
  if (JC_STRING(jso)->len < 0)
1310
0
    free(JC_STRING(jso)->c_string.pdata);
1311
111k
  json_object_generic_delete(jso);
1312
111k
}
1313
1314
static struct json_object *_json_object_new_string(const char *s, const size_t len)
1315
111k
{
1316
111k
  size_t objsize;
1317
111k
  struct json_object_string *jso;
1318
1319
  /*
1320
   * Structures           Actual memory layout
1321
   * -------------------  --------------------
1322
   * [json_object_string  [json_object_string
1323
   *  [json_object]        [json_object]
1324
   *  ...other fields...   ...other fields...
1325
   *  c_string]            len
1326
   *                       bytes
1327
   *                       of
1328
   *                       string
1329
   *                       data
1330
   *                       \0]
1331
   */
1332
111k
  if (len > (SSIZE_T_MAX - (sizeof(*jso) - sizeof(jso->c_string)) - 1))
1333
0
    return NULL;
1334
111k
  objsize = (sizeof(*jso) - sizeof(jso->c_string)) + len + 1;
1335
111k
  if (len < sizeof(void *))
1336
    // We need a minimum size to support json_object_set_string() mutability
1337
    // so we can stuff a pointer into pdata :(
1338
73.1k
    objsize += sizeof(void *) - len;
1339
1340
111k
  jso = (struct json_object_string *)json_object_new(json_type_string, objsize,
1341
111k
                                                     &json_object_string_to_json_string);
1342
1343
111k
  if (!jso)
1344
0
    return NULL;
1345
111k
  jso->len = len;
1346
111k
  memcpy(jso->c_string.idata, s, len);
1347
  // Cast below needed for Clang UB sanitizer
1348
111k
  ((char *)jso->c_string.idata)[len] = '\0';
1349
111k
  return &jso->base;
1350
111k
}
1351
1352
struct json_object *json_object_new_string(const char *s)
1353
0
{
1354
0
  return _json_object_new_string(s, strlen(s));
1355
0
}
1356
1357
struct json_object *json_object_new_string_len(const char *s, const int len)
1358
111k
{
1359
111k
  return _json_object_new_string(s, len);
1360
111k
}
1361
1362
const char *json_object_get_string(struct json_object *jso)
1363
188k
{
1364
188k
  if (!jso)
1365
0
    return NULL;
1366
188k
  switch (jso->o_type)
1367
188k
  {
1368
188k
  case json_type_string: return get_string_component(jso);
1369
34
  default: return json_object_to_json_string(jso);
1370
188k
  }
1371
188k
}
1372
1373
static inline ssize_t _json_object_get_string_len(const struct json_object_string *jso)
1374
23.0k
{
1375
23.0k
  ssize_t len;
1376
23.0k
  len = jso->len;
1377
23.0k
  return (len < 0) ? -(ssize_t)len : len;
1378
23.0k
}
1379
int json_object_get_string_len(const struct json_object *jso)
1380
0
{
1381
0
  if (!jso)
1382
0
    return 0;
1383
0
  switch (jso->o_type)
1384
0
  {
1385
0
  case json_type_string: return _json_object_get_string_len(JC_STRING_C(jso));
1386
0
  default: return 0;
1387
0
  }
1388
0
}
1389
1390
static int _json_object_set_string_len(json_object *jso, const char *s, size_t len)
1391
0
{
1392
0
  char *dstbuf;
1393
0
  ssize_t curlen;
1394
0
  ssize_t newlen;
1395
0
  if (jso == NULL || jso->o_type != json_type_string)
1396
0
    return 0;
1397
1398
0
  if (len >= INT_MAX - 1)
1399
    // jso->len is a signed ssize_t, so it can't hold the
1400
    // full size_t range. json_object_get_string_len returns
1401
    // length as int, cap length at INT_MAX.
1402
0
    return 0;
1403
1404
0
  curlen = JC_STRING(jso)->len;
1405
0
  if (curlen < 0) {
1406
0
    if (len == 0) {
1407
0
      free(JC_STRING(jso)->c_string.pdata);
1408
0
      JC_STRING(jso)->len = curlen = 0;
1409
0
    } else {
1410
0
      curlen = -curlen;
1411
0
    }
1412
0
  }
1413
1414
0
  newlen = len;
1415
0
  dstbuf = get_string_component_mutable(jso);
1416
1417
0
  if ((ssize_t)len > curlen)
1418
0
  {
1419
    // We have no way to return the new ptr from realloc(jso, newlen)
1420
    // and we have no way of knowing whether there's extra room available
1421
    // so we need to stuff a pointer in to pdata :(
1422
0
    dstbuf = (char *)malloc(len + 1);
1423
0
    if (dstbuf == NULL)
1424
0
      return 0;
1425
0
    if (JC_STRING(jso)->len < 0)
1426
0
      free(JC_STRING(jso)->c_string.pdata);
1427
0
    JC_STRING(jso)->c_string.pdata = dstbuf;
1428
0
    newlen = -(ssize_t)len;
1429
0
  }
1430
0
  else if (JC_STRING(jso)->len < 0)
1431
0
  {
1432
    // We've got enough room in the separate allocated buffer,
1433
    // so use it as-is and continue to indicate that pdata is used.
1434
0
    newlen = -(ssize_t)len;
1435
0
  }
1436
1437
0
  memcpy(dstbuf, (const void *)s, len);
1438
0
  dstbuf[len] = '\0';
1439
0
  JC_STRING(jso)->len = newlen;
1440
0
  return 1;
1441
0
}
1442
1443
int json_object_set_string(json_object *jso, const char *s)
1444
0
{
1445
0
  return _json_object_set_string_len(jso, s, strlen(s));
1446
0
}
1447
1448
int json_object_set_string_len(json_object *jso, const char *s, int len)
1449
0
{
1450
0
  return _json_object_set_string_len(jso, s, len);
1451
0
}
1452
1453
/* json_object_array */
1454
1455
static int json_object_array_to_json_string(struct json_object *jso, struct printbuf *pb, int level,
1456
                                            int flags)
1457
8.65k
{
1458
8.65k
  int had_children = 0;
1459
8.65k
  size_t ii;
1460
1461
8.65k
  printbuf_strappend(pb, "[");
1462
31.5k
  for (ii = 0; ii < json_object_array_length(jso); ii++)
1463
22.9k
  {
1464
22.9k
    struct json_object *val;
1465
22.9k
    if (had_children)
1466
14.4k
    {
1467
14.4k
      printbuf_strappend(pb, ",");
1468
14.4k
    }
1469
22.9k
    if (flags & JSON_C_TO_STRING_PRETTY)
1470
0
      printbuf_strappend(pb, "\n");
1471
22.9k
    had_children = 1;
1472
22.9k
    if (flags & JSON_C_TO_STRING_SPACED && !(flags & JSON_C_TO_STRING_PRETTY))
1473
0
      printbuf_strappend(pb, " ");
1474
22.9k
    indent(pb, level + 1, flags);
1475
22.9k
    val = json_object_array_get_idx(jso, ii);
1476
22.9k
    if (val == NULL) {
1477
1478
34
      if (flags & JSON_C_TO_STRING_COLOR)
1479
0
        printbuf_strappend(pb, ANSI_COLOR_FG_MAGENTA);
1480
34
      printbuf_strappend(pb, "null");
1481
34
      if (flags & JSON_C_TO_STRING_COLOR)
1482
0
        printbuf_strappend(pb, ANSI_COLOR_RESET);
1483
1484
22.8k
    } else if (val->_to_json_string(val, pb, level + 1, flags) < 0)
1485
0
      return -1;
1486
22.9k
  }
1487
8.65k
  if ((flags & JSON_C_TO_STRING_PRETTY) && had_children)
1488
0
  {
1489
0
    printbuf_strappend(pb, "\n");
1490
0
    indent(pb, level, flags);
1491
0
  }
1492
1493
8.65k
  if (flags & JSON_C_TO_STRING_SPACED && !(flags & JSON_C_TO_STRING_PRETTY))
1494
0
    return printbuf_strappend(pb, " ]");
1495
8.65k
  return printbuf_strappend(pb, "]");
1496
8.65k
}
1497
1498
static void json_object_array_entry_free(void *data)
1499
47.9k
{
1500
47.9k
  json_object_put((struct json_object *)data);
1501
47.9k
}
1502
1503
static void json_object_array_delete(struct json_object *jso)
1504
15.0k
{
1505
15.0k
  array_list_free(JC_ARRAY(jso)->c_array);
1506
15.0k
  json_object_generic_delete(jso);
1507
15.0k
}
1508
1509
struct json_object *json_object_new_array(void)
1510
15.0k
{
1511
15.0k
  return json_object_new_array_ext(ARRAY_LIST_DEFAULT_SIZE);
1512
15.0k
}
1513
struct json_object *json_object_new_array_ext(int initial_size)
1514
15.0k
{
1515
15.0k
  struct json_object_array *jso = JSON_OBJECT_NEW(array);
1516
15.0k
  if (!jso)
1517
0
    return NULL;
1518
15.0k
  jso->c_array = array_list_new2(&json_object_array_entry_free, initial_size);
1519
15.0k
  if (jso->c_array == NULL)
1520
0
  {
1521
0
    free(jso);
1522
0
    return NULL;
1523
0
  }
1524
15.0k
  return &jso->base;
1525
15.0k
}
1526
1527
struct array_list *json_object_get_array(const struct json_object *jso)
1528
0
{
1529
0
  if (!jso)
1530
0
    return NULL;
1531
0
  switch (jso->o_type)
1532
0
  {
1533
0
  case json_type_array: return JC_ARRAY_C(jso)->c_array;
1534
0
  default: return NULL;
1535
0
  }
1536
0
}
1537
1538
void json_object_array_sort(struct json_object *jso, int (*sort_fn)(const void *, const void *))
1539
0
{
1540
0
  assert(json_object_get_type(jso) == json_type_array);
1541
0
  array_list_sort(JC_ARRAY(jso)->c_array, sort_fn);
1542
0
}
1543
1544
struct json_object *json_object_array_bsearch(const struct json_object *key,
1545
                                              const struct json_object *jso,
1546
                                              int (*sort_fn)(const void *, const void *))
1547
0
{
1548
0
  struct json_object **result;
1549
1550
0
  assert(json_object_get_type(jso) == json_type_array);
1551
0
  result = (struct json_object **)array_list_bsearch((const void **)(void *)&key,
1552
0
                                                     JC_ARRAY_C(jso)->c_array, sort_fn);
1553
1554
0
  if (!result)
1555
0
    return NULL;
1556
0
  return *result;
1557
0
}
1558
1559
size_t json_object_array_length(const struct json_object *jso)
1560
116k
{
1561
116k
  assert(json_object_get_type(jso) == json_type_array);
1562
116k
  return array_list_length(JC_ARRAY_C(jso)->c_array);
1563
116k
}
1564
1565
int json_object_array_add(struct json_object *jso, struct json_object *val)
1566
48.0k
{
1567
48.0k
  assert(json_object_get_type(jso) == json_type_array);
1568
48.0k
  return array_list_add(JC_ARRAY(jso)->c_array, val);
1569
48.0k
}
1570
1571
int json_object_array_insert_idx(struct json_object *jso, size_t idx, struct json_object *val)
1572
0
{
1573
0
  assert(json_object_get_type(jso) == json_type_array);
1574
0
  return array_list_insert_idx(JC_ARRAY(jso)->c_array, idx, val);
1575
0
}
1576
1577
int json_object_array_put_idx(struct json_object *jso, size_t idx, struct json_object *val)
1578
0
{
1579
0
  assert(json_object_get_type(jso) == json_type_array);
1580
0
  return array_list_put_idx(JC_ARRAY(jso)->c_array, idx, val);
1581
0
}
1582
1583
int json_object_array_del_idx(struct json_object *jso, size_t idx, size_t count)
1584
0
{
1585
0
  assert(json_object_get_type(jso) == json_type_array);
1586
0
  return array_list_del_idx(JC_ARRAY(jso)->c_array, idx, count);
1587
0
}
1588
1589
struct json_object *json_object_array_get_idx(const struct json_object *jso, size_t idx)
1590
93.3k
{
1591
93.3k
  assert(json_object_get_type(jso) == json_type_array);
1592
93.3k
  return (struct json_object *)array_list_get_idx(JC_ARRAY_C(jso)->c_array, idx);
1593
93.3k
}
1594
1595
static int json_array_equal(struct json_object *jso1, struct json_object *jso2)
1596
0
{
1597
0
  size_t len, i;
1598
1599
0
  len = json_object_array_length(jso1);
1600
0
  if (len != json_object_array_length(jso2))
1601
0
    return 0;
1602
1603
0
  for (i = 0; i < len; i++)
1604
0
  {
1605
0
    if (!json_object_equal(json_object_array_get_idx(jso1, i),
1606
0
                           json_object_array_get_idx(jso2, i)))
1607
0
      return 0;
1608
0
  }
1609
0
  return 1;
1610
0
}
1611
1612
int json_object_array_shrink(struct json_object *jso, int empty_slots)
1613
11.4k
{
1614
11.4k
  if (empty_slots < 0)
1615
0
    json_abort("json_object_array_shrink called with negative empty_slots");
1616
11.4k
  return array_list_shrink(JC_ARRAY(jso)->c_array, empty_slots);
1617
11.4k
}
1618
1619
struct json_object *json_object_new_null(void)
1620
0
{
1621
0
  return NULL;
1622
0
}
1623
1624
static int json_object_all_values_equal(struct json_object *jso1, struct json_object *jso2)
1625
0
{
1626
0
  struct json_object_iter iter;
1627
0
  struct json_object *sub;
1628
1629
0
  assert(json_object_get_type(jso1) == json_type_object);
1630
0
  assert(json_object_get_type(jso2) == json_type_object);
1631
  /* Iterate over jso1 keys and see if they exist and are equal in jso2 */
1632
0
  json_object_object_foreachC(jso1, iter)
1633
0
  {
1634
0
    if (!lh_table_lookup_ex(JC_OBJECT(jso2)->c_object, (void *)iter.key,
1635
0
                            (void **)(void *)&sub))
1636
0
      return 0;
1637
0
    if (!json_object_equal(iter.val, sub))
1638
0
      return 0;
1639
0
  }
1640
1641
  /* Iterate over jso2 keys to see if any exist that are not in jso1 */
1642
0
  json_object_object_foreachC(jso2, iter)
1643
0
  {
1644
0
    if (!lh_table_lookup_ex(JC_OBJECT(jso1)->c_object, (void *)iter.key,
1645
0
                            (void **)(void *)&sub))
1646
0
      return 0;
1647
0
  }
1648
1649
0
  return 1;
1650
0
}
1651
1652
int json_object_equal(struct json_object *jso1, struct json_object *jso2)
1653
0
{
1654
0
  if (jso1 == jso2)
1655
0
    return 1;
1656
1657
0
  if (!jso1 || !jso2)
1658
0
    return 0;
1659
1660
0
  if (jso1->o_type != jso2->o_type)
1661
0
    return 0;
1662
1663
0
  switch (jso1->o_type)
1664
0
  {
1665
0
  case json_type_boolean: return (JC_BOOL(jso1)->c_boolean == JC_BOOL(jso2)->c_boolean);
1666
1667
0
  case json_type_double: return (JC_DOUBLE(jso1)->c_double == JC_DOUBLE(jso2)->c_double);
1668
1669
0
  case json_type_int:
1670
0
  {
1671
0
    struct json_object_int *int1 = JC_INT(jso1);
1672
0
    struct json_object_int *int2 = JC_INT(jso2);
1673
0
    if (int1->cint_type == json_object_int_type_int64)
1674
0
    {
1675
0
      if (int2->cint_type == json_object_int_type_int64)
1676
0
        return (int1->cint.c_int64 == int2->cint.c_int64);
1677
0
      if (int1->cint.c_int64 < 0)
1678
0
        return 0;
1679
0
      return ((uint64_t)int1->cint.c_int64 == int2->cint.c_uint64);
1680
0
    }
1681
    // else jso1 is a uint64
1682
0
    if (int2->cint_type == json_object_int_type_uint64)
1683
0
      return (int1->cint.c_uint64 == int2->cint.c_uint64);
1684
0
    if (int2->cint.c_int64 < 0)
1685
0
      return 0;
1686
0
    return (int1->cint.c_uint64 == (uint64_t)int2->cint.c_int64);
1687
0
  }
1688
1689
0
  case json_type_string:
1690
0
  {
1691
0
    return (_json_object_get_string_len(JC_STRING(jso1)) ==
1692
0
                _json_object_get_string_len(JC_STRING(jso2)) &&
1693
0
            memcmp(get_string_component(jso1), get_string_component(jso2),
1694
0
                   _json_object_get_string_len(JC_STRING(jso1))) == 0);
1695
0
  }
1696
1697
0
  case json_type_object: return json_object_all_values_equal(jso1, jso2);
1698
1699
0
  case json_type_array: return json_array_equal(jso1, jso2);
1700
1701
0
  case json_type_null: return 1;
1702
0
  };
1703
1704
0
  return 0;
1705
0
}
1706
1707
static int json_object_copy_serializer_data(struct json_object *src, struct json_object *dst)
1708
43.5k
{
1709
43.5k
  if (!src->_userdata && !src->_user_delete)
1710
43.4k
    return 0;
1711
1712
56
  if (dst->_to_json_string == json_object_userdata_to_json_string ||
1713
56
      dst->_to_json_string == _json_object_userdata_to_json_string)
1714
56
  {
1715
56
    char *p;
1716
56
    assert(src->_userdata);
1717
56
    p = strdup(src->_userdata);
1718
56
    if (p == NULL)
1719
0
    {
1720
0
      _json_c_set_last_err("json_object_copy_serializer_data: out of memory\n");
1721
0
      return -1;
1722
0
    }
1723
56
    dst->_userdata = p;
1724
56
  }
1725
  // else if ... other supported serializers ...
1726
0
  else
1727
0
  {
1728
0
    _json_c_set_last_err(
1729
0
        "json_object_copy_serializer_data: unable to copy unknown serializer data: "
1730
0
        "%p\n", (void *)dst->_to_json_string);
1731
0
    return -1;
1732
0
  }
1733
56
  dst->_user_delete = src->_user_delete;
1734
56
  return 0;
1735
56
}
1736
1737
/**
1738
 * The default shallow copy implementation.  Simply creates a new object of the same
1739
 * type but does *not* copy over _userdata nor retain any custom serializer.
1740
 * If custom serializers are in use, json_object_deep_copy() must be passed a shallow copy
1741
 * implementation that is aware of how to copy them.
1742
 *
1743
 * This always returns -1 or 1.  It will never return 2 since it does not copy the serializer.
1744
 */
1745
int json_c_shallow_copy_default(json_object *src, json_object *parent, const char *key,
1746
                                size_t index, json_object **dst)
1747
43.5k
{
1748
43.5k
  switch (src->o_type)
1749
43.5k
  {
1750
20
  case json_type_boolean: *dst = json_object_new_boolean(JC_BOOL(src)->c_boolean); break;
1751
1752
72
  case json_type_double: *dst = json_object_new_double(JC_DOUBLE(src)->c_double); break;
1753
1754
745
  case json_type_int:
1755
745
    switch (JC_INT(src)->cint_type)
1756
745
    {
1757
722
    case json_object_int_type_int64:
1758
722
      *dst = json_object_new_int64(JC_INT(src)->cint.c_int64);
1759
722
      break;
1760
23
    case json_object_int_type_uint64:
1761
23
      *dst = json_object_new_uint64(JC_INT(src)->cint.c_uint64);
1762
23
      break;
1763
0
    default: json_abort("invalid cint_type");
1764
745
    }
1765
745
    break;
1766
1767
23.0k
  case json_type_string:
1768
23.0k
    *dst = json_object_new_string_len(get_string_component(src),
1769
23.0k
                                      _json_object_get_string_len(JC_STRING(src)));
1770
23.0k
    break;
1771
1772
16.3k
  case json_type_object: *dst = json_object_new_object(); break;
1773
1774
3.20k
  case json_type_array: *dst = json_object_new_array(); break;
1775
1776
0
  default: errno = EINVAL; return -1;
1777
43.5k
  }
1778
1779
43.5k
  if (!*dst)
1780
0
  {
1781
0
    errno = ENOMEM;
1782
0
    return -1;
1783
0
  }
1784
43.5k
  (*dst)->_to_json_string = src->_to_json_string;
1785
  // _userdata and _user_delete are copied later
1786
43.5k
  return 1;
1787
43.5k
}
1788
1789
/*
1790
 * The actual guts of json_object_deep_copy(), with a few additional args
1791
 * needed so we can keep track of where we are within the object tree.
1792
 *
1793
 * Note: caller is responsible for freeing *dst if this fails and returns -1.
1794
 */
1795
static int json_object_deep_copy_recursive(struct json_object *src, struct json_object *parent,
1796
                                           const char *key_in_parent, size_t index_in_parent,
1797
                                           struct json_object **dst,
1798
                                           json_c_shallow_copy_fn *shallow_copy)
1799
43.5k
{
1800
43.5k
  struct json_object_iter iter;
1801
43.5k
  size_t src_array_len, ii;
1802
1803
43.5k
  int shallow_copy_rc = 0;
1804
43.5k
  shallow_copy_rc = shallow_copy(src, parent, key_in_parent, index_in_parent, dst);
1805
  /* -1=error, 1=object created ok, 2=userdata set */
1806
43.5k
  if (shallow_copy_rc < 1)
1807
0
  {
1808
0
    errno = EINVAL;
1809
0
    return -1;
1810
0
  }
1811
43.5k
  assert(*dst != NULL);
1812
1813
43.5k
  switch (src->o_type)
1814
43.5k
  {
1815
16.3k
  case json_type_object:
1816
16.3k
    json_object_object_foreachC(src, iter)
1817
31.9k
    {
1818
31.9k
      struct json_object *jso = NULL;
1819
      /* This handles the `json_type_null` case */
1820
31.9k
      if (!iter.val)
1821
30
        jso = NULL;
1822
31.9k
      else if (json_object_deep_copy_recursive(iter.val, src, iter.key, UINT_MAX,
1823
31.9k
                                               &jso, shallow_copy) < 0)
1824
0
      {
1825
0
        json_object_put(jso);
1826
0
        return -1;
1827
0
      }
1828
1829
31.9k
      if (json_object_object_add(*dst, iter.key, jso) < 0)
1830
0
      {
1831
0
        json_object_put(jso);
1832
0
        return -1;
1833
0
      }
1834
31.9k
    }
1835
16.3k
    break;
1836
1837
16.3k
  case json_type_array:
1838
3.20k
    src_array_len = json_object_array_length(src);
1839
12.5k
    for (ii = 0; ii < src_array_len; ii++)
1840
9.30k
    {
1841
9.30k
      struct json_object *jso = NULL;
1842
9.30k
      struct json_object *jso1 = json_object_array_get_idx(src, ii);
1843
      /* This handles the `json_type_null` case */
1844
9.30k
      if (!jso1)
1845
17
        jso = NULL;
1846
9.28k
      else if (json_object_deep_copy_recursive(jso1, src, NULL, ii, &jso,
1847
9.28k
                                               shallow_copy) < 0)
1848
0
      {
1849
0
        json_object_put(jso);
1850
0
        return -1;
1851
0
      }
1852
1853
9.30k
      if (json_object_array_add(*dst, jso) < 0)
1854
0
      {
1855
0
        json_object_put(jso);
1856
0
        return -1;
1857
0
      }
1858
9.30k
    }
1859
3.20k
    break;
1860
1861
23.9k
  default:
1862
23.9k
    break;
1863
    /* else, nothing to do, shallow_copy already did. */
1864
43.5k
  }
1865
1866
43.5k
  if (shallow_copy_rc != 2)
1867
43.5k
    return json_object_copy_serializer_data(src, *dst);
1868
1869
0
  return 0;
1870
43.5k
}
1871
1872
int json_object_deep_copy(struct json_object *src, struct json_object **dst,
1873
                          json_c_shallow_copy_fn *shallow_copy)
1874
2.29k
{
1875
2.29k
  int rc;
1876
1877
  /* Check if arguments are sane ; *dst must not point to a non-NULL object */
1878
2.29k
  if (!src || !dst || *dst)
1879
0
  {
1880
0
    errno = EINVAL;
1881
0
    return -1;
1882
0
  }
1883
1884
2.29k
  if (shallow_copy == NULL)
1885
2.29k
    shallow_copy = json_c_shallow_copy_default;
1886
1887
2.29k
  rc = json_object_deep_copy_recursive(src, NULL, NULL, UINT_MAX, dst, shallow_copy);
1888
2.29k
  if (rc < 0)
1889
0
  {
1890
0
    json_object_put(*dst);
1891
0
    *dst = NULL;
1892
0
  }
1893
1894
2.29k
  return rc;
1895
2.29k
}
1896
1897
static void json_abort(const char *message)
1898
0
{
1899
0
  if (message != NULL)
1900
0
    fprintf(stderr, "json-c aborts with error: %s\n", message);
1901
0
  abort();
1902
0
}