Coverage Report

Created: 2025-07-23 06:33

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