Coverage Report

Created: 2025-12-14 06:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/php-src/ext/json/json_encoder.c
Line
Count
Source
1
/*
2
  +----------------------------------------------------------------------+
3
  | Copyright (c) The PHP Group                                          |
4
  +----------------------------------------------------------------------+
5
  | This source file is subject to version 3.01 of the PHP license,      |
6
  | that is bundled with this package in the file LICENSE, and is        |
7
  | available through the world-wide-web at the following url:           |
8
  | https://www.php.net/license/3_01.txt                                 |
9
  | If you did not receive a copy of the PHP license and are unable to   |
10
  | obtain it through the world-wide-web, please send a note to          |
11
  | license@php.net so we can mail you a copy immediately.               |
12
  +----------------------------------------------------------------------+
13
  | Author: Omar Kilani <omar@php.net>                                   |
14
  |         Jakub Zelenka <bukka@php.net>                                |
15
  +----------------------------------------------------------------------+
16
*/
17
18
#ifdef HAVE_CONFIG_H
19
#include <config.h>
20
#endif
21
22
#include "php.h"
23
#include "ext/standard/html.h"
24
#include "zend_smart_str.h"
25
#include "php_json.h"
26
#include "php_json_encoder.h"
27
#include "zend_portability.h"
28
#include <zend_exceptions.h>
29
#include "zend_enum.h"
30
#include "zend_property_hooks.h"
31
#include "zend_lazy_objects.h"
32
33
static const char digits[] = "0123456789abcdef";
34
35
static zend_always_inline bool php_json_check_stack_limit(void)
36
98
{
37
98
#ifdef ZEND_CHECK_STACK_LIMIT
38
98
  return zend_call_stack_overflowed(EG(stack_limit));
39
#else
40
  return false;
41
#endif
42
98
}
43
44
/* {{{ Pretty printing support functions */
45
46
static inline void php_json_pretty_print_char(smart_str *buf, int options, char c) /* {{{ */
47
635
{
48
635
  if (options & PHP_JSON_PRETTY_PRINT) {
49
0
    smart_str_appendc(buf, c);
50
0
  }
51
635
}
52
/* }}} */
53
54
static inline void php_json_pretty_print_indent(smart_str *buf, int options, const php_json_encoder *encoder) /* {{{ */
55
491
{
56
491
  if (options & PHP_JSON_PRETTY_PRINT) {
57
0
    for (int i = 0; i < encoder->depth; ++i) {
58
0
      smart_str_appendl(buf, "    ", 4);
59
0
    }
60
0
  }
61
491
}
62
/* }}} */
63
64
/* }}} */
65
66
static
67
#if defined(_MSC_VER) && defined(_M_ARM64)
68
// MSVC bug: https://developercommunity.visualstudio.com/t/corrupt-optimization-on-arm64-with-Ox-/10102551
69
zend_never_inline
70
#else
71
inline
72
#endif
73
bool php_json_is_valid_double(double d) /* {{{ */
74
47
{
75
47
  return !zend_isinf(d) && !zend_isnan(d);
76
47
}
77
/* }}} */
78
79
static inline void php_json_encode_double(smart_str *buf, double d, int options) /* {{{ */
80
47
{
81
47
  size_t len;
82
47
  char num[ZEND_DOUBLE_MAX_LENGTH];
83
84
47
  zend_gcvt(d, (int)PG(serialize_precision), '.', 'e', num);
85
47
  len = strlen(num);
86
47
  if (options & PHP_JSON_PRESERVE_ZERO_FRACTION && strchr(num, '.') == NULL && len < ZEND_DOUBLE_MAX_LENGTH - 2) {
87
25
    num[len++] = '.';
88
25
    num[len++] = '0';
89
25
    num[len] = '\0';
90
25
  }
91
47
  smart_str_appendl(buf, num, len);
92
47
}
93
/* }}} */
94
95
#define PHP_JSON_HASH_PROTECT_RECURSION(_tmp_ht) \
96
98
  do { \
97
98
    if (_tmp_ht) { \
98
96
      GC_TRY_PROTECT_RECURSION(_tmp_ht); \
99
96
    } \
100
98
  } while (0)
101
102
#define PHP_JSON_HASH_UNPROTECT_RECURSION(_tmp_ht) \
103
98
  do { \
104
98
    if (_tmp_ht) { \
105
96
      GC_TRY_UNPROTECT_RECURSION(_tmp_ht); \
106
96
    } \
107
98
  } while (0)
