Coverage Report

Created: 2026-05-30 06:06

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