Coverage Report

Created: 2026-06-13 07:01

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