108
109
static zend_result php_json_encode_array(smart_str *buf, zval *val, int options, php_json_encoder *encoder) /* {{{ */
110
98
{
111
98
  bool encode_as_object = options & PHP_JSON_FORCE_OBJECT;
112
98
  HashTable *myht, *prop_ht;
113
98
  zend_refcounted *recursion_rc;
114
115
98
  if (php_json_check_stack_limit()) {
116
0
    encoder->error_code = PHP_JSON_ERROR_DEPTH;
117
0
    if (options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR) {
118
0
      smart_str_appendl(buf, "null", 4);
119
0
    }
120
0
    return FAILURE;
121
0
  }
122
123
98
  if (Z_TYPE_P(val) == IS_ARRAY) {
124
22
    myht = Z_ARRVAL_P(val);
125
22
    recursion_rc = (zend_refcounted *)myht;
126
22
    prop_ht = NULL;
127
22
    encode_as_object = encode_as_object || !zend_array_is_list(myht);
128
76
  } else if (Z_OBJ_P(val)->properties == NULL
129
36
   && Z_OBJ_HT_P(val)->get_properties_for == NULL
130
34
   && Z_OBJ_HT_P(val)->get_properties == zend_std_get_properties
131
34
   && Z_OBJ_P(val)->ce->num_hooked_props == 0
132
10
   && !zend_object_is_lazy(Z_OBJ_P(val))) {
133
    /* Optimized version without rebuilding properties HashTable */
134
0
    zend_object *obj = Z_OBJ_P(val);
135
0
    const zend_class_entry *ce = obj->ce;
136
137
0
    if (GC_IS_RECURSIVE(obj)) {
138
0
      encoder->error_code = PHP_JSON_ERROR_RECURSION;
139
0
      smart_str_appendl(buf, "null", 4);
140
0
      return FAILURE;
141
0
    }
142
143
0
    PHP_JSON_HASH_PROTECT_RECURSION(obj);
144
145
0
    smart_str_appendc(buf, '{');
146
147
0
    ++encoder->depth;
148
149
0
    for (int i = 0; i < ce->default_properties_count; i++) {
150
0
      zend_property_info *prop_info = ce->properties_info_table[i];
151
0
      if (!prop_info) {
152
0
        continue;
153
0
      }
154
0
      if (ZSTR_VAL(prop_info->name)[0] == '\0' && ZSTR_LEN(prop_info->name) > 0) {
155
        /* Skip protected and private members. */
156
0
        continue;
157
0
      }
158
0
      zval *prop = OBJ_PROP(obj, prop_info->offset);
159
0
      if (Z_TYPE_P(prop) == IS_UNDEF) {
160
0
        continue;
161
0
      }
162
163
0
      php_json_pretty_print_char(buf, options, '\n');
164
0
      php_json_pretty_print_indent(buf, options, encoder);
165
166
0
      if (php_json_escape_string(buf, ZSTR_VAL(prop_info->name), ZSTR_LEN(prop_info->name),
167
0
          options & ~PHP_JSON_NUMERIC_CHECK, encoder) == FAILURE &&
168
0
          (options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR) &&
169
0
          buf->s) {
170
0
        ZSTR_LEN(buf->s) -= 4;
171
0
        smart_str_appendl(buf, "\"\"", 2);
172
0
      }
173
174
0
      smart_str_appendc(buf, ':');
175
0
      php_json_pretty_print_char(buf, options, ' ');
176
177
0
      if (php_json_encode_zval(buf, prop, options, encoder) == FAILURE &&
178
0
          !(options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR)) {
179
0
        PHP_JSON_HASH_UNPROTECT_RECURSION(obj);
180
0
        return FAILURE;
181
0
      }
182
183
0
      smart_str_appendc(buf, ',');
184
0
    }
185
186
0
    bool empty = ZSTR_VAL(buf->s)[ZSTR_LEN(buf->s) - 1] != ',';
187
0
    if (!empty) {
188
      /* Drop the trailing comma. */
189
0
      ZSTR_LEN(buf->s)--;
190
0
    }
191
192
0
    PHP_JSON_HASH_UNPROTECT_RECURSION(obj);
193
0
    if (encoder->depth > encoder->max_depth) {
194
0
      encoder->error_code = PHP_JSON_ERROR_DEPTH;
195
0
      if (!(options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR)) {
196
0
        return FAILURE;
197
0
      }
198
0
    }
199
0
    --encoder->depth;
200
201
0
    if (!empty) {
202
0
      php_json_pretty_print_char(buf, options, '\n');
203
0
      php_json_pretty_print_indent(buf, options, encoder);
204
0
    }
205
0
    smart_str_appendc(buf, '}');
206
0
    return SUCCESS;
207
76
  } else {
208
76
    zend_object *obj = Z_OBJ_P(val);
209
76
    prop_ht = myht = zend_get_properties_for(val, ZEND_PROP_PURPOSE_JSON);
210
76
    if (obj->ce->num_hooked_props == 0) {
211
16
      recursion_rc = (zend_refcounted *)prop_ht;
212
60
    } else {
213
      /* Protecting the object itself is fine here because myht is temporary and can't be
214
       * referenced from a different place in the object graph. */
215
60
      recursion_rc = (zend_refcounted *)obj;
216
60
    }
217
76
    encode_as_object = true;
218
76
  }
219
220
98
  if (recursion_rc && GC_IS_RECURSIVE(recursion_rc)) {
221
0
    encoder->error_code = PHP_JSON_ERROR_RECURSION;
222
0
    smart_str_appendl(buf, "null", 4);
223
0
    zend_release_properties(prop_ht);
224
0
    return FAILURE;
225
0
  }
226
227
98
  PHP_JSON_HASH_PROTECT_RECURSION(recursion_rc);
228
229
98
  if (!encode_as_object) {
230
20
    smart_str_appendc(buf, '[');
231
78
  } else {
232
78
    smart_str_appendc(buf, '{');
233
78
  }
234
235
98
  ++encoder->depth;
236
237
98
  uint32_t i = myht ? zend_hash_num_elements(myht) : 0;
238
239
98
  bool empty = true;
240
98
  if (i > 0) {
241
74
    zend_string *key;
242
74
    zval *data;
243
74
    zend_ulong index;
244
245
1.11k
    ZEND_HASH_FOREACH_KEY_VAL_IND(myht, index, key, data) {
246
1.11k
      bool need_dtor = false;
247
1.11k
      zval tmp;
248
1.11k
      ZVAL_UNDEF(&tmp);
249
250
1.11k
      if (!encode_as_object) {
251
275
        ZEND_ASSERT(Z_TYPE_P(data) != IS_PTR);
252
253
275
        php_json_pretty_print_char(buf, options, '\n');
254
275
        php_json_pretty_print_indent(buf, options, encoder);
255
275
      } else {
256
244
        if (key) {
257
244
          if (ZSTR_VAL(key)[0] == '\0' && ZSTR_LEN(key) > 0 && Z_TYPE_P(val) == IS_OBJECT) {
258
            /* Skip protected and private members. */
259
74
            continue;
260
74
          }
261
262
          /* data is IS_PTR for properties with hooks. */
263
170
          if (UNEXPECTED(Z_TYPE_P(data) == IS_PTR)) {
264
118
            zend_property_info *prop_info = Z_PTR_P(data);
265
118
            if ((prop_info->flags & ZEND_ACC_VIRTUAL) && !prop_info->hooks[ZEND_PROPERTY_HOOK_GET]) {
266
26
              continue;
267
26
            }
268
92
            need_dtor = true;
269
92
            data = zend_read_property_ex(prop_info->ce, Z_OBJ_P(val), prop_info->name, /* silent */ true, &tmp);
270
92
            if (EG(exception)) {
271
0
              PHP_JSON_HASH_UNPROTECT_RECURSION(recursion_rc);
272
0
              zend_release_properties(prop_ht);
273
0
              return FAILURE;
274
0
            }
275
92
          }
276
277
278
144
          php_json_pretty_print_char(buf, options, '\n');
279
144
          php_json_pretty_print_indent(buf, options, encoder);
280
281
144
          if (php_json_escape_string(buf, ZSTR_VAL(key), ZSTR_LEN(key),
282
144
                options & ~PHP_JSON_NUMERIC_CHECK, encoder) == FAILURE &&
283
0
              (options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR) &&
284
0
              buf->s) {
285
0
            ZSTR_LEN(buf->s) -= 4;
286
0
            smart_str_appendl(buf, "\"\"", 2);
287
0
          }
288
144
        } else {
289
0
          php_json_pretty_print_char(buf, options, '\n');
290
0
          php_json_pretty_print_indent(buf, options, encoder);
291
292
0
          smart_str_appendc(buf, '"');
293
0
          smart_str_append_long(buf, (zend_long) index);
294
0
          smart_str_appendc(buf, '"');
295
0
        }
296
297
144
        smart_str_appendc(buf, ':');
298
144
        php_json_pretty_print_char(buf, options, ' ');
299
144
      }
300
301
419
      if (php_json_encode_zval(buf, data, options, encoder) == FAILURE &&
302
0
          !(options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR)) {
303
0
        PHP_JSON_HASH_UNPROTECT_RECURSION(recursion_rc);
304
0
        zend_release_properties(prop_ht);
305
0
        zval_ptr_dtor(&tmp);
306
0
        return FAILURE;
307
0
      }
308
419
      if (UNEXPECTED(need_dtor)) {
309
92
        zval_ptr_dtor(&tmp);
310
92
      }
311
312
419
      smart_str_appendc(buf, ',');
313
419
    } ZEND_HASH_FOREACH_END();
314
315
74
    empty = ZSTR_VAL(buf->s)[ZSTR_LEN(buf->s) - 1] != ',';
316
74
    if (!empty) {
317
      /* Drop the trailing comma. */
318
72
      ZSTR_LEN(buf->s)--;
319
72
    }
320
74
  }
321
322
98
  PHP_JSON_HASH_UNPROTECT_RECURSION(recursion_rc);
323
324
98
  if (encoder->depth > encoder->max_depth) {
325
0
    encoder->error_code = PHP_JSON_ERROR_DEPTH;
326
0
    if (!(options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR)) {
327
0
      zend_release_properties(prop_ht);
328
0
      return FAILURE;
329
0
    }
330
0
  }
331
98
  --encoder->depth;
332
333
  /* Only keep closing bracket on same line for empty arrays/objects */
334
98
  if (!empty) {
335
72
    php_json_pretty_print_char(buf, options, '\n');
336
72
    php_json_pretty_print_indent(buf, options, encoder);
337
72
  }
338
339
98
  if (!encode_as_object) {
340
20
    smart_str_appendc(buf, ']');
341
78
  } else {
342
78
    smart_str_appendc(buf, '}');
343
78
  }
344
345
98
  zend_release_properties(prop_ht);
346
98
  return SUCCESS;
347
98
}
348
/* }}} */
349
350
zend_result php_json_escape_string(
351
    smart_str *buf, const char *s, size_t len,
352
    int options, php_json_encoder *encoder) /* {{{ */
353
617
{
354
617
  size_t pos, checkpoint;
355
617
  char *dst;
356
357
617
  if (len == 0) {
358
16
    smart_str_appendl(buf, "\"\"", 2);
359
16
    return SUCCESS;
360
16
  }
361
362
601
  if (options & PHP_JSON_NUMERIC_CHECK) {
363
2
    double d;
364
2
    int type;
365
2
    zend_long p;
366
367
2
    if ((type = is_numeric_string(s, len, &p, &d, 0)) != 0) {
368
0
      if (type == IS_LONG) {
369
0
        smart_str_append_long(buf, p);
370
0
        return SUCCESS;
371
0
      } else if (type == IS_DOUBLE && php_json_is_valid_double(d)) {
372
0
        php_json_encode_double(buf, d, options);
373
0
        return SUCCESS;
374
0
      }
375
0
    }
376
377
2
  }
378
601
  checkpoint = buf->s ? ZSTR_LEN(buf->s) : 0;
379
380
  /* pre-allocate for string length plus 2 quotes */
381
601
  smart_str_alloc(buf, len+2, 0);
382
601
  smart_str_appendc(buf, '"');
383
384
601
  pos = 0;
385
386
94.6k
  do {
387
94.6k
    static const uint32_t charmap[8] = {
388
94.6k
      0xffffffff, 0x500080c4, 0x10000000, 0x00000000,
389
94.6k
      0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff};
390
391
94.6k
    unsigned int us = (unsigned char)s[pos];
392
94.6k
    if (EXPECTED(!ZEND_BIT_TEST(charmap, us))) {
393
47.3k
      pos++;
394
47.3k
      len--;
395
47.3k
      if (len == 0) {
396
564
        smart_str_appendl(buf, s, pos);
397
564
        break;
398
564
      }
399
47.3k
    } else {
400
47.3k
      if (pos) {
401
4.22k
        smart_str_appendl(buf, s, pos);
402
4.22k
        s += pos;
403
4.22k
        pos = 0;
404
4.22k
      }
405
47.3k
      us = (unsigned char)s[0];
406
47.3k
      if (UNEXPECTED(us >= 0x80)) {
407
18.4k
        zend_result status;
408
18.4k
        us = php_next_utf8_char((unsigned char *)s, len, &pos, &status);
409
410
        /* check whether UTF8 character is correct */
411
18.4k
        if (UNEXPECTED(status != SUCCESS)) {
412
18.3k
          if (options & PHP_JSON_INVALID_UTF8_IGNORE) {
413
            /* ignore invalid UTF8 character */
414
18.3k
          } else if (options & PHP_JSON_INVALID_UTF8_SUBSTITUTE) {
415
            /* Use Unicode character 'REPLACEMENT CHARACTER' (U+FFFD) */
416
0
            if (options & PHP_JSON_UNESCAPED_UNICODE) {
417
0
              smart_str_appendl(buf, "\xef\xbf\xbd", 3);
418
0
            } else {
419
0
              smart_str_appendl(buf, "\\ufffd", 6);
420
0
            }
421
37
          } else {
422
37
            ZSTR_LEN(buf->s) = checkpoint;
423
37
            encoder->error_code = PHP_JSON_ERROR_UTF8;
424
37
            if (options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR) {
425
0
              smart_str_appendl(buf, "null", 4);
426
0
            }
427
37
            return FAILURE;
428
37
          }
429
430
        /* Escape U+2028/U+2029 line terminators, UNLESS both
431
           JSON_UNESCAPED_UNICODE and
432
           JSON_UNESCAPED_LINE_TERMINATORS were provided */
433
18.3k
        } else if ((options & PHP_JSON_UNESCAPED_UNICODE)
434
139
            && ((options & PHP_JSON_UNESCAPED_LINE_TERMINATORS)
435
139
            || us < 0x2028 || us > 0x2029)) {
436
139
          smart_str_appendl(buf, s, pos);
437
139
        } else {
438
          /* From http://en.wikipedia.org/wiki/UTF16 */
439
0
          if (us >= 0x10000) {
440
0
            unsigned int next_us;
441
442
0
            us -= 0x10000;
443
0
            next_us = (unsigned short)((us & 0x3ff) | 0xdc00);
444
0
            us = (unsigned short)((us >> 10) | 0xd800);
445
0
            dst = smart_str_extend(buf, 6);
446
0
            dst[0] = '\\';
447
0
            dst[1] = 'u';
448
0
            dst[2] = digits[(us >> 12) & 0xf];
449
0
            dst[3] = digits[(us >> 8) & 0xf];
450
0
            dst[4] = digits[(us >> 4) & 0xf];
451
0
            dst[5] = digits[us & 0xf];
452
0
            us = next_us;
453
0
          }
454
0
          dst = smart_str_extend(buf, 6);
455
0
          dst[0] = '\\';
456
0
          dst[1] = 'u';
457
0
          dst[2] = digits[(us >> 12) & 0xf];
458
0
          dst[3] = digits[(us >> 8) & 0xf];
459
0
          dst[4] = digits[(us >> 4) & 0xf];
460
0
          dst[5] = digits[us & 0xf];
461
0
        }
462
18.4k
        s += pos;
463
18.4k
        len -= pos;
464
18.4k
        pos = 0;
465
28.8k
      } else {
466
28.8k
        s++;
467
28.8k
        switch (us) {
468
458
          case '"':
469
458
            if (options & PHP_JSON_HEX_QUOT) {
470
2
              smart_str_appendl(buf, "\\u0022", 6);
471
456
            } else {
472
456
              smart_str_appendl(buf, "\\\"", 2);
473
456
            }
474
458
            break;
475
476
236
          case '\\':
477
236
            smart_str_appendl(buf, "\\\\", 2);
478
236
            break;
479
480
117
          case '/':
481
117
            if (options & PHP_JSON_UNESCAPED_SLASHES) {
482
117
              smart_str_appendc(buf, '/');
483
117
            } else {
484
0
              smart_str_appendl(buf, "\\/", 2);
485
0
            }
486
117
            break;
487
488
72
          case '\b':
489
72
            smart_str_appendl(buf, "\\b", 2);
490
72
            break;
491
492
71
          case '\f':
493
71
            smart_str_appendl(buf, "\\f", 2);
494
71
            break;
495
496
509
          case '\n':
497
509
            smart_str_appendl(buf, "\\n", 2);
498
509
            break;
499
500
2.96k
          case '\r':
501
2.96k
            smart_str_appendl(buf, "\\r", 2);
502
2.96k
            break;
503
504
82
          case '\t':
505
82
            smart_str_appendl(buf, "\\t", 2);
506
82
            break;
507
508
11.9k
          case '<':
509
11.9k
            if (options & PHP_JSON_HEX_TAG) {
510
5.87k
              smart_str_appendl(buf, "\\u003C", 6);
511
6.06k
            } else {
512
6.06k
              smart_str_appendc(buf, '<');
513
6.06k
            }
514
11.9k
            break;
515
516
140
          case '>':
517
140
            if (options & PHP_JSON_HEX_TAG) {
518
97
              smart_str_appendl(buf, "\\u003E", 6);
519
97
            } else {
520
43
              smart_str_appendc(buf, '>');
521
43
            }
522
140
            break;
523
524
33
          case '&':
525
33
            if (options & PHP_JSON_HEX_AMP) {
526
18
              smart_str_appendl(buf, "\\u0026", 6);
527
18
            } else {
528
15
              smart_str_appendc(buf, '&');
529
15
            }
530
33
            break;
531
532
0
          case '\'':
533
0
            if (options & PHP_JSON_HEX_APOS) {
534
0
              smart_str_appendl(buf, "\\u0027", 6);
535
0
            } else {
536
0
              smart_str_appendc(buf, '\'');
537
0
            }
538
0
            break;
539
540
12.2k
          default:
541
12.2k
            ZEND_ASSERT(us < ' ');
542
12.2k
            dst = smart_str_extend(buf, 6);
543
12.2k
            dst[0] = '\\';
544
12.2k
            dst[1] = 'u';
545
12.2k
            dst[2] = '0';
546
12.2k
            dst[3] = '0';
547
12.2k
            dst[4] = digits[(us >> 4) & 0xf];
548
12.2k
            dst[5] = digits[us & 0xf];
549
12.2k
            break;
550
28.8k
        }
551
28.8k
        len--;
552
28.8k
      }
553
47.3k
    }
554
94.6k
  } while (len);
555
556
564
  smart_str_appendc(buf, '"');
557
558
564
  return SUCCESS;
559
601
}
560
/* }}} */
561
562
static zend_result php_json_encode_serializable_object(smart_str *buf, zend_object *obj, int options, php_json_encoder *encoder)
563
6
{
564
6
  zend_class_entry *ce = obj->ce;
565
6
  uint32_t *guard = zend_get_recursion_guard(obj);
566
6
  zval retval;
567
6
  zend_result return_code;
568
569
6
  ZEND_ASSERT(guard != NULL);
570
571
6
  if (ZEND_GUARD_IS_RECURSIVE(guard, JSON)) {
572
0
    encoder->error_code = PHP_JSON_ERROR_RECURSION;
573
0
    if (options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR) {
574
0
      smart_str_appendl(buf, "null", 4);
575
0
    }
576
0
    return FAILURE;
577
0
  }
578
579
6
  ZEND_GUARD_PROTECT_RECURSION(guard, JSON);
580
581
6
  zend_function *json_serialize_method = zend_hash_str_find_ptr(&ce->function_table, ZEND_STRL("jsonserialize"));
582
6
  ZEND_ASSERT(json_serialize_method != NULL && "This should be guaranteed prior to calling this function");
583
6
  zend_call_known_function(json_serialize_method, obj, ce, &retval, 0, NULL, NULL);
584
  /* An exception has occurred */
585
6
  if (Z_TYPE(retval) == IS_UNDEF) {
586
0
    if (options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR) {
587
0
      smart_str_appendl(buf, "null", 4);
588
0
    }
589
0
    ZEND_GUARD_UNPROTECT_RECURSION(guard, JSON);
590
0
    return FAILURE;
591
0
  }
592
593
6
  if (Z_TYPE(retval) == IS_OBJECT && Z_OBJ(retval) == obj) {
594
    /* Handle the case where jsonSerialize does: return $this; by going straight to encode array */
595
0
    ZEND_GUARD_UNPROTECT_RECURSION(guard, JSON);
596
0
    return_code = php_json_encode_array(buf, &retval, options, encoder);
597
6
  } else {
598
    /* All other types, encode as normal */
599
6
    return_code = php_json_encode_zval(buf, &retval, options, encoder);
600
6
    ZEND_GUARD_UNPROTECT_RECURSION(guard, JSON);
601
6
  }
602
603
6
  zval_ptr_dtor(&retval);
604
605
6
  return return_code;
606
6
}
607
608
static zend_result php_json_encode_serializable_enum(smart_str *buf, zval *val, int options, php_json_encoder *encoder)
609
234
{
610
234
  const zend_class_entry *ce = Z_OBJCE_P(val);
611
234
  if (ce->enum_backing_type == IS_UNDEF) {
612
82
    encoder->error_code = PHP_JSON_ERROR_NON_BACKED_ENUM;
613
82
    smart_str_appendc(buf, '0');
614
82
    return FAILURE;
615
82
  }
616
617
152
  zval *value_zv = zend_enum_fetch_case_value(Z_OBJ_P(val));
618
152
  return php_json_encode_zval(buf, value_zv, options, encoder);
619
234
}
620
621
zend_result php_json_encode_zval(smart_str *buf, zval *val, int options, php_json_encoder *encoder) /* {{{ */
622
1.63k
{
623
1.65k
again:
624
1.65k
  switch (Z_TYPE_P(val))
625
1.65k
  {
626
25
    case IS_NULL:
627
25
      smart_str_appendl(buf, "null", 4);
628
25
      break;
629
630
140
    case IS_TRUE:
631
140
      smart_str_appendl(buf, "true", 4);
632
140
      break;
633
57
    case IS_FALSE:
634
57
      smart_str_appendl(buf, "false", 5);
635
57
      break;
636
637
558
    case IS_LONG:
638
558
      smart_str_append_long(buf, Z_LVAL_P(val));
639
558
      break;
640
641
47
    case IS_DOUBLE:
642
47
      if (php_json_is_valid_double(Z_DVAL_P(val))) {
643
47
        php_json_encode_double(buf, Z_DVAL_P(val), options);
644
47
      } else {
645
0
        encoder->error_code = PHP_JSON_ERROR_INF_OR_NAN;
646
0
        smart_str_appendc(buf, '0');
647
0
      }
648
47
      break;
649
650
473
    case IS_STRING:
651
473
      return php_json_escape_string(buf, Z_STRVAL_P(val), Z_STRLEN_P(val), options, encoder);
652
653
316
    case IS_OBJECT:
654
316
      if (instanceof_function(Z_OBJCE_P(val), php_json_serializable_ce)) {
655
6
        return php_json_encode_serializable_object(buf, Z_OBJ_P(val), options, encoder);
656
6
      }
657
310
      if (Z_OBJCE_P(val)->ce_flags & ZEND_ACC_ENUM) {
658
234
        return php_json_encode_serializable_enum(buf, val, options, encoder);
659
234
      }
660
      /* fallthrough -- Non-serializable object */
661
76
      ZEND_FALLTHROUGH;
662
98
    case IS_ARRAY: {
663
      /* Avoid modifications (and potential freeing) of the array through a reference when a
664
       * jsonSerialize() method is invoked. */
665
98
      zval zv;
666
98
      zend_result res;
667
98
      ZVAL_COPY(&zv, val);
668
98
      res = php_json_encode_array(buf, &zv, options, encoder);
669
98
      zval_ptr_dtor_nogc(&zv);
670
98
      return res;
671
76
    }
672
673
20
    case IS_REFERENCE:
674
20
      val = Z_REFVAL_P(val);
675
20
      goto again;
676
677
0
    default:
678
0
      encoder->error_code = PHP_JSON_ERROR_UNSUPPORTED_TYPE;
679
0
      if (options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR) {
680
0
        smart_str_appendl(buf, "null", 4);
681
0
      }
682
0
      return FAILURE;
683
1.65k
  }
684
685
827
  return SUCCESS;
686
1.65k
}
687
/* }}} */