Coverage Report

Created: 2026-07-25 06:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/php-src/Zend/zend_operators.c
Line
Count
Source
1
/*
2
   +----------------------------------------------------------------------+
3
   | Zend Engine                                                          |
4
   +----------------------------------------------------------------------+
5
   | Copyright © Zend Technologies Ltd., a subsidiary company of          |
6
   |     Perforce Software, Inc., and Contributors.                       |
7
   +----------------------------------------------------------------------+
8
   | This source file is subject to the Modified BSD License that is      |
9
   | bundled with this package in the file LICENSE, and is available      |
10
   | through the World Wide Web at <https://www.php.net/license/>.        |
11
   |                                                                      |
12
   | SPDX-License-Identifier: BSD-3-Clause                                |
13
   +----------------------------------------------------------------------+
14
   | Authors: Andi Gutmans <andi@php.net>                                 |
15
   |          Zeev Suraski <zeev@php.net>                                 |
16
   |          Dmitry Stogov <dmitry@php.net>                              |
17
   +----------------------------------------------------------------------+
18
*/
19
20
#include <ctype.h>
21
22
#include "zend.h"
23
#include "zend_operators.h"
24
#include "zend_variables.h"
25
#include "zend_globals.h"
26
#include "zend_list.h"
27
#include "zend_API.h"
28
#include "zend_strtod.h"
29
#include "zend_exceptions.h"
30
#include "zend_closures.h"
31
32
#include <locale.h>
33
#ifdef HAVE_LANGINFO_H
34
# include <langinfo.h>
35
#endif
36
37
#ifdef ZEND_INTRIN_AVX2_NATIVE
38
#include <immintrin.h>
39
#endif
40
#ifdef __SSE2__
41
#include <emmintrin.h>
42
#endif
43
#if defined(__aarch64__) || defined(_M_ARM64)
44
#include <arm_neon.h>
45
#endif
46
47
#if defined(ZEND_WIN32) && !defined(ZTS) && defined(_MSC_VER)
48
/* This performance improvement of tolower() on Windows gives 10-18% on bench.php */
49
#define ZEND_USE_TOLOWER_L 1
50
#endif
51
52
#ifdef ZEND_USE_TOLOWER_L
53
static _locale_t current_locale = NULL;
54
/* this is true global! may lead to strange effects on ZTS, but so may setlocale() */
55
#define zend_tolower(c) _tolower_l(c, current_locale)
56
#else
57
4
#define zend_tolower(c) tolower(c)
58
#endif
59
60
2.07M
#define TYPE_PAIR(t1,t2) (((t1) << 4) | (t2))
61
62
#ifdef ZEND_INTRIN_AVX2_NATIVE
63
#define HAVE_BLOCKCONV
64
65
#define BLOCKCONV_INIT_RANGE(start, end) \
66
  const __m256i blconv_offset = _mm256_set1_epi8((signed char)(SCHAR_MIN - start)); \
67
  const __m256i blconv_threshold = _mm256_set1_epi8(SCHAR_MIN + (end - start) + 1);
68
69
#define BLOCKCONV_STRIDE sizeof(__m256i)
70
71
#define BLOCKCONV_INIT_DELTA(delta) \
72
  const __m256i blconv_delta = _mm256_set1_epi8(delta);
73
74
#define BLOCKCONV_LOAD(input) \
75
  __m256i blconv_operand = _mm256_loadu_si256((__m256i*)(input)); \
76
  __m256i blconv_mask = _mm256_cmpgt_epi8(blconv_threshold, _mm256_add_epi8(blconv_operand, blconv_offset));
77
78
#define BLOCKCONV_FOUND() _mm256_movemask_epi8(blconv_mask)
79
80
#define BLOCKCONV_STORE(dest) \
81
  __m256i blconv_add = _mm256_and_si256(blconv_mask, blconv_delta); \
82
  __m256i blconv_result = _mm256_add_epi8(blconv_operand, blconv_add); \
83
  _mm256_storeu_si256((__m256i*)(dest), blconv_result);
84
85
#elif __SSE2__
86
#define HAVE_BLOCKCONV
87
88
/* Common code for SSE2 accelerated character case conversion */
89
90
#define BLOCKCONV_INIT_RANGE(start, end) \
91
15.6M
  const __m128i blconv_offset = _mm_set1_epi8((signed char)(SCHAR_MIN - start)); \
92
15.6M
  const __m128i blconv_threshold = _mm_set1_epi8(SCHAR_MIN + (end - start) + 1);
93
94
100M
#define BLOCKCONV_STRIDE sizeof(__m128i)
95
96
#define BLOCKCONV_INIT_DELTA(delta) \
97
7.43M
  const __m128i blconv_delta = _mm_set1_epi8(delta);
98
99
#define BLOCKCONV_LOAD(input) \
100
31.3M
  __m128i blconv_operand = _mm_loadu_si128((__m128i*)(input)); \
101
31.3M
  __m128i blconv_mask = _mm_cmplt_epi8(_mm_add_epi8(blconv_operand, blconv_offset), blconv_threshold);
102
103
27.9M
#define BLOCKCONV_FOUND() _mm_movemask_epi8(blconv_mask)
104
105
#define BLOCKCONV_STORE(dest) \
106
8.85M
  __m128i blconv_add = _mm_and_si128(blconv_mask, blconv_delta); \
107
8.85M
  __m128i blconv_result = _mm_add_epi8(blconv_operand, blconv_add); \
108
8.85M
  _mm_storeu_si128((__m128i *)(dest), blconv_result);
109
110
#elif defined(__aarch64__) || defined(_M_ARM64)
111
#define HAVE_BLOCKCONV
112
113
#define BLOCKCONV_INIT_RANGE(start, end) \
114
  const int8x16_t blconv_offset = vdupq_n_s8((signed char)(SCHAR_MIN - start)); \
115
  const int8x16_t blconv_threshold = vdupq_n_s8(SCHAR_MIN + (end - start) + 1);
116
117
#define BLOCKCONV_STRIDE sizeof(int8x16_t)
118
119
#define BLOCKCONV_INIT_DELTA(delta) \
120
  const int8x16_t blconv_delta = vdupq_n_s8(delta);
121
122
#define BLOCKCONV_LOAD(input) \
123
  int8x16_t blconv_operand = vld1q_s8((const int8_t*)(input)); \
124
  uint8x16_t blconv_mask = vcltq_s8(vreinterpretq_s8_u8(vaddq_u8(vreinterpretq_u8_s8(blconv_operand), vreinterpretq_u8_s8(blconv_offset))), blconv_threshold);
125
126
#define BLOCKCONV_FOUND() vmaxvq_u8(blconv_mask)
127
128
#define BLOCKCONV_STORE(dest) \
129
  int8x16_t blconv_add = vandq_s8(vreinterpretq_s8_u8(blconv_mask), blconv_delta); \
130
  int8x16_t blconv_result = vaddq_s8(blconv_operand, blconv_add); \
131
  vst1q_s8((int8_t *)(dest), blconv_result);
132
133
#endif /* defined(__aarch64__) || defined(_M_ARM64) */
134
135
ZEND_API const unsigned char zend_tolower_map[256] = {
136
0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,
137
0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,
138
0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,
139
0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,0x3f,
140
0x40,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,0x6f,
141
0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7a,0x5b,0x5c,0x5d,0x5e,0x5f,
142
0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,0x6f,
143
0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7a,0x7b,0x7c,0x7d,0x7e,0x7f,
144
0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8e,0x8f,
145
0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9a,0x9b,0x9c,0x9d,0x9e,0x9f,
146
0xa0,0xa1,0xa2,0xa3,0xa4,0xa5,0xa6,0xa7,0xa8,0xa9,0xaa,0xab,0xac,0xad,0xae,0xaf,
147
0xb0,0xb1,0xb2,0xb3,0xb4,0xb5,0xb6,0xb7,0xb8,0xb9,0xba,0xbb,0xbc,0xbd,0xbe,0xbf,
148
0xc0,0xc1,0xc2,0xc3,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xcb,0xcc,0xcd,0xce,0xcf,
149
0xd0,0xd1,0xd2,0xd3,0xd4,0xd5,0xd6,0xd7,0xd8,0xd9,0xda,0xdb,0xdc,0xdd,0xde,0xdf,
150
0xe0,0xe1,0xe2,0xe3,0xe4,0xe5,0xe6,0xe7,0xe8,0xe9,0xea,0xeb,0xec,0xed,0xee,0xef,
151
0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
152
};
153
154
ZEND_API const unsigned char zend_toupper_map[256] = {
155
0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,
156
0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,
157
0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,
158
0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,0x3f,
159
0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f,
160
0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5a,0x5b,0x5c,0x5d,0x5e,0x5f,
161
0x60,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f,
162
0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5a,0x7b,0x7c,0x7d,0x7e,0x7f,
163
0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8e,0x8f,
164
0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9a,0x9b,0x9c,0x9d,0x9e,0x9f,
165
0xa0,0xa1,0xa2,0xa3,0xa4,0xa5,0xa6,0xa7,0xa8,0xa9,0xaa,0xab,0xac,0xad,0xae,0xaf,
166
0xb0,0xb1,0xb2,0xb3,0xb4,0xb5,0xb6,0xb7,0xb8,0xb9,0xba,0xbb,0xbc,0xbd,0xbe,0xbf,
167
0xc0,0xc1,0xc2,0xc3,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xcb,0xcc,0xcd,0xce,0xcf,
168
0xd0,0xd1,0xd2,0xd3,0xd4,0xd5,0xd6,0xd7,0xd8,0xd9,0xda,0xdb,0xdc,0xdd,0xde,0xdf,
169
0xe0,0xe1,0xe2,0xe3,0xe4,0xe5,0xe6,0xe7,0xe8,0xe9,0xea,0xeb,0xec,0xed,0xee,0xef,
170
0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
171
};
172
173
174
/**
175
 * Functions using locale lowercase:
176
    zend_binary_strncasecmp_l
177
    zend_binary_strcasecmp_l
178
 * Functions using ascii lowercase:
179
    string_compare_function_ex
180
    string_case_compare_function
181
      zend_str_tolower_copy
182
    zend_str_tolower_dup
183
    zend_str_tolower
184
    zend_binary_strcasecmp
185
    zend_binary_strncasecmp
186
 */
187
188
static zend_long ZEND_FASTCALL zend_atol_internal(const char *str, size_t str_len) /* {{{ */
189
0
{
190
0
  if (!str_len) {
191
0
    str_len = strlen(str);
192
0
  }
193
194
  /* Perform following multiplications on unsigned to avoid overflow UB.
195
   * For now overflow is silently ignored -- not clear what else can be
196
   * done here, especially as the final result of this function may be
197
   * used in an unsigned context (e.g. "memory_limit=3G", which overflows
198
   * zend_long on 32-bit, but not size_t). */
199
0
  zend_ulong retval = (zend_ulong) ZEND_STRTOL(str, NULL, 0);
200
0
  if (str_len>0) {
201
0
    switch (str[str_len-1]) {
202
0
      case 'g':
203
0
      case 'G':
204
0
        retval *= 1024;
205
0
        ZEND_FALLTHROUGH;
206
0
      case 'm':
207
0
      case 'M':
208
0
        retval *= 1024;
209
0
        ZEND_FALLTHROUGH;
210
0
      case 'k':
211
0
      case 'K':
212
0
        retval *= 1024;
213
0
        break;
214
0
    }
215
0
  }
216
0
  return (zend_long) retval;
217
0
}
218
/* }}} */
219
220
ZEND_API zend_long ZEND_FASTCALL zend_atol(const char *str, size_t str_len)
221
0
{
222
0
  return zend_atol_internal(str, str_len);
223
0
}
224
225
ZEND_API int ZEND_FASTCALL zend_atoi(const char *str, size_t str_len)
226
0
{
227
0
  return (int) zend_atol_internal(str, str_len);
228
0
}
229
230
/* {{{ convert_object_to_type: dst will be either ctype or UNDEF */
231
#define convert_object_to_type(op, dst, ctype)                  \
232
361
  ZVAL_UNDEF(dst);                                    \
233
361
  if (Z_OBJ_HT_P(op)->cast_object(Z_OBJ_P(op), dst, ctype) == FAILURE) {         \
234
361
    zend_error(E_WARNING,                               \
235
361
      "Object of class %s could not be converted to %s", ZSTR_VAL(Z_OBJCE_P(op)->name),\
236
361
    zend_get_type_by_const(ctype));                           \
237
361
  }                                            \
238
239
/* }}} */
240
241
ZEND_API void ZEND_FASTCALL convert_scalar_to_number(zval *op) /* {{{ */
242
0
{
243
0
try_again:
244
0
  switch (Z_TYPE_P(op)) {
245
0
    case IS_REFERENCE:
246
0
      zend_unwrap_reference(op);
247
0
      goto try_again;
248
0
    case IS_STRING:
249
0
      {
250
0
        zend_string *str;
251
252
0
        str = Z_STR_P(op);
253
0
        if ((Z_TYPE_INFO_P(op)=is_numeric_string(ZSTR_VAL(str), ZSTR_LEN(str), &Z_LVAL_P(op), &Z_DVAL_P(op), 1)) == 0) {
254
0
          ZVAL_LONG(op, 0);
255
0
        }
256
0
        zend_string_release_ex(str, 0);
257
0
        break;
258
0
      }
259
0
    case IS_NULL:
260
0
    case IS_FALSE:
261
0
      ZVAL_LONG(op, 0);
262
0
      break;
263
0
    case IS_TRUE:
264
0
      ZVAL_LONG(op, 1);
265
0
      break;
266
0
    case IS_RESOURCE:
267
0
      {
268
0
        zend_long l = Z_RES_HANDLE_P(op);
269
0
        zval_ptr_dtor(op);
270
0
        ZVAL_LONG(op, l);
271
0
      }
272
0
      break;
273
0
    case IS_OBJECT:
274
0
      {
275
0
        zval dst;
276
277
0
        convert_object_to_type(op, &dst, _IS_NUMBER);
278
0
        zval_ptr_dtor(op);
279
280
0
        if (Z_TYPE(dst) == IS_LONG || Z_TYPE(dst) == IS_DOUBLE) {
281
0
          ZVAL_COPY_VALUE(op, &dst);
282
0
        } else {
283
0
          ZVAL_LONG(op, 1);
284
0
        }
285
0
      }
286
0
      break;
287
0
  }
288
0
}
289
/* }}} */
290
291
static zend_never_inline zval* ZEND_FASTCALL _zendi_convert_scalar_to_number_silent(zval *op, zval *holder) /* {{{ */
292
42.9k
{
293
42.9k
  switch (Z_TYPE_P(op)) {
294
0
    case IS_NULL:
295
0
    case IS_FALSE:
296
0
      ZVAL_LONG(holder, 0);
297
0
      return holder;
298
0
    case IS_TRUE:
299
0
      ZVAL_LONG(holder, 1);
300
0
      return holder;
301
6.36k
    case IS_STRING:
302
6.36k
      if ((Z_TYPE_INFO_P(holder) = is_numeric_string(Z_STRVAL_P(op), Z_STRLEN_P(op), &Z_LVAL_P(holder), &Z_DVAL_P(holder), 1)) == 0) {
303
2.34k
        ZVAL_LONG(holder, 0);
304
2.34k
      }
305
6.36k
      return holder;
306
0
    case IS_RESOURCE:
307
0
      ZVAL_LONG(holder, Z_RES_HANDLE_P(op));
308
0
      return holder;
309
0
    case IS_OBJECT:
310
0
      convert_object_to_type(op, holder, _IS_NUMBER);
311
0
      if (UNEXPECTED(EG(exception)) ||
312
0
          UNEXPECTED(Z_TYPE_P(holder) != IS_LONG && Z_TYPE_P(holder) != IS_DOUBLE)) {
313
0
        ZVAL_LONG(holder, 1);
314
0
      }
315
0
      return holder;
316
14.9k
    case IS_LONG:
317
15.0k
    case IS_DOUBLE:
318
36.6k
    default:
319
36.6k
      return op;
320
42.9k
  }
321
42.9k
}
322
/* }}} */
323
324
static zend_never_inline zend_result ZEND_FASTCALL _zendi_try_convert_scalar_to_number(zval *op, zval *holder) /* {{{ */
325
158k
{
326
158k
  switch (Z_TYPE_P(op)) {
327
81.4k
    case IS_NULL:
328
99.9k
    case IS_FALSE:
329
99.9k
      ZVAL_LONG(holder, 0);
330
99.9k
      return SUCCESS;
331
12.1k
    case IS_TRUE:
332
12.1k
      ZVAL_LONG(holder, 1);
333
12.1k
      return SUCCESS;
334
45.2k
    case IS_STRING:
335
45.2k
    {
336
45.2k
      bool trailing_data = false;
337
      /* For BC reasons we allow errors so that we can warn on leading numeric string */
338
45.2k
      if (0 == (Z_TYPE_INFO_P(holder) = is_numeric_string_ex(Z_STRVAL_P(op), Z_STRLEN_P(op),
339
45.2k
          &Z_LVAL_P(holder), &Z_DVAL_P(holder),  /* allow errors */ true, NULL, &trailing_data))) {
340
        /* Will lead to invalid OP type error */
341
787
        return FAILURE;
342
787
      }
343
44.5k
      if (UNEXPECTED(trailing_data)) {
344
19.6k
        zend_error(E_WARNING, "A non-numeric value encountered");
345
19.6k
        if (UNEXPECTED(EG(exception))) {
346
0
          return FAILURE;
347
0
        }
348
19.6k
      }
349
44.5k
      return SUCCESS;
350
44.5k
    }
351
391
    case IS_OBJECT:
352
391
      if (Z_OBJ_HT_P(op)->cast_object(Z_OBJ_P(op), holder, _IS_NUMBER) == FAILURE
353
391
          || EG(exception)) {
354
391
        return FAILURE;
355
391
      }
356
0
      ZEND_ASSERT(Z_TYPE_P(holder) == IS_LONG || Z_TYPE_P(holder) == IS_DOUBLE);
357
0
      return SUCCESS;
358
3
    case IS_RESOURCE:
359
340
    case IS_ARRAY:
360
340
      return FAILURE;
361
0
    default: ZEND_UNREACHABLE();
362
158k
  }
363
158k
}
364
/* }}} */
365
366
static zend_always_inline zend_result zendi_try_convert_scalar_to_number(zval *op, zval *holder) /* {{{ */
367
268k
{
368
268k
  if (Z_TYPE_P(op) == IS_LONG || Z_TYPE_P(op) == IS_DOUBLE) {
369
110k
    ZVAL_COPY_VALUE(holder, op);
370
110k
    return SUCCESS;
371
158k
  } else {
372
158k
    return _zendi_try_convert_scalar_to_number(op, holder);
373
158k
  }
374
268k
}
375
/* }}} */
376
377
static zend_never_inline zend_long ZEND_FASTCALL zendi_try_get_long(const zval *op, bool *failed) /* {{{ */
378
130k
{
379
130k
  *failed = false;
380
130k
try_again:
381
130k
  switch (Z_TYPE_P(op)) {
382
18.0k
    case IS_NULL:
383
27.5k
    case IS_FALSE:
384
27.5k
      return 0;
385
7.47k
    case IS_TRUE:
386
7.47k
      return 1;
387
83.6k
    case IS_DOUBLE: {
388
83.6k
      double dval = Z_DVAL_P(op);
389
83.6k
      zend_long lval = zend_dval_to_lval_safe(dval);
390
83.6k
      if (UNEXPECTED(EG(exception))) {
391
0
        *failed = true;
392
0
      }
393
83.6k
      return lval;
394
18.0k
    }
395
11.2k
    case IS_STRING:
396
11.2k
      {
397
11.2k
        uint8_t type;
398
11.2k
        zend_long lval;
399
11.2k
        double dval;
400
11.2k
        bool trailing_data = false;
401
11.2k
        zend_string *op_str = NULL; /* protect against error handlers */
402
403
        /* For BC reasons we allow errors so that we can warn on leading numeric string */
404
11.2k
        type = is_numeric_string_ex(Z_STRVAL_P(op), Z_STRLEN_P(op), &lval, &dval,
405
11.2k
          /* allow errors */ true, NULL, &trailing_data);
406
11.2k
        if (type == 0) {
407
1.41k
          *failed = true;
408
1.41k
          return 0;
409
1.41k
        }
410
9.85k
        if (UNEXPECTED(trailing_data)) {
411
4.05k
          if (type != IS_LONG) {
412
1.18k
            op_str = zend_string_copy(Z_STR_P(op));
413
1.18k
          }
414
4.05k
          zend_error(E_WARNING, "A non-numeric value encountered");
415
4.05k
          if (UNEXPECTED(EG(exception))) {
416
0
            *failed = true;
417
0
            zend_tmp_string_release(op_str);
418
0
            return 0;
419
0
          }
420
4.05k
        }
421
9.85k
        if (EXPECTED(type == IS_LONG)) {
422
6.94k
          return lval;
423
6.94k
        } else {
424
          /* Previously we used strtol here, not is_numeric_string,
425
           * and strtol gives you LONG_MAX/_MIN on overflow.
426
           * We use use saturating conversion to emulate strtol()'s
427
           * behaviour.
428
           */
429
2.91k
          lval = zend_dval_to_lval_cap(dval);
430
2.91k
          if (!zend_is_long_compatible(dval, lval)) {
431
2.39k
            zend_incompatible_string_to_long_error(op_str ? op_str : Z_STR_P(op));
432
2.39k
            if (UNEXPECTED(EG(exception))) {
433
0
              *failed = true;
434
0
            }
435
2.39k
          }
436
2.91k
          zend_tmp_string_release(op_str);
437
2.91k
          return lval;
438
2.91k
        }
439
9.85k
      }
440
151
    case IS_OBJECT:
441
151
      {
442
151
        zval dst;
443
151
        if (Z_OBJ_HT_P(op)->cast_object(Z_OBJ_P(op), &dst, IS_LONG) == FAILURE
444
151
            || EG(exception)) {
445
151
          *failed = true;
446
151
          return 0;
447
151
        }
448
0
        ZEND_ASSERT(Z_TYPE(dst) == IS_LONG);
449
0
        return Z_LVAL(dst);
450
0
      }
451
0
    case IS_UNDEF:
452
0
    case IS_RESOURCE:
453
65
    case IS_ARRAY:
454
65
      *failed = true;
455
65
      return 0;
456
0
    case IS_REFERENCE:
457
0
      op = Z_REFVAL_P(op);
458
0
      if (Z_TYPE_P(op) == IS_LONG) {
459
0
        return Z_LVAL_P(op);
460
0
      } else {
461
0
        goto try_again;
462
0
      }
463
0
      break;
464
0
    default: ZEND_UNREACHABLE();
465
130k
  }
466
130k
}
467
/* }}} */
468
469
ZEND_API zend_long ZEND_FASTCALL zval_try_get_long(const zval *op, bool *failed)
470
0
{
471
0
  if (EXPECTED(Z_TYPE_P(op) == IS_LONG)) {
472
0
    *failed = false;
473
0
    return Z_LVAL_P(op);
474
0
  }
475
0
  return zendi_try_get_long(op, failed);
476
0
}
477
478
#define ZEND_TRY_BINARY_OP1_OBJECT_OPERATION(opcode) \
479
323k
  if (UNEXPECTED(Z_TYPE_P(op1) == IS_OBJECT) \
480
323k
    && UNEXPECTED(Z_OBJ_HANDLER_P(op1, do_operation))) { \
481
0
    if (EXPECTED(SUCCESS == Z_OBJ_HANDLER_P(op1, do_operation)(opcode, result, op1, op2))) { \
482
0
      return SUCCESS; \
483
0
    } \
484
0
  }
485
486
#define ZEND_TRY_BINARY_OP2_OBJECT_OPERATION(opcode) \
487
437k
  if (UNEXPECTED(Z_TYPE_P(op2) == IS_OBJECT) \
488
437k
    && UNEXPECTED(Z_OBJ_HANDLER_P(op2, do_operation)) \
489
437k
    && EXPECTED(SUCCESS == Z_OBJ_HANDLER_P(op2, do_operation)(opcode, result, op1, op2))) { \
490
0
    return SUCCESS; \
491
0
  }
492
493
#define ZEND_TRY_BINARY_OBJECT_OPERATION(opcode) \
494
267k
  ZEND_TRY_BINARY_OP1_OBJECT_OPERATION(opcode) \
495
267k
  else \
496
267k
  ZEND_TRY_BINARY_OP2_OBJECT_OPERATION(opcode)
497
498
#define ZEND_TRY_UNARY_OBJECT_OPERATION(opcode) \
499
256k
  if (UNEXPECTED(Z_TYPE_P(op1) == IS_OBJECT) \
500
256k
    && UNEXPECTED(Z_OBJ_HANDLER_P(op1, do_operation)) \
501
256k
    && EXPECTED(SUCCESS == Z_OBJ_HANDLER_P(op1, do_operation)(opcode, result, op1, NULL))) { \
502
0
    return SUCCESS; \
503
0
  }
504
505
#define convert_op1_op2_long(op1, op1_lval, op2, op2_lval, result, opcode, sigil) \
506
47.6k
  do {                               \
507
47.6k
    if (UNEXPECTED(Z_TYPE_P(op1) != IS_LONG)) {           \
508
28.3k
      bool failed;                      \
509
28.3k
      if (Z_ISREF_P(op1)) {                   \
510
6
        op1 = Z_REFVAL_P(op1);                  \
511
6
        if (Z_TYPE_P(op1) == IS_LONG) {             \
512
3
          op1_lval = Z_LVAL_P(op1);              \
513
3
          break;                        \
514
3
        }                            \
515
6
      }                              \
516
28.3k
      ZEND_TRY_BINARY_OP1_OBJECT_OPERATION(opcode);       \
517
28.3k
      op1_lval = zendi_try_get_long(op1, &failed);        \
518
28.3k
      if (UNEXPECTED(failed)) {                 \
519
446
        zend_binop_error(sigil, op1, op2);            \
520
446
        if (result != op1) {                 \
521
369
          ZVAL_UNDEF(result);                 \
522
369
        }                            \
523
446
        return FAILURE;                     \
524
446
      }                              \
525
28.3k
    } else {                           \
526
19.2k
      op1_lval = Z_LVAL_P(op1);                  \
527
19.2k
    }                                \
528
47.6k
  } while (0);                            \
529
47.6k
  do {                               \
530
47.1k
    if (UNEXPECTED(Z_TYPE_P(op2) != IS_LONG)) {           \
531
14.5k
      bool failed;                      \
532
14.5k
      if (Z_ISREF_P(op2)) {                   \
533
0
        op2 = Z_REFVAL_P(op2);                  \
534
0
        if (Z_TYPE_P(op2) == IS_LONG) {             \
535
0
          op2_lval = Z_LVAL_P(op2);              \
536
0
          break;                        \
537
0
        }                           \
538
0
      }                              \
539
14.5k
      ZEND_TRY_BINARY_OP2_OBJECT_OPERATION(opcode);       \
540
14.5k
      op2_lval = zendi_try_get_long(op2, &failed);        \
541
14.5k
      if (UNEXPECTED(failed)) {                 \
542
380
        zend_binop_error(sigil, op1, op2);            \
543
380
        if (result != op1) {                 \
544
363
          ZVAL_UNDEF(result);                 \
545
363
        }                            \
546
380
        return FAILURE;                     \
547
380
      }                              \
548
32.6k
    } else {                           \
549
32.6k
      op2_lval = Z_LVAL_P(op2);                  \
550
32.6k
    }                                \
551
47.1k
  } while (0);
552
553
ZEND_API void ZEND_FASTCALL convert_to_long(zval *op) /* {{{ */
554
43
{
555
43
  zend_long tmp;
556
557
43
try_again:
558
43
  switch (Z_TYPE_P(op)) {
559
0
    case IS_NULL:
560
7
    case IS_FALSE:
561
7
      ZVAL_LONG(op, 0);
562
7
      break;
563
0
    case IS_TRUE:
564
0
      ZVAL_LONG(op, 1);
565
0
      break;
566
0
    case IS_RESOURCE:
567
0
      tmp = Z_RES_HANDLE_P(op);
568
0
      zval_ptr_dtor(op);
569
0
      ZVAL_LONG(op, tmp);
570
0
      break;
571
23
    case IS_LONG:
572
23
      break;
573
13
    case IS_DOUBLE: {
574
      /* NAN might emit a warning */
575
13
      zend_long new_value = zend_dval_to_lval(Z_DVAL_P(op));
576
13
      zval_ptr_dtor(op);
577
13
      ZVAL_LONG(op, new_value);
578
13
      break;
579
0
    }
580
0
    case IS_STRING:
581
0
      {
582
0
        zend_string *str = Z_STR_P(op);
583
0
        ZVAL_LONG(op, zval_get_long(op));
584
0
        zend_string_release_ex(str, 0);
585
0
      }
586
0
      break;
587
0
    case IS_ARRAY:
588
0
      tmp = (zend_hash_num_elements(Z_ARRVAL_P(op))?1:0);
589
0
      zval_ptr_dtor(op);
590
0
      ZVAL_LONG(op, tmp);
591
0
      break;
592
0
    case IS_OBJECT:
593
0
      {
594
0
        zval dst;
595
596
0
        convert_object_to_type(op, &dst, IS_LONG);
597
0
        zval_ptr_dtor(op);
598
599
0
        if (Z_TYPE(dst) == IS_LONG) {
600
0
          ZVAL_LONG(op, Z_LVAL(dst));
601
0
        } else {
602
0
          ZVAL_LONG(op, 1);
603
0
        }
604
0
        return;
605
0
      }
606
0
    case IS_REFERENCE:
607
0
      zend_unwrap_reference(op);
608
0
      goto try_again;
609
0
    default: ZEND_UNREACHABLE();
610
43
  }
611
43
}
612
/* }}} */
613
614
ZEND_API void ZEND_FASTCALL convert_to_double(zval *op) /* {{{ */
615
1.10k
{
616
1.10k
  double tmp;
617
618
1.10k
try_again:
619
1.10k
  switch (Z_TYPE_P(op)) {
620
0
    case IS_NULL:
621
0
    case IS_FALSE:
622
0
      ZVAL_DOUBLE(op, 0.0);
623
0
      break;
624
0
    case IS_TRUE:
625
0
      ZVAL_DOUBLE(op, 1.0);
626
0
      break;
627
0
    case IS_RESOURCE: {
628
0
        double d = (double) Z_RES_HANDLE_P(op);
629
0
        zval_ptr_dtor(op);
630
0
        ZVAL_DOUBLE(op, d);
631
0
      }
632
0
      break;
633
1.10k
    case IS_LONG:
634
1.10k
      ZVAL_DOUBLE(op, (double) Z_LVAL_P(op));
635
1.10k
      break;
636
0
    case IS_DOUBLE:
637
0
      break;
638
0
    case IS_STRING:
639
0
      {
640
0
        zend_string *str = Z_STR_P(op);
641
642
0
        ZVAL_DOUBLE(op, zend_strtod(ZSTR_VAL(str), NULL));
643
0
        zend_string_release_ex(str, 0);
644
0
      }
645
0
      break;
646
0
    case IS_ARRAY:
647
0
      tmp = (zend_hash_num_elements(Z_ARRVAL_P(op))?1:0);
648
0
      zval_ptr_dtor(op);
649
0
      ZVAL_DOUBLE(op, tmp);
650
0
      break;
651
0
    case IS_OBJECT:
652
0
      {
653
0
        zval dst;
654
655
0
        convert_object_to_type(op, &dst, IS_DOUBLE);
656
0
        zval_ptr_dtor(op);
657
658
0
        if (Z_TYPE(dst) == IS_DOUBLE) {
659
0
          ZVAL_DOUBLE(op, Z_DVAL(dst));
660
0
        } else {
661
0
          ZVAL_DOUBLE(op, 1.0);
662
0
        }
663
0
        break;
664
0
      }
665
0
    case IS_REFERENCE:
666
0
      zend_unwrap_reference(op);
667
0
      goto try_again;
668
0
    default: ZEND_UNREACHABLE();
669
1.10k
  }
670
1.10k
}
671
/* }}} */
672
673
ZEND_API void ZEND_FASTCALL convert_to_null(zval *op) /* {{{ */
674
29
{
675
29
  if (UNEXPECTED(Z_TYPE_P(op) == IS_DOUBLE && zend_isnan(Z_DVAL_P(op)))) {
676
17
    zend_nan_coerced_to_type_warning(IS_NULL);
677
17
  }
678
29
  zval_ptr_dtor(op);
679
29
  ZVAL_NULL(op);
680
29
}
681
/* }}} */
682
683
ZEND_API void ZEND_FASTCALL convert_to_boolean(zval *op) /* {{{ */
684
24
{
685
24
  bool tmp;
686
687
24
try_again:
688
24
  switch (Z_TYPE_P(op)) {
689
0
    case IS_FALSE:
690
0
    case IS_TRUE:
691
0
      break;
692
7
    case IS_NULL:
693
7
      ZVAL_FALSE(op);
694
7
      break;
695
0
    case IS_RESOURCE: {
696
0
        zend_long l = (Z_RES_HANDLE_P(op) ? 1 : 0);
697
698
0
        zval_ptr_dtor(op);
699
0
        ZVAL_BOOL(op, l);
700
0
      }
701
0
      break;
702
0
    case IS_LONG:
703
0
      ZVAL_BOOL(op, Z_LVAL_P(op) ? 1 : 0);
704
0
      break;
705
17
    case IS_DOUBLE: {
706
      /* We compute the new value before emitting the warning as the zval may change */
707
17
      bool new_value = Z_DVAL_P(op) ? true : false;
708
17
      if (UNEXPECTED(zend_isnan(Z_DVAL_P(op)))) {
709
14
        zend_nan_coerced_to_type_warning(_IS_BOOL);
710
14
        zval_ptr_dtor(op);
711
14
      }
712
17
      ZVAL_BOOL(op, new_value);
713
17
      break;
714
0
    }
715
0
    case IS_STRING:
716
0
      {
717
0
        zend_string *str = Z_STR_P(op);
718
719
0
        if (ZSTR_LEN(str) == 0
720
0
          || (ZSTR_LEN(str) == 1 && ZSTR_VAL(str)[0] == '0')) {
721
0
          ZVAL_FALSE(op);
722
0
        } else {
723
0
          ZVAL_TRUE(op);
724
0
        }
725
0
        zend_string_release_ex(str, 0);
726
0
      }
727
0
      break;
728
0
    case IS_ARRAY:
729
0
      tmp = (zend_hash_num_elements(Z_ARRVAL_P(op))?1:0);
730
0
      zval_ptr_dtor(op);
731
0
      ZVAL_BOOL(op, tmp);
732
0
      break;
733
0
    case IS_OBJECT:
734
0
      {
735
0
        zval dst;
736
737
0
        convert_object_to_type(op, &dst, _IS_BOOL);
738
0
        zval_ptr_dtor(op);
739
740
0
        if (Z_TYPE_INFO(dst) == IS_FALSE || Z_TYPE_INFO(dst) == IS_TRUE) {
741
0
          Z_TYPE_INFO_P(op) = Z_TYPE_INFO(dst);
742
0
        } else {
743
0
          ZVAL_TRUE(op);
744
0
        }
745
0
        break;
746
0
      }
747
0
    case IS_REFERENCE:
748
0
      zend_unwrap_reference(op);
749
0
      goto try_again;
750
0
    default: ZEND_UNREACHABLE();
751
24
  }
752
24
}
753
/* }}} */
754
755
ZEND_API void ZEND_FASTCALL _convert_to_string(zval *op) /* {{{ */
756
1.06M
{
757
1.06M
try_again:
758
1.06M
  switch (Z_TYPE_P(op)) {
759
0
    case IS_UNDEF:
760
7.22k
    case IS_NULL:
761
9.66k
    case IS_FALSE: {
762
9.66k
      ZVAL_EMPTY_STRING(op);
763
9.66k
      break;
764
7.22k
    }
765
2.71k
    case IS_TRUE:
766
2.71k
      ZVAL_CHAR(op, '1');
767
2.71k
      break;
768
0
    case IS_STRING:
769
0
      break;
770
0
    case IS_RESOURCE: {
771
0
      zend_string *str = zend_strpprintf(0, "Resource id #" ZEND_LONG_FMT, (zend_long)Z_RES_HANDLE_P(op));
772
0
      zval_ptr_dtor(op);
773
0
      ZVAL_NEW_STR(op, str);
774
0
      break;
775
7.22k
    }
776
1.05M
    case IS_LONG:
777
1.05M
      ZVAL_STR(op, zend_long_to_str(Z_LVAL_P(op)));
778
1.05M
      break;
779
3.57k
    case IS_DOUBLE: {
780
      /* Casting NAN will cause a warning */
781
3.57k
      zend_string *new_value = zend_double_to_str(Z_DVAL_P(op));
782
3.57k
      zval_ptr_dtor(op);
783
3.57k
      ZVAL_NEW_STR(op, new_value);
784
3.57k
      break;
785
7.22k
    }
786
44
    case IS_ARRAY:
787
44
      zend_error(E_WARNING, "Array to string conversion");
788
44
      zval_ptr_dtor(op);
789
44
      ZVAL_INTERNED_STR(op, ZSTR_KNOWN(ZEND_STR_ARRAY_CAPITALIZED));
790
44
      break;
791
0
    case IS_OBJECT: {
792
0
      zval tmp;
793
0
      if (Z_OBJ_HT_P(op)->cast_object(Z_OBJ_P(op), &tmp, IS_STRING) == SUCCESS) {
794
0
        zval_ptr_dtor(op);
795
0
        ZVAL_COPY_VALUE(op, &tmp);
796
0
        return;
797
0
      }
798
0
      if (!EG(exception)) {
799
0
        zend_throw_error(NULL, "Object of class %s could not be converted to string", ZSTR_VAL(Z_OBJCE_P(op)->name));
800
0
      }
801
0
      zval_ptr_dtor(op);
802
0
      ZVAL_EMPTY_STRING(op);
803
0
      break;
804
0
    }
805
0
    case IS_REFERENCE:
806
0
      zend_unwrap_reference(op);
807
0
      goto try_again;
808
0
    default: ZEND_UNREACHABLE();
809
1.06M
  }
810
1.06M
}
811
/* }}} */
812
813
ZEND_API bool ZEND_FASTCALL _try_convert_to_string(zval *op) /* {{{ */
814
36
{
815
36
  zend_string *str;
816
817
36
  ZEND_ASSERT(Z_TYPE_P(op) != IS_STRING);
818
36
  str = zval_try_get_string_func(op);
819
36
  if (UNEXPECTED(!str)) {
820
19
    return 0;
821
19
  }
822
17
  zval_ptr_dtor(op);
823
17
  ZVAL_STR(op, str);
824
17
  return 1;
825
36
}
826
/* }}} */
827
828
static void convert_scalar_to_array(zval *op) /* {{{ */
829
283
{
830
283
  if (UNEXPECTED(Z_TYPE_P(op) == IS_DOUBLE && zend_isnan(Z_DVAL_P(op)))) {
831
5
    zend_nan_coerced_to_type_warning(IS_ARRAY);
832
5
  }
833
283
  HashTable *ht = zend_new_array(1);
834
283
  zend_hash_index_add_new(ht, 0, op);
835
283
  ZVAL_ARR(op, ht);
836
283
}
837
/* }}} */
838
839
ZEND_API void ZEND_FASTCALL convert_to_array(zval *op) /* {{{ */
840
990
{
841
990
try_again:
842
990
  switch (Z_TYPE_P(op)) {
843
560
    case IS_ARRAY:
844
560
      break;
845
/* OBJECTS_OPTIMIZE */
846
77
    case IS_OBJECT:
847
77
      if (Z_OBJCE_P(op) == zend_ce_closure) {
848
5
        convert_scalar_to_array(op);
849
72
      } else if (ZEND_STD_BUILD_OBJECT_PROPERTIES_ARRAY_COMPATIBLE(op)) {
850
        /* Optimized version without rebuilding properties HashTable */
851
40
        HashTable *ht = zend_std_build_object_properties_array(Z_OBJ_P(op));
852
40
        OBJ_RELEASE(Z_OBJ_P(op));
853
40
        ZVAL_ARR(op, ht);
854
40
      } else {
855
32
        HashTable *obj_ht = zend_get_properties_for(op, ZEND_PROP_PURPOSE_ARRAY_CAST);
856
32
        if (obj_ht) {
857
32
          HashTable *new_obj_ht = zend_proptable_to_symtable(obj_ht,
858
32
            (Z_OBJCE_P(op)->default_properties_count ||
859
0
             Z_OBJ_P(op)->handlers != &std_object_handlers ||
860
0
             GC_IS_RECURSIVE(obj_ht)));
861
32
          zval_ptr_dtor(op);
862
32
          ZVAL_ARR(op, new_obj_ht);
863
32
          zend_release_properties(obj_ht);
864
32
        } else {
865
0
          zval_ptr_dtor(op);
866
          /*ZVAL_EMPTY_ARRAY(op);*/
867
0
          array_init(op);
868
0
        }
869
32
      }
870
77
      break;
871
75
    case IS_NULL:
872
      /*ZVAL_EMPTY_ARRAY(op);*/
873
75
      array_init(op);
874
75
      break;
875
0
    case IS_REFERENCE:
876
0
      zend_unwrap_reference(op);
877
0
      goto try_again;
878
278
    default:
879
278
      convert_scalar_to_array(op);
880
278
      break;
881
990
  }
882
990
}
883
/* }}} */
884
885
ZEND_API void ZEND_FASTCALL convert_to_object(zval *op) /* {{{ */
886
12
{
887
12
try_again:
888
12
  switch (Z_TYPE_P(op)) {
889
7
    case IS_ARRAY:
890
7
      {
891
7
        HashTable *ht = zend_symtable_to_proptable(Z_ARR_P(op));
892
7
        zend_object *obj;
893
894
7
        if (GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE) {
895
          /* TODO: try not to duplicate immutable arrays as well ??? */
896
0
          ht = zend_array_dup(ht);
897
7
        } else if (ht != Z_ARR_P(op)) {
898
7
          zval_ptr_dtor(op);
899
7
        } else {
900
0
          GC_DELREF(ht);
901
0
        }
902
7
        obj = zend_objects_new(zend_standard_class_def);
903
7
        obj->properties = ht;
904
7
        ZVAL_OBJ(op, obj);
905
7
        break;
906
0
      }
907
0
    case IS_OBJECT:
908
0
      break;
909
0
    case IS_NULL:
910
0
      object_init(op);
911
0
      break;
912
0
    case IS_REFERENCE:
913
0
      zend_unwrap_reference(op);
914
0
      goto try_again;
915
5
    case IS_DOUBLE:
916
5
      if (UNEXPECTED(zend_isnan(Z_DVAL_P(op)))) {
917
5
        zend_nan_coerced_to_type_warning(IS_OBJECT);
918
5
      }
919
5
      ZEND_FALLTHROUGH;
920
5
    default: {
921
5
      zval tmp;
922
5
      ZVAL_COPY_VALUE(&tmp, op);
923
5
      object_init(op);
924
5
      zend_hash_add_new(Z_OBJPROP_P(op), ZSTR_KNOWN(ZEND_STR_SCALAR), &tmp);
925
5
      break;
926
5
    }
927
12
  }
928
12
}
929
/* }}} */
930
931
ZEND_API void ZEND_COLD zend_incompatible_double_to_long_error(double d)
932
56.7k
{
933
56.7k
  zend_error_unchecked(E_DEPRECATED, "Implicit conversion from float %.*H to int loses precision", -1, d);
934
56.7k
}
935
ZEND_API void ZEND_COLD zend_incompatible_string_to_long_error(const zend_string *s)
936
2.45k
{
937
2.45k
  zend_error(E_DEPRECATED, "Implicit conversion from float-string \"%s\" to int loses precision", ZSTR_VAL(s));
938
2.45k
}
939
ZEND_API void ZEND_COLD zend_oob_double_to_long_error(double d)
940
52.6k
{
941
52.6k
  zend_error_unchecked(E_WARNING, "The float %.*H is not representable as an int, cast occurred", -1, d);
942
52.6k
}
943
ZEND_API void ZEND_COLD zend_oob_string_to_long_error(const zend_string *s)
944
0
{
945
0
  zend_error_unchecked(E_WARNING, "The float-string \"%s\" is not representable as an int, cast occurred", ZSTR_VAL(s));
946
0
}
947
948
ZEND_API void ZEND_COLD zend_nan_coerced_to_type_warning(uint8_t type)
949
635
{
950
635
  zend_error(E_WARNING, "unexpected NAN value was coerced to %s", zend_get_type_by_const(type));
951
635
}
952
953
ZEND_API zend_long ZEND_FASTCALL zval_get_long_func(const zval *op, bool is_strict) /* {{{ */
954
57.5k
{
955
57.5k
try_again:
956
57.5k
  switch (Z_TYPE_P(op)) {
957
94
    case IS_UNDEF:
958
937
    case IS_NULL:
959
2.72k
    case IS_FALSE:
960
2.72k
      return 0;
961
1.26k
    case IS_TRUE:
962
1.26k
      return 1;
963
0
    case IS_RESOURCE:
964
0
      return Z_RES_HANDLE_P(op);
965
10
    case IS_LONG:
966
10
      return Z_LVAL_P(op);
967
27.8k
    case IS_DOUBLE: {
968
27.8k
      double dval = Z_DVAL_P(op);
969
27.8k
      zend_long lval = zend_dval_to_lval(dval);
970
27.8k
      if (UNEXPECTED(is_strict)) {
971
91
        if (!zend_is_long_compatible(dval, lval)) {
972
91
          zend_incompatible_double_to_long_error(dval);
973
91
        }
974
91
      }
975
27.8k
      return lval;
976
937
    }
977
25.5k
    case IS_STRING:
978
25.5k
      {
979
25.5k
        uint8_t type;
980
25.5k
        zend_long lval;
981
25.5k
        double dval;
982
25.5k
        if (0 == (type = is_numeric_string(Z_STRVAL_P(op), Z_STRLEN_P(op), &lval, &dval, true))) {
983
2.34k
          return 0;
984
23.2k
        } else if (EXPECTED(type == IS_LONG)) {
985
14.3k
          return lval;
986
14.3k
        } else {
987
          /* Previously we used strtol here, not is_numeric_string,
988
           * and strtol gives you LONG_MAX/_MIN on overflow.
989
           * We use saturating conversion to emulate strtol()'s
990
           * behaviour.
991
           */
992
           /* Most usages are expected to not be (int) casts */
993
8.85k
          lval = zend_dval_to_lval_cap(dval);
994
8.85k
          if (UNEXPECTED(is_strict)) {
995
0
            if (!zend_is_long_compatible(dval, lval)) {
996
0
              zend_incompatible_string_to_long_error(Z_STR_P(op));
997
0
            }
998
0
          }
999
8.85k
          return lval;
1000
8.85k
        }
1001
25.5k
      }
1002
68
    case IS_ARRAY:
1003
68
      return zend_hash_num_elements(Z_ARRVAL_P(op)) ? 1 : 0;
1004
27
    case IS_OBJECT:
1005
27
      {
1006
27
        zval dst;
1007
27
        convert_object_to_type(op, &dst, IS_LONG);
1008
27
        if (Z_TYPE(dst) == IS_LONG) {
1009
0
          return Z_LVAL(dst);
1010
27
        } else {
1011
27
          return 1;
1012
27
        }
1013
27
      }
1014
10
    case IS_REFERENCE:
1015
10
      op = Z_REFVAL_P(op);
1016
10
      goto try_again;
1017
0
    default: ZEND_UNREACHABLE();
1018
57.5k
  }
1019
0
  return 0;
1020
57.5k
}
1021
/* }}} */
1022
1023
ZEND_API double ZEND_FASTCALL zval_get_double_func(const zval *op) /* {{{ */
1024
60.0k
{
1025
69.8k
try_again:
1026
69.8k
  switch (Z_TYPE_P(op)) {
1027
1.13k
    case IS_NULL:
1028
4.91k
    case IS_FALSE:
1029
4.91k
      return 0.0;
1030
727
    case IS_TRUE:
1031
727
      return 1.0;
1032
0
    case IS_RESOURCE:
1033
0
      return (double) Z_RES_HANDLE_P(op);
1034
28.8k
    case IS_LONG:
1035
28.8k
      return (double) Z_LVAL_P(op);
1036
3.02k
    case IS_DOUBLE:
1037
3.02k
      return Z_DVAL_P(op);
1038
13.5k
    case IS_STRING:
1039
13.5k
      return zend_strtod(Z_STRVAL_P(op), NULL);
1040
8.70k
    case IS_ARRAY:
1041
8.70k
      return zend_hash_num_elements(Z_ARRVAL_P(op)) ? 1.0 : 0.0;
1042
334
    case IS_OBJECT:
1043
334
      {
1044
334
        zval dst;
1045
334
        convert_object_to_type(op, &dst, IS_DOUBLE);
1046
1047
334
        if (Z_TYPE(dst) == IS_DOUBLE) {
1048
0
          return Z_DVAL(dst);
1049
334
        } else {
1050
334
          return 1.0;
1051
334
        }
1052
334
      }
1053
9.76k
    case IS_REFERENCE:
1054
9.76k
      op = Z_REFVAL_P(op);
1055
9.76k
      goto try_again;
1056
0
    default: ZEND_UNREACHABLE();
1057
69.8k
  }
1058
0
  return 0.0;
1059
69.8k
}
1060
/* }}} */
1061
1062
static zend_always_inline zend_string* __zval_get_string_func(const zval *op, bool try) /* {{{ */
1063
1.20M
{
1064
1.21M
try_again:
1065
1.21M
  switch (Z_TYPE_P(op)) {
1066
586k
    case IS_UNDEF:
1067
819k
    case IS_NULL:
1068
859k
    case IS_FALSE:
1069
859k
      return ZSTR_EMPTY_ALLOC();
1070
21.9k
    case IS_TRUE:
1071
21.9k
      return ZSTR_CHAR('1');
1072
0
    case IS_RESOURCE:
1073
0
      return zend_strpprintf(0, "Resource id #" ZEND_LONG_FMT, (zend_long)Z_RES_HANDLE_P(op));
1074
188k
    case IS_LONG:
1075
188k
      return zend_long_to_str(Z_LVAL_P(op));
1076
120k
    case IS_DOUBLE:
1077
120k
      return zend_double_to_str(Z_DVAL_P(op));
1078
5.66k
    case IS_ARRAY:
1079
5.66k
      zend_error(E_WARNING, "Array to string conversion");
1080
5.66k
      return (try && UNEXPECTED(EG(exception))) ?
1081
5.66k
        NULL : ZSTR_KNOWN(ZEND_STR_ARRAY_CAPITALIZED);
1082
8.26k
    case IS_OBJECT: {
1083
8.26k
      zval tmp;
1084
8.26k
      if (Z_OBJ_HT_P(op)->cast_object(Z_OBJ_P(op), &tmp, IS_STRING) == SUCCESS) {
1085
4.85k
        return Z_STR(tmp);
1086
4.85k
      }
1087
3.40k
      if (!EG(exception)) {
1088
942
        zend_throw_error(NULL, "Object of class %s could not be converted to string", ZSTR_VAL(Z_OBJCE_P(op)->name));
1089
942
      }
1090
3.40k
      return try ? NULL : ZSTR_EMPTY_ALLOC();
1091
8.26k
    }
1092
7.51k
    case IS_REFERENCE:
1093
7.51k
      op = Z_REFVAL_P(op);
1094
7.51k
      goto try_again;
1095
694
    case IS_STRING:
1096
694
      return zend_string_copy(Z_STR_P(op));
1097
0
    default: ZEND_UNREACHABLE();
1098
1.21M
  }
1099
0
  return NULL;
1100
1.21M
}
1101
/* }}} */
1102
1103
ZEND_API zend_string* ZEND_FASTCALL zval_get_string_func(const zval *op) /* {{{ */
1104
917k
{
1105
917k
  return __zval_get_string_func(op, false);
1106
917k
}
1107
/* }}} */
1108
1109
ZEND_API zend_string* ZEND_FASTCALL zval_try_get_string_func(const zval *op) /* {{{ */
1110
288k
{
1111
288k
  return __zval_get_string_func(op, true);
1112
288k
}
1113
/* }}} */
1114
1115
3.14k
static ZEND_COLD zend_never_inline void ZEND_FASTCALL zend_binop_error(const char *operator, const zval *op1, const zval *op2) /* {{{ */ {
1116
3.14k
  if (EG(exception)) {
1117
0
    return;
1118
0
  }
1119
1120
3.14k
  zend_type_error("Unsupported operand types: %s %s %s",
1121
3.14k
    zend_zval_type_name(op1), operator, zend_zval_type_name(op2));
1122
3.14k
}
1123
/* }}} */
1124
1125
static zend_never_inline void ZEND_FASTCALL add_function_array(zval *result, const zval *op1, const zval *op2) /* {{{ */
1126
102k
{
1127
102k
  if (result == op1 && Z_ARR_P(op1) == Z_ARR_P(op2)) {
1128
    /* $a += $a */
1129
397
    return;
1130
397
  }
1131
101k
  if (result != op1) {
1132
101k
    ZVAL_ARR(result, zend_array_dup(Z_ARR_P(op1)));
1133
101k
  } else {
1134
129
    SEPARATE_ARRAY(result);
1135
129
  }
1136
101k
  zend_hash_merge(Z_ARRVAL_P(result), Z_ARRVAL_P(op2), zval_add_ref, 0);
1137
101k
}
1138
/* }}} */
1139
1140
static zend_always_inline zend_result add_function_fast(zval *result, zval *op1, zval *op2) /* {{{ */
1141
293k
{
1142
293k
  uint8_t type_pair = TYPE_PAIR(Z_TYPE_P(op1), Z_TYPE_P(op2));
1143
1144
293k
  if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_LONG))) {
1145
61.4k
    fast_long_add_function(result, op1, op2);
1146
61.4k
    return SUCCESS;
1147
231k
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_DOUBLE, IS_DOUBLE))) {
1148
6.36k
    ZVAL_DOUBLE(result, Z_DVAL_P(op1) + Z_DVAL_P(op2));
1149
6.36k
    return SUCCESS;
1150
225k
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_DOUBLE))) {
1151
6.54k
    ZVAL_DOUBLE(result, ((double)Z_LVAL_P(op1)) + Z_DVAL_P(op2));
1152
6.54k
    return SUCCESS;
1153
219k
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_DOUBLE, IS_LONG))) {
1154
6.47k
    ZVAL_DOUBLE(result, Z_DVAL_P(op1) + ((double)Z_LVAL_P(op2)));
1155
6.47k
    return SUCCESS;
1156
212k
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_ARRAY, IS_ARRAY))) {
1157
102k
    add_function_array(result, op1, op2);
1158
102k
    return SUCCESS;
1159
110k
  } else {
1160
110k
    return FAILURE;
1161
110k
  }
1162
293k
} /* }}} */
1163
1164
static zend_never_inline zend_result ZEND_FASTCALL add_function_slow(zval *result, zval *op1, zval *op2) /* {{{ */
1165
57.6k
{
1166
57.6k
  ZVAL_DEREF(op1);
1167
57.6k
  ZVAL_DEREF(op2);
1168
57.6k
  if (add_function_fast(result, op1, op2) == SUCCESS) {
1169
4.79k
    return SUCCESS;
1170
4.79k
  }
1171
1172
57.6k
  ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_ADD);
1173
1174
52.8k
  zval op1_copy, op2_copy;
1175
52.8k
  if (UNEXPECTED(zendi_try_convert_scalar_to_number(op1, &op1_copy) == FAILURE)
1176
52.6k
      || UNEXPECTED(zendi_try_convert_scalar_to_number(op2, &op2_copy) == FAILURE)) {
1177
361
    zend_binop_error("+", op1, op2);
1178
361
    if (result != op1) {
1179
271
      ZVAL_UNDEF(result);
1180
271
    }
1181
361
    return FAILURE;
1182
361
  }
1183
1184
52.4k
  if (result == op1) {
1185
9.51k
    zval_ptr_dtor(result);
1186
9.51k
  }
1187
1188
52.4k
  if (add_function_fast(result, &op1_copy, &op2_copy) == SUCCESS) {
1189
52.4k
    return SUCCESS;
1190
52.4k
  }
1191
1192
0
  ZEND_ASSERT(0 && "Operation must succeed");
1193
0
  return FAILURE;
1194
0
} /* }}} */
1195
1196
ZEND_API zend_result ZEND_FASTCALL add_function(zval *result, zval *op1, zval *op2) /* {{{ */
1197
183k
{
1198
183k
  if (add_function_fast(result, op1, op2) == SUCCESS) {
1199
125k
    return SUCCESS;
1200
125k
  } else {
1201
57.6k
    return add_function_slow(result, op1, op2);
1202
57.6k
  }
1203
183k
}
1204
/* }}} */
1205
1206
static zend_always_inline zend_result sub_function_fast(zval *result, zval *op1, zval *op2) /* {{{ */
1207
69.8k
{
1208
69.8k
  uint8_t type_pair = TYPE_PAIR(Z_TYPE_P(op1), Z_TYPE_P(op2));
1209
1210
69.8k
  if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_LONG))) {
1211
22.9k
    fast_long_sub_function(result, op1, op2);
1212
22.9k
    return SUCCESS;
1213
46.9k
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_DOUBLE, IS_DOUBLE))) {
1214
1.96k
    ZVAL_DOUBLE(result, Z_DVAL_P(op1) - Z_DVAL_P(op2));
1215
1.96k
    return SUCCESS;
1216
44.9k
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_DOUBLE))) {
1217
3.90k
    ZVAL_DOUBLE(result, ((double)Z_LVAL_P(op1)) - Z_DVAL_P(op2));
1218
3.90k
    return SUCCESS;
1219
41.0k
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_DOUBLE, IS_LONG))) {
1220
4.28k
    ZVAL_DOUBLE(result, Z_DVAL_P(op1) - ((double)Z_LVAL_P(op2)));
1221
4.28k
    return SUCCESS;
1222
36.7k
  } else {
1223
36.7k
    return FAILURE;
1224
36.7k
  }
1225
69.8k
}
1226
/* }}} */
1227
1228
static zend_never_inline zend_result ZEND_FASTCALL sub_function_slow(zval *result, zval *op1, zval *op2) /* {{{ */
1229
19.0k
{
1230
19.0k
  ZVAL_DEREF(op1);
1231
19.0k
  ZVAL_DEREF(op2);
1232
19.0k
  if (sub_function_fast(result, op1, op2) == SUCCESS) {
1233
1.40k
    return SUCCESS;
1234
1.40k
  }
1235
1236
19.0k
  ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_SUB);
1237
1238
17.6k
  zval op1_copy, op2_copy;
1239
17.6k
  if (UNEXPECTED(zendi_try_convert_scalar_to_number(op1, &op1_copy) == FAILURE)
1240
17.4k
      || UNEXPECTED(zendi_try_convert_scalar_to_number(op2, &op2_copy) == FAILURE)) {
1241
357
    zend_binop_error("-", op1, op2);
1242
357
    if (result != op1) {
1243
301
      ZVAL_UNDEF(result);
1244
301
    }
1245
357
    return FAILURE;
1246
357
  }
1247
1248
17.3k
  if (result == op1) {
1249
1.72k
    zval_ptr_dtor(result);
1250
1.72k
  }
1251
1252
17.3k
  if (sub_function_fast(result, &op1_copy, &op2_copy) == SUCCESS) {
1253
17.3k
    return SUCCESS;
1254
17.3k
  }
1255
1256
0
  ZEND_ASSERT(0 && "Operation must succeed");
1257
0
  return FAILURE;
1258
0
}
1259
/* }}} */
1260
1261
ZEND_API zend_result ZEND_FASTCALL sub_function(zval *result, zval *op1, zval *op2) /* {{{ */
1262
33.4k
{
1263
33.4k
  if (sub_function_fast(result, op1, op2) == SUCCESS) {
1264
14.3k
    return SUCCESS;
1265
19.0k
  } else {
1266
19.0k
    return sub_function_slow(result, op1, op2);
1267
19.0k
  }
1268
33.4k
}
1269
/* }}} */
1270
1271
static zend_always_inline zend_result mul_function_fast(zval *result, zval *op1, zval *op2) /* {{{ */
1272
223k
{
1273
223k
  uint8_t type_pair = TYPE_PAIR(Z_TYPE_P(op1), Z_TYPE_P(op2));
1274
1275
223k
  if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_LONG))) {
1276
88.6k
    zend_long overflow;
1277
88.6k
    ZEND_SIGNED_MULTIPLY_LONG(
1278
88.6k
      Z_LVAL_P(op1), Z_LVAL_P(op2),
1279
88.6k
      Z_LVAL_P(result), Z_DVAL_P(result), overflow);
1280
88.6k
    Z_TYPE_INFO_P(result) = overflow ? IS_DOUBLE : IS_LONG;
1281
88.6k
    return SUCCESS;
1282
134k
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_DOUBLE, IS_DOUBLE))) {
1283
1.70k
    ZVAL_DOUBLE(result, Z_DVAL_P(op1) * Z_DVAL_P(op2));
1284
1.70k
    return SUCCESS;
1285
133k
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_DOUBLE))) {
1286
1.75k
    ZVAL_DOUBLE(result, ((double)Z_LVAL_P(op1)) * Z_DVAL_P(op2));
1287
1.75k
    return SUCCESS;
1288
131k
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_DOUBLE, IS_LONG))) {
1289
11.1k
    ZVAL_DOUBLE(result, Z_DVAL_P(op1) * ((double)Z_LVAL_P(op2)));
1290
11.1k
    return SUCCESS;
1291
120k
  } else {
1292
120k
    return FAILURE;
1293
120k
  }
1294
223k
}
1295
/* }}} */
1296
1297
static zend_never_inline zend_result ZEND_FASTCALL mul_function_slow(zval *result, zval *op1, zval *op2) /* {{{ */
1298
60.6k
{
1299
60.6k
  ZVAL_DEREF(op1);
1300
60.6k
  ZVAL_DEREF(op2);
1301
60.6k
  if (mul_function_fast(result, op1, op2) == SUCCESS) {
1302
1.13k
    return SUCCESS;
1303
1.13k
  }
1304
1305
60.6k
  ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_MUL);
1306
1307
59.4k
  zval op1_copy, op2_copy;
1308
59.4k
  if (UNEXPECTED(zendi_try_convert_scalar_to_number(op1, &op1_copy) == FAILURE)
1309
59.1k
      || UNEXPECTED(zendi_try_convert_scalar_to_number(op2, &op2_copy) == FAILURE)) {
1310
357
    zend_binop_error("*", op1, op2);
1311
357
    if (result != op1) {
1312
272
      ZVAL_UNDEF(result);
1313
272
    }
1314
357
    return FAILURE;
1315
357
  }
1316
1317
59.1k
  if (result == op1) {
1318
6.86k
    zval_ptr_dtor(result);
1319
6.86k
  }
1320
1321
59.1k
  if (mul_function_fast(result, &op1_copy, &op2_copy) == SUCCESS) {
1322
59.1k
    return SUCCESS;
1323
59.1k
  }
1324
1325
0
  ZEND_ASSERT(0 && "Operation must succeed");
1326
0
  return FAILURE;
1327
0
}
1328
/* }}} */
1329
1330
ZEND_API zend_result ZEND_FASTCALL mul_function(zval *result, zval *op1, zval *op2) /* {{{ */
1331
103k
{
1332
103k
  if (mul_function_fast(result, op1, op2) == SUCCESS) {
1333
43.0k
    return SUCCESS;
1334
60.6k
  } else {
1335
60.6k
    return mul_function_slow(result, op1, op2);
1336
60.6k
  }
1337
103k
}
1338
/* }}} */
1339
1340
static void ZEND_COLD zend_power_base_0_exponent_lt_0_error(void)
1341
0
{
1342
0
  zend_error(E_DEPRECATED, "Power of base 0 and negative exponent is deprecated");
1343
0
}
1344
1345
static double safe_pow(double base, double exponent)
1346
2.51k
{
1347
2.51k
  if (UNEXPECTED(base == 0.0 && exponent < 0.0)) {
1348
0
    zend_power_base_0_exponent_lt_0_error();
1349
0
  }
1350
1351
2.51k
  return pow(base, exponent);
1352
2.51k
}
1353
1354
static zend_result ZEND_FASTCALL pow_function_base(zval *result, zval *op1, zval *op2) /* {{{ */
1355
5.51k
{
1356
5.51k
  uint8_t type_pair = TYPE_PAIR(Z_TYPE_P(op1), Z_TYPE_P(op2));
1357
1358
5.51k
  if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_LONG))) {
1359
3.33k
    if (Z_LVAL_P(op2) >= 0) {
1360
3.10k
      zend_long l1 = 1, l2 = Z_LVAL_P(op1), i = Z_LVAL_P(op2);
1361
1362
3.10k
      if (i == 0) {
1363
591
        ZVAL_LONG(result, 1L);
1364
591
        return SUCCESS;
1365
2.51k
      } else if (l2 == 0) {
1366
502
        ZVAL_LONG(result, 0);
1367
502
        return SUCCESS;
1368
502
      }
1369
1370
12.7k
      while (i >= 1) {
1371
11.9k
        zend_long overflow;
1372
11.9k
        double dval = 0.0;
1373
1374
11.9k
        if (i % 2) {
1375
4.29k
          --i;
1376
4.29k
          ZEND_SIGNED_MULTIPLY_LONG(l1, l2, l1, dval, overflow);
1377
4.29k
          if (overflow) {
1378
491
            ZVAL_DOUBLE(result, dval * safe_pow(l2, i));
1379
491
            return SUCCESS;
1380
491
          }
1381
7.67k
        } else {
1382
7.67k
          i /= 2;
1383
7.67k
          ZEND_SIGNED_MULTIPLY_LONG(l2, l2, l2, dval, overflow);
1384
7.67k
          if (overflow) {
1385
702
            ZVAL_DOUBLE(result, (double)l1 * safe_pow(dval, i));
1386
702
            return SUCCESS;
1387
702
          }
1388
7.67k
        }
1389
11.9k
      }
1390
      /* i == 0 */
1391
820
      ZVAL_LONG(result, l1);
1392
820
    } else {
1393
224
      ZVAL_DOUBLE(result, safe_pow((double)Z_LVAL_P(op1), (double)Z_LVAL_P(op2)));
1394
224
    }
1395
1.04k
    return SUCCESS;
1396
3.33k
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_DOUBLE, IS_DOUBLE))) {
1397
148
    ZVAL_DOUBLE(result, safe_pow(Z_DVAL_P(op1), Z_DVAL_P(op2)));
1398
148
    return SUCCESS;
1399
2.03k
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_DOUBLE))) {
1400
506
    ZVAL_DOUBLE(result, safe_pow((double)Z_LVAL_P(op1), Z_DVAL_P(op2)));
1401
506
    return SUCCESS;
1402
1.53k
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_DOUBLE, IS_LONG))) {
1403
439
    ZVAL_DOUBLE(result, safe_pow(Z_DVAL_P(op1), (double)Z_LVAL_P(op2)));
1404
439
    return SUCCESS;
1405
1.09k
  } else {
1406
1.09k
    return FAILURE;
1407
1.09k
  }
1408
5.51k
}
1409
/* }}} */
1410
1411
ZEND_API zend_result ZEND_FASTCALL pow_function(zval *result, zval *op1, zval *op2) /* {{{ */
1412
4.51k
{
1413
4.51k
  ZVAL_DEREF(op1);
1414
4.51k
  ZVAL_DEREF(op2);
1415
4.51k
  if (pow_function_base(result, op1, op2) == SUCCESS) {
1416
3.41k
    return SUCCESS;
1417
3.41k
  }
1418
1419
4.51k
  ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_POW);
1420
1421
1.09k
  zval op1_copy, op2_copy;
1422
1.09k
  if (UNEXPECTED(zendi_try_convert_scalar_to_number(op1, &op1_copy) == FAILURE)
1423
1.01k
      || UNEXPECTED(zendi_try_convert_scalar_to_number(op2, &op2_copy) == FAILURE)) {
1424
88
    zend_binop_error("**", op1, op2);
1425
88
    if (result != op1) {
1426
49
      ZVAL_UNDEF(result);
1427
49
    }
1428
88
    return FAILURE;
1429
88
  }
1430
1431
1.00k
  if (result == op1) {
1432
49
    zval_ptr_dtor(result);
1433
49
  }
1434
1435
1.00k
  if (pow_function_base(result, &op1_copy, &op2_copy) == SUCCESS) {
1436
1.00k
    return SUCCESS;
1437
1.00k
  }
1438
1439
0
  ZEND_ASSERT(0 && "Operation must succeed");
1440
0
  return FAILURE;
1441
0
}
1442
/* }}} */
1443
1444
typedef enum {
1445
  DIV_SUCCESS,
1446
  DIV_BY_ZERO,
1447
  DIV_TYPES_NOT_HANDLED
1448
} zend_div_status;
1449
1450
static zend_div_status ZEND_FASTCALL div_function_base(zval *result, const zval *op1, const zval *op2) /* {{{ */
1451
20.3k
{
1452
20.3k
  uint8_t type_pair = TYPE_PAIR(Z_TYPE_P(op1), Z_TYPE_P(op2));
1453
1454
20.3k
  if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_LONG))) {
1455
10.2k
    if (Z_LVAL_P(op2) == 0) {
1456
150
      return DIV_BY_ZERO;
1457
10.0k
    } else if (Z_LVAL_P(op2) == -1 && Z_LVAL_P(op1) == ZEND_LONG_MIN) {
1458
      /* Prevent overflow error/crash */
1459
73
      ZVAL_DOUBLE(result, (double) ZEND_LONG_MIN / -1);
1460
73
      return DIV_SUCCESS;
1461
73
    }
1462
10.0k
    if (Z_LVAL_P(op1) % Z_LVAL_P(op2) == 0) { /* integer */
1463
3.00k
      ZVAL_LONG(result, Z_LVAL_P(op1) / Z_LVAL_P(op2));
1464
7.02k
    } else {
1465
7.02k
      ZVAL_DOUBLE(result, ((double) Z_LVAL_P(op1)) / Z_LVAL_P(op2));
1466
7.02k
    }
1467
10.0k
    return DIV_SUCCESS;
1468
10.2k
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_DOUBLE, IS_DOUBLE))) {
1469
989
    if (Z_DVAL_P(op2) == 0) {
1470
6
      return DIV_BY_ZERO;
1471
6
    }
1472
983
    ZVAL_DOUBLE(result, Z_DVAL_P(op1) / Z_DVAL_P(op2));
1473
983
    return DIV_SUCCESS;
1474
9.14k
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_DOUBLE, IS_LONG))) {
1475
2.93k
    if (Z_LVAL_P(op2) == 0) {
1476
9
      return DIV_BY_ZERO;
1477
9
    }
1478
2.92k
    ZVAL_DOUBLE(result, Z_DVAL_P(op1) / (double)Z_LVAL_P(op2));
1479
2.92k
    return DIV_SUCCESS;
1480
6.21k
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_DOUBLE))) {
1481
2.27k
    if (Z_DVAL_P(op2) == 0) {
1482
15
      return DIV_BY_ZERO;
1483
15
    }
1484
2.25k
    ZVAL_DOUBLE(result, (double)Z_LVAL_P(op1) / Z_DVAL_P(op2));
1485
2.25k
    return DIV_SUCCESS;
1486
3.93k
  } else {
1487
3.93k
    return DIV_TYPES_NOT_HANDLED;
1488
3.93k
  }
1489
20.3k
}
1490
/* }}} */
1491
1492
ZEND_API zend_result ZEND_FASTCALL div_function(zval *result, zval *op1, zval *op2) /* {{{ */
1493
16.7k
{
1494
16.7k
  ZVAL_DEREF(op1);
1495
16.7k
  ZVAL_DEREF(op2);
1496
1497
16.7k
  zend_div_status retval = div_function_base(result, op1, op2);
1498
16.7k
  if (EXPECTED(retval == DIV_SUCCESS)) {
1499
12.7k
    return SUCCESS;
1500
12.7k
  }
1501
1502
4.00k
  if (UNEXPECTED(retval == DIV_BY_ZERO)) {
1503
64
    goto div_by_zero;
1504
64
  }
1505
1506
4.00k
  ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_DIV);
1507
1508
3.93k
  zval result_copy, op1_copy, op2_copy;
1509
3.93k
  if (UNEXPECTED(zendi_try_convert_scalar_to_number(op1, &op1_copy) == FAILURE)
1510
3.63k
      || UNEXPECTED(zendi_try_convert_scalar_to_number(op2, &op2_copy) == FAILURE)) {
1511
355
    zend_binop_error("/", op1, op2);
1512
355
    if (result != op1) {
1513
298
      ZVAL_UNDEF(result);
1514
298
    }
1515
355
    return FAILURE;
1516
355
  }
1517
1518
3.58k
  retval = div_function_base(&result_copy, &op1_copy, &op2_copy);
1519
3.58k
  if (retval == DIV_SUCCESS) {
1520
3.46k
    if (result == op1) {
1521
606
      zval_ptr_dtor(result);
1522
606
    }
1523
3.46k
    ZVAL_COPY_VALUE(result, &result_copy);
1524
3.46k
    return SUCCESS;
1525
3.46k
  }
1526
1527
180
div_by_zero:
1528
180
  ZEND_ASSERT(retval == DIV_BY_ZERO && "DIV_TYPES_NOT_HANDLED should not occur here");
1529
180
  if (result != op1) {
1530
153
    ZVAL_UNDEF(result);
1531
153
  }
1532
180
  zend_throw_error(zend_ce_division_by_zero_error, "Division by zero");
1533
180
  return FAILURE;
1534
180
}
1535
/* }}} */
1536
1537
ZEND_API zend_result ZEND_FASTCALL mod_function(zval *result, zval *op1, zval *op2) /* {{{ */
1538
27.6k
{
1539
27.6k
  zend_long op1_lval, op2_lval;
1540
1541
27.6k
  convert_op1_op2_long(op1, op1_lval, op2, op2_lval, result, ZEND_MOD, "%");
1542
1543
27.4k
  if (op2_lval == 0) {
1544
    /* modulus by zero */
1545
214
    if (EG(current_execute_data) && !CG(in_compilation)) {
1546
214
      zend_throw_exception_ex(zend_ce_division_by_zero_error, 0, "Modulo by zero");
1547
214
    } else {
1548
0
      zend_error_noreturn(E_ERROR, "Modulo by zero");
1549
0
    }
1550
214
    if (op1 != result) {
1551
154
      ZVAL_UNDEF(result);
1552
154
    }
1553
214
    return FAILURE;
1554
214
  }
1555
1556
27.2k
  if (op1 == result) {
1557
270
    zval_ptr_dtor(result);
1558
270
  }
1559
1560
27.2k
  if (op2_lval == -1) {
1561
    /* Prevent overflow error/crash if op1==LONG_MIN */
1562
713
    ZVAL_LONG(result, 0);
1563
713
    return SUCCESS;
1564
713
  }
1565
1566
26.5k
  ZVAL_LONG(result, op1_lval % op2_lval);
1567
26.5k
  return SUCCESS;
1568
27.2k
}
1569
/* }}} */
1570
1571
ZEND_API zend_result ZEND_FASTCALL boolean_xor_function(zval *result, zval *op1, zval *op2) /* {{{ */
1572
9.18k
{
1573
9.18k
  int op1_val, op2_val;
1574
1575
9.18k
  do {
1576
9.18k
    if (Z_TYPE_P(op1) == IS_FALSE) {
1577
849
      op1_val = 0;
1578
8.33k
    } else if (EXPECTED(Z_TYPE_P(op1) == IS_TRUE)) {
1579
878
      op1_val = 1;
1580
7.45k
    } else {
1581
7.45k
      if (Z_ISREF_P(op1)) {
1582
0
        op1 = Z_REFVAL_P(op1);
1583
0
        if (Z_TYPE_P(op1) == IS_FALSE) {
1584
0
          op1_val = 0;
1585
0
          break;
1586
0
        } else if (EXPECTED(Z_TYPE_P(op1) == IS_TRUE)) {
1587
0
          op1_val = 1;
1588
0
          break;
1589
0
        }
1590
0
      }
1591
7.45k
      ZEND_TRY_BINARY_OP1_OBJECT_OPERATION(ZEND_BOOL_XOR);
1592
7.45k
      op1_val = zend_is_true(op1);
1593
7.45k
    }
1594
9.18k
  } while (0);
1595
9.18k
  do {
1596
9.18k
    if (Z_TYPE_P(op2) == IS_FALSE) {
1597
509
      op2_val = 0;
1598
8.67k
    } else if (EXPECTED(Z_TYPE_P(op2) == IS_TRUE)) {
1599
675
      op2_val = 1;
1600
7.99k
    } else {
1601
7.99k
      if (Z_ISREF_P(op2)) {
1602
0
        op2 = Z_REFVAL_P(op2);
1603
0
        if (Z_TYPE_P(op2) == IS_FALSE) {
1604
0
          op2_val = 0;
1605
0
          break;
1606
0
        } else if (EXPECTED(Z_TYPE_P(op2) == IS_TRUE)) {
1607
0
          op2_val = 1;
1608
0
          break;
1609
0
        }
1610
0
      }
1611
7.99k
      ZEND_TRY_BINARY_OP2_OBJECT_OPERATION(ZEND_BOOL_XOR);
1612
7.99k
      op2_val = zend_is_true(op2);
1613
7.99k
    }
1614
9.18k
  } while (0);
1615
1616
9.18k
  ZVAL_BOOL(result, op1_val ^ op2_val);
1617
9.18k
  return SUCCESS;
1618
9.18k
}
1619
/* }}} */
1620
1621
ZEND_API zend_result ZEND_FASTCALL boolean_not_function(zval *result, zval *op1) /* {{{ */
1622
267k
{
1623
267k
  if (Z_TYPE_P(op1) < IS_TRUE) {
1624
6.79k
    ZVAL_TRUE(result);
1625
260k
  } else if (EXPECTED(Z_TYPE_P(op1) == IS_TRUE)) {
1626
4.99k
    ZVAL_FALSE(result);
1627
255k
  } else {
1628
255k
    if (Z_ISREF_P(op1)) {
1629
0
      op1 = Z_REFVAL_P(op1);
1630
0
      if (Z_TYPE_P(op1) < IS_TRUE) {
1631
0
        ZVAL_TRUE(result);
1632
0
        return SUCCESS;
1633
0
      } else if (EXPECTED(Z_TYPE_P(op1) == IS_TRUE)) {
1634
0
        ZVAL_FALSE(result);
1635
0
        return SUCCESS;
1636
0
      }
1637
0
    }
1638
255k
    ZEND_TRY_UNARY_OBJECT_OPERATION(ZEND_BOOL_NOT);
1639
1640
255k
    ZVAL_BOOL(result, !zend_is_true(op1));
1641
255k
  }
1642
267k
  return SUCCESS;
1643
267k
}
1644
/* }}} */
1645
1646
ZEND_API zend_result ZEND_FASTCALL bitwise_not_function(zval *result, zval *op1) /* {{{ */
1647
14.2k
{
1648
14.2k
try_again:
1649
14.2k
  switch (Z_TYPE_P(op1)) {
1650
4.33k
    case IS_LONG:
1651
4.33k
      ZVAL_LONG(result, ~Z_LVAL_P(op1));
1652
4.33k
      return SUCCESS;
1653
1.62k
    case IS_DOUBLE: {
1654
1.62k
      zend_long lval = zend_dval_to_lval_safe(Z_DVAL_P(op1));
1655
1.62k
      if (EG(exception)) {
1656
0
        if (result != op1) {
1657
0
          ZVAL_UNDEF(result);
1658
0
        }
1659
0
        return FAILURE;
1660
0
      }
1661
1.62k
      ZVAL_LONG(result, ~lval);
1662
1.62k
      return SUCCESS;
1663
1.62k
    }
1664
8.14k
    case IS_STRING: {
1665
8.14k
      size_t i;
1666
1667
8.14k
      if (Z_STRLEN_P(op1) == 1) {
1668
280
        zend_uchar not = (zend_uchar) ~*Z_STRVAL_P(op1);
1669
280
        ZVAL_CHAR(result, not);
1670
7.86k
      } else {
1671
7.86k
        ZVAL_NEW_STR(result, zend_string_alloc(Z_STRLEN_P(op1), 0));
1672
295M
        for (i = 0; i < Z_STRLEN_P(op1); i++) {
1673
295M
          Z_STRVAL_P(result)[i] = ~Z_STRVAL_P(op1)[i];
1674
295M
        }
1675
7.86k
        Z_STRVAL_P(result)[i] = 0;
1676
7.86k
      }
1677
8.14k
      return SUCCESS;
1678
1.62k
    }
1679
39
    case IS_REFERENCE:
1680
39
      op1 = Z_REFVAL_P(op1);
1681
39
      goto try_again;
1682
147
    default:
1683
147
      ZEND_TRY_UNARY_OBJECT_OPERATION(ZEND_BW_NOT);
1684
1685
147
      if (result != op1) {
1686
147
        ZVAL_UNDEF(result);
1687
147
      }
1688
147
      zend_type_error("Cannot perform bitwise not on %s", zend_zval_value_name(op1));
1689
147
      return FAILURE;
1690
14.2k
  }
1691
14.2k
}
1692
/* }}} */
1693
1694
ZEND_API zend_result ZEND_FASTCALL bitwise_or_function(zval *result, zval *op1, zval *op2) /* {{{ */
1695
23.3k
{
1696
23.3k
  zend_long op1_lval, op2_lval;
1697
1698
23.3k
  if (EXPECTED(Z_TYPE_P(op1) == IS_LONG) && EXPECTED(Z_TYPE_P(op2) == IS_LONG)) {
1699
1.69k
    ZVAL_LONG(result, Z_LVAL_P(op1) | Z_LVAL_P(op2));
1700
1.69k
    return SUCCESS;
1701
1.69k
  }
1702
1703
21.6k
  ZVAL_DEREF(op1);
1704
21.6k
  ZVAL_DEREF(op2);
1705
1706
21.6k
  if (Z_TYPE_P(op1) == IS_STRING && EXPECTED(Z_TYPE_P(op2) == IS_STRING)) {
1707
14.7k
    zval *longer, *shorter;
1708
14.7k
    zend_string *str;
1709
14.7k
    size_t i;
1710
1711
14.7k
    if (EXPECTED(Z_STRLEN_P(op1) >= Z_STRLEN_P(op2))) {
1712
9.50k
      if (EXPECTED(Z_STRLEN_P(op1) == Z_STRLEN_P(op2)) && Z_STRLEN_P(op1) == 1) {
1713
231
        zend_uchar or = (zend_uchar) (*Z_STRVAL_P(op1) | *Z_STRVAL_P(op2));
1714
231
        if (result==op1) {
1715
7
          zval_ptr_dtor_str(result);
1716
7
        }
1717
231
        ZVAL_CHAR(result, or);
1718
231
        return SUCCESS;
1719
231
      }
1720
9.27k
      longer = op1;
1721
9.27k
      shorter = op2;
1722
9.27k
    } else {
1723
5.19k
      longer = op2;
1724
5.19k
      shorter = op1;
1725
5.19k
    }
1726
1727
14.4k
    str = zend_string_alloc(Z_STRLEN_P(longer), 0);
1728
4.17M
    for (i = 0; i < Z_STRLEN_P(shorter); i++) {
1729
4.15M
      ZSTR_VAL(str)[i] = Z_STRVAL_P(longer)[i] | Z_STRVAL_P(shorter)[i];
1730
4.15M
    }
1731
14.4k
    memcpy(ZSTR_VAL(str) + i, Z_STRVAL_P(longer) + i, Z_STRLEN_P(longer) - i + 1);
1732
14.4k
    if (result==op1) {
1733
18
      zval_ptr_dtor_str(result);
1734
18
    }
1735
14.4k
    ZVAL_NEW_STR(result, str);
1736
14.4k
    return SUCCESS;
1737
14.7k
  }
1738
1739
6.93k
  if (UNEXPECTED(Z_TYPE_P(op1) != IS_LONG)) {
1740
3.89k
    bool failed;
1741
3.89k
    ZEND_TRY_BINARY_OP1_OBJECT_OPERATION(ZEND_BW_OR);
1742
3.89k
    op1_lval = zendi_try_get_long(op1, &failed);
1743
3.89k
    if (UNEXPECTED(failed)) {
1744
40
      zend_binop_error("|", op1, op2);
1745
40
      if (result != op1) {
1746
20
        ZVAL_UNDEF(result);
1747
20
      }
1748
40
      return FAILURE;
1749
40
    }
1750
3.89k
  } else {
1751
3.03k
    op1_lval = Z_LVAL_P(op1);
1752
3.03k
  }
1753
6.89k
  if (UNEXPECTED(Z_TYPE_P(op2) != IS_LONG)) {
1754
3.77k
    bool failed;
1755
3.77k
    ZEND_TRY_BINARY_OP2_OBJECT_OPERATION(ZEND_BW_OR);
1756
3.77k
    op2_lval = zendi_try_get_long(op2, &failed);
1757
3.77k
    if (UNEXPECTED(failed)) {
1758
43
      zend_binop_error("|", op1, op2);
1759
43
      if (result != op1) {
1760
31
        ZVAL_UNDEF(result);
1761
31
      }
1762
43
      return FAILURE;
1763
43
    }
1764
3.77k
  } else {
1765
3.11k
    op2_lval = Z_LVAL_P(op2);
1766
3.11k
  }
1767
1768
6.85k
  if (op1 == result) {
1769
119
    zval_ptr_dtor(result);
1770
119
  }
1771
6.85k
  ZVAL_LONG(result, op1_lval | op2_lval);
1772
6.85k
  return SUCCESS;
1773
6.89k
}
1774
/* }}} */
1775
1776
ZEND_API zend_result ZEND_FASTCALL bitwise_and_function(zval *result, zval *op1, zval *op2) /* {{{ */
1777
338k
{
1778
338k
  zend_long op1_lval, op2_lval;
1779
1780
338k
  if (EXPECTED(Z_TYPE_P(op1) == IS_LONG) && EXPECTED(Z_TYPE_P(op2) == IS_LONG)) {
1781
15.5k
    ZVAL_LONG(result, Z_LVAL_P(op1) & Z_LVAL_P(op2));
1782
15.5k
    return SUCCESS;
1783
15.5k
  }
1784
1785
323k
  ZVAL_DEREF(op1);
1786
323k
  ZVAL_DEREF(op2);
1787
1788
323k
  if (Z_TYPE_P(op1) == IS_STRING && Z_TYPE_P(op2) == IS_STRING) {
1789
291k
    zval *longer, *shorter;
1790
291k
    zend_string *str;
1791
291k
    size_t i;
1792
1793
291k
    if (EXPECTED(Z_STRLEN_P(op1) >= Z_STRLEN_P(op2))) {
1794
78.0k
      if (EXPECTED(Z_STRLEN_P(op1) == Z_STRLEN_P(op2)) && Z_STRLEN_P(op1) == 1) {
1795
112
        zend_uchar and = (zend_uchar) (*Z_STRVAL_P(op1) & *Z_STRVAL_P(op2));
1796
112
        if (result==op1) {
1797
19
          zval_ptr_dtor_str(result);
1798
19
        }
1799
112
        ZVAL_CHAR(result, and);
1800
112
        return SUCCESS;
1801
112
      }
1802
77.9k
      longer = op1;
1803
77.9k
      shorter = op2;
1804
213k
    } else {
1805
213k
      longer = op2;
1806
213k
      shorter = op1;
1807
213k
    }
1808
1809
291k
    str = zend_string_alloc(Z_STRLEN_P(shorter), 0);
1810
2.42G
    for (i = 0; i < Z_STRLEN_P(shorter); i++) {
1811
2.42G
      ZSTR_VAL(str)[i] = Z_STRVAL_P(shorter)[i] & Z_STRVAL_P(longer)[i];
1812
2.42G
    }
1813
291k
    ZSTR_VAL(str)[i] = 0;
1814
291k
    if (result==op1) {
1815
190k
      zval_ptr_dtor_str(result);
1816
190k
    }
1817
291k
    ZVAL_NEW_STR(result, str);
1818
291k
    return SUCCESS;
1819
291k
  }
1820
1821
31.6k
  if (UNEXPECTED(Z_TYPE_P(op1) != IS_LONG)) {
1822
13.6k
    bool failed;
1823
13.6k
    ZEND_TRY_BINARY_OP1_OBJECT_OPERATION(ZEND_BW_AND);
1824
13.6k
    op1_lval = zendi_try_get_long(op1, &failed);
1825
13.6k
    if (UNEXPECTED(failed)) {
1826
70
      zend_binop_error("&", op1, op2);
1827
70
      if (result != op1) {
1828
40
        ZVAL_UNDEF(result);
1829
40
      }
1830
70
      return FAILURE;
1831
70
    }
1832
17.9k
  } else {
1833
17.9k
    op1_lval = Z_LVAL_P(op1);
1834
17.9k
  }
1835
31.5k
  if (UNEXPECTED(Z_TYPE_P(op2) != IS_LONG)) {
1836
26.7k
    bool failed;
1837
26.7k
    ZEND_TRY_BINARY_OP2_OBJECT_OPERATION(ZEND_BW_AND);
1838
26.7k
    op2_lval = zendi_try_get_long(op2, &failed);
1839
26.7k
    if (UNEXPECTED(failed)) {
1840
280
      zend_binop_error("&", op1, op2);
1841
280
      if (result != op1) {
1842
243
        ZVAL_UNDEF(result);
1843
243
      }
1844
280
      return FAILURE;
1845
280
    }
1846
26.7k
  } else {
1847
4.80k
    op2_lval = Z_LVAL_P(op2);
1848
4.80k
  }
1849
1850
31.2k
  if (op1 == result) {
1851
21.3k
    zval_ptr_dtor(result);
1852
21.3k
  }
1853
31.2k
  ZVAL_LONG(result, op1_lval & op2_lval);
1854
31.2k
  return SUCCESS;
1855
31.5k
}
1856
/* }}} */
1857
1858
ZEND_API zend_result ZEND_FASTCALL bitwise_xor_function(zval *result, zval *op1, zval *op2) /* {{{ */
1859
108k
{
1860
108k
  zend_long op1_lval, op2_lval;
1861
1862
108k
  if (EXPECTED(Z_TYPE_P(op1) == IS_LONG) && EXPECTED(Z_TYPE_P(op2) == IS_LONG)) {
1863
1.87k
    ZVAL_LONG(result, Z_LVAL_P(op1) ^ Z_LVAL_P(op2));
1864
1.87k
    return SUCCESS;
1865
1.87k
  }
1866
1867
106k
  ZVAL_DEREF(op1);
1868
106k
  ZVAL_DEREF(op2);
1869
1870
106k
  if (Z_TYPE_P(op1) == IS_STRING && Z_TYPE_P(op2) == IS_STRING) {
1871
68.7k
    zval *longer, *shorter;
1872
68.7k
    zend_string *str;
1873
68.7k
    size_t i;
1874
1875
68.7k
    if (EXPECTED(Z_STRLEN_P(op1) >= Z_STRLEN_P(op2))) {
1876
57.9k
      if (EXPECTED(Z_STRLEN_P(op1) == Z_STRLEN_P(op2)) && Z_STRLEN_P(op1) == 1) {
1877
30
        zend_uchar xor = (zend_uchar) (*Z_STRVAL_P(op1) ^ *Z_STRVAL_P(op2));
1878
30
        if (result==op1) {
1879
8
          zval_ptr_dtor_str(result);
1880
8
        }
1881
30
        ZVAL_CHAR(result, xor);
1882
30
        return SUCCESS;
1883
30
      }
1884
57.9k
      longer = op1;
1885
57.9k
      shorter = op2;
1886
57.9k
    } else {
1887
10.7k
      longer = op2;
1888
10.7k
      shorter = op1;
1889
10.7k
    }
1890
1891
68.7k
    str = zend_string_alloc(Z_STRLEN_P(shorter), 0);
1892
279M
    for (i = 0; i < Z_STRLEN_P(shorter); i++) {
1893
279M
      ZSTR_VAL(str)[i] = Z_STRVAL_P(shorter)[i] ^ Z_STRVAL_P(longer)[i];
1894
279M
    }
1895
68.7k
    ZSTR_VAL(str)[i] = 0;
1896
68.7k
    if (result==op1) {
1897
8
      zval_ptr_dtor_str(result);
1898
8
    }
1899
68.7k
    ZVAL_NEW_STR(result, str);
1900
68.7k
    return SUCCESS;
1901
68.7k
  }
1902
1903
37.4k
  if (UNEXPECTED(Z_TYPE_P(op1) != IS_LONG)) {
1904
3.34k
    bool failed;
1905
3.34k
    ZEND_TRY_BINARY_OP1_OBJECT_OPERATION(ZEND_BW_XOR);
1906
3.34k
    op1_lval = zendi_try_get_long(op1, &failed);
1907
3.34k
    if (UNEXPECTED(failed)) {
1908
41
      zend_binop_error("^", op1, op2);
1909
41
      if (result != op1) {
1910
23
        ZVAL_UNDEF(result);
1911
23
      }
1912
41
      return FAILURE;
1913
41
    }
1914
34.1k
  } else {
1915
34.1k
    op1_lval = Z_LVAL_P(op1);
1916
34.1k
  }
1917
37.4k
  if (UNEXPECTED(Z_TYPE_P(op2) != IS_LONG)) {
1918
35.8k
    bool failed;
1919
35.8k
    ZEND_TRY_BINARY_OP2_OBJECT_OPERATION(ZEND_BW_XOR);
1920
35.8k
    op2_lval = zendi_try_get_long(op2, &failed);
1921
35.8k
    if (UNEXPECTED(failed)) {
1922
329
      zend_binop_error("^", op1, op2);
1923
329
      if (result != op1) {
1924
322
        ZVAL_UNDEF(result);
1925
322
      }
1926
329
      return FAILURE;
1927
329
    }
1928
35.8k
  } else {
1929
1.57k
    op2_lval = Z_LVAL_P(op2);
1930
1.57k
  }
1931
1932
37.1k
  if (op1 == result) {
1933
44
    zval_ptr_dtor(result);
1934
44
  }
1935
37.1k
  ZVAL_LONG(result, op1_lval ^ op2_lval);
1936
37.1k
  return SUCCESS;
1937
37.4k
}
1938
/* }}} */
1939
1940
ZEND_API zend_result ZEND_FASTCALL shift_left_function(zval *result, zval *op1, zval *op2) /* {{{ */
1941
6.85k
{
1942
6.85k
  zend_long op1_lval, op2_lval;
1943
1944
6.85k
  convert_op1_op2_long(op1, op1_lval, op2, op2_lval, result, ZEND_SL, "<<");
1945
1946
  /* prevent wrapping quirkiness on some processors where << 64 + x == << x */
1947
6.28k
  if (UNEXPECTED((zend_ulong)op2_lval >= SIZEOF_ZEND_LONG * 8)) {
1948
786
    if (EXPECTED(op2_lval > 0)) {
1949
737
      if (op1 == result) {
1950
5
        zval_ptr_dtor(result);
1951
5
      }
1952
737
      ZVAL_LONG(result, 0);
1953
737
      return SUCCESS;
1954
737
    } else {
1955
49
      if (EG(current_execute_data) && !CG(in_compilation)) {
1956
49
        zend_throw_exception_ex(zend_ce_arithmetic_error, 0, "Bit shift by negative number");
1957
49
      } else {
1958
0
        zend_error_noreturn(E_ERROR, "Bit shift by negative number");
1959
0
      }
1960
49
      if (op1 != result) {
1961
31
        ZVAL_UNDEF(result);
1962
31
      }
1963
49
      return FAILURE;
1964
49
    }
1965
786
  }
1966
1967
5.50k
  if (op1 == result) {
1968
69
    zval_ptr_dtor(result);
1969
69
  }
1970
1971
  /* Perform shift on unsigned numbers to get well-defined wrap behavior. */
1972
5.50k
  ZVAL_LONG(result, (zend_long) ((zend_ulong) op1_lval << op2_lval));
1973
5.50k
  return SUCCESS;
1974
6.28k
}
1975
/* }}} */
1976
1977
ZEND_API zend_result ZEND_FASTCALL shift_right_function(zval *result, zval *op1, zval *op2) /* {{{ */
1978
13.1k
{
1979
13.1k
  zend_long op1_lval, op2_lval;
1980
1981
13.1k
  convert_op1_op2_long(op1, op1_lval, op2, op2_lval, result, ZEND_SR, ">>");
1982
1983
  /* prevent wrapping quirkiness on some processors where >> 64 + x == >> x */
1984
13.0k
  if (UNEXPECTED((zend_ulong)op2_lval >= SIZEOF_ZEND_LONG * 8)) {
1985
2.57k
    if (EXPECTED(op2_lval > 0)) {
1986
2.37k
      if (op1 == result) {
1987
8
        zval_ptr_dtor(result);
1988
8
      }
1989
2.37k
      ZVAL_LONG(result, (op1_lval < 0) ? -1 : 0);
1990
2.37k
      return SUCCESS;
1991
2.37k
    } else {
1992
195
      if (EG(current_execute_data) && !CG(in_compilation)) {
1993
195
        zend_throw_exception_ex(zend_ce_arithmetic_error, 0, "Bit shift by negative number");
1994
195
      } else {
1995
0
        zend_error_noreturn(E_ERROR, "Bit shift by negative number");
1996
0
      }
1997
195
      if (op1 != result) {
1998
179
        ZVAL_UNDEF(result);
1999
179
      }
2000
195
      return FAILURE;
2001
195
    }
2002
2.57k
  }
2003
2004
10.4k
  if (op1 == result) {
2005
5.41k
    zval_ptr_dtor(result);
2006
5.41k
  }
2007
2008
10.4k
  ZVAL_LONG(result, op1_lval >> op2_lval);
2009
10.4k
  return SUCCESS;
2010
13.0k
}
2011
/* }}} */
2012
2013
ZEND_API zend_result ZEND_FASTCALL concat_function(zval *result, zval *op1, zval *op2) /* {{{ */
2014
1.28M
{
2015
1.28M
  zval *orig_op1 = op1;
2016
1.28M
  zend_string *op1_string, *op2_string;
2017
1.28M
  bool free_op1_string = false;
2018
1.28M
  bool free_op2_string = false;
2019
2020
1.28M
  do {
2021
1.28M
    if (EXPECTED(Z_TYPE_P(op1) == IS_STRING)) {
2022
1.15M
      op1_string = Z_STR_P(op1);
2023
1.15M
    } else {
2024
132k
      if (Z_ISREF_P(op1)) {
2025
1.94k
        op1 = Z_REFVAL_P(op1);
2026
1.94k
        if (Z_TYPE_P(op1) == IS_STRING) {
2027
517
          op1_string = Z_STR_P(op1);
2028
517
          break;
2029
517
        }
2030
1.94k
      }
2031
132k
      ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_CONCAT);
2032
132k
      op1_string = zval_try_get_string_func(op1);
2033
132k
      if (UNEXPECTED(!op1_string)) {
2034
70
        if (orig_op1 != result) {
2035
30
          ZVAL_UNDEF(result);
2036
30
        }
2037
70
        return FAILURE;
2038
70
      }
2039
131k
      free_op1_string = true;
2040
131k
      if (result == op1) {
2041
101k
        if (UNEXPECTED(op1 == op2)) {
2042
74
          op2_string = op1_string;
2043
74
          goto has_op2_string;
2044
74
        }
2045
101k
      }
2046
131k
    }
2047
1.28M
  } while (0);
2048
1.28M
  do {
2049
1.28M
    if (EXPECTED(Z_TYPE_P(op2) == IS_STRING)) {
2050
1.20M
      op2_string = Z_STR_P(op2);
2051
1.20M
    } else {
2052
81.3k
      if (Z_ISREF_P(op2)) {
2053
250
        op2 = Z_REFVAL_P(op2);
2054
250
        if (Z_TYPE_P(op2) == IS_STRING) {
2055
102
          op2_string = Z_STR_P(op2);
2056
102
          break;
2057
102
        }
2058
250
      }
2059
      /* hold an additional reference because a userland function could free this */
2060
81.2k
      if (!free_op1_string) {
2061
49.9k
        op1_string = zend_string_copy(op1_string);
2062
49.9k
        free_op1_string = true;
2063
49.9k
      }
2064
81.2k
      ZEND_TRY_BINARY_OP2_OBJECT_OPERATION(ZEND_CONCAT);
2065
81.2k
      op2_string = zval_try_get_string_func(op2);
2066
81.2k
      if (UNEXPECTED(!op2_string)) {
2067
81
        zend_string_release_ex(op1_string, false);
2068
81
        if (orig_op1 != result) {
2069
40
          ZVAL_UNDEF(result);
2070
40
        }
2071
81
        return FAILURE;
2072
81
      }
2073
81.1k
      free_op2_string = true;
2074
81.1k
    }
2075
1.28M
  } while (0);
2076
2077
1.28M
has_op2_string:;
2078
1.28M
  if (UNEXPECTED(ZSTR_LEN(op1_string) == 0)) {
2079
130k
    if (EXPECTED(result != op2 || Z_TYPE_P(result) != IS_STRING)) {
2080
130k
      if (result == orig_op1) {
2081
118k
        i_zval_ptr_dtor(result);
2082
118k
      }
2083
130k
      if (free_op2_string) {
2084
        /* transfer ownership of op2_string */
2085
32.8k
        ZVAL_STR(result, op2_string);
2086
32.8k
        free_op2_string = false;
2087
97.9k
      } else {
2088
97.9k
        ZVAL_STR_COPY(result, op2_string);
2089
97.9k
      }
2090
130k
    }
2091
1.15M
  } else if (UNEXPECTED(ZSTR_LEN(op2_string) == 0)) {
2092
25.4k
    if (EXPECTED(result != op1 || Z_TYPE_P(result) != IS_STRING)) {
2093
7.52k
      if (result == orig_op1) {
2094
845
        i_zval_ptr_dtor(result);
2095
845
      }
2096
7.52k
      if (free_op1_string) {
2097
        /* transfer ownership of op1_string */
2098
7.23k
        ZVAL_STR(result, op1_string);
2099
7.23k
        free_op1_string = false;
2100
7.23k
      } else {
2101
284
        ZVAL_STR_COPY(result, op1_string);
2102
284
      }
2103
7.52k
    }
2104
1.13M
  } else {
2105
1.13M
    size_t op1_len = ZSTR_LEN(op1_string);
2106
1.13M
    size_t op2_len = ZSTR_LEN(op2_string);
2107
1.13M
    size_t result_len = op1_len + op2_len;
2108
1.13M
    zend_string *result_str;
2109
1.13M
    uint32_t flags = ZSTR_GET_COPYABLE_CONCAT_PROPERTIES_BOTH(op1_string, op2_string);
2110
2111
1.13M
    if (UNEXPECTED(op1_len > ZSTR_MAX_LEN - op2_len)) {
2112
0
      if (free_op1_string) zend_string_release_ex(op1_string, false);
2113
0
      if (free_op2_string) zend_string_release_ex(op2_string, false);
2114
0
      zend_throw_error(NULL, "String size overflow");
2115
0
      if (orig_op1 != result) {
2116
0
        ZVAL_UNDEF(result);
2117
0
      }
2118
0
      return FAILURE;
2119
0
    }
2120
2121
1.13M
    if (result == op1) {
2122
      /* Destroy the old result first to drop the refcount, such that $x .= ...; may happen in-place. */
2123
1.10M
      if (free_op1_string) {
2124
        /* op1_string will be used as the result, so we should not free it */
2125
24.9k
        i_zval_ptr_dtor(result);
2126
        /* Set it to NULL in case that the extension will throw an out-of-memory error.
2127
         * Otherwise the shutdown sequence will try to free this again. */
2128
24.9k
        ZVAL_NULL(result);
2129
24.9k
        free_op1_string = false;
2130
24.9k
      }
2131
      /* special case, perform operations on result */
2132
1.10M
      result_str = zend_string_extend(op1_string, result_len, 0);
2133
      /* account for the case where result_str == op1_string == op2_string and the realloc is done */
2134
1.10M
      if (op1_string == op2_string) {
2135
35.5k
        if (free_op2_string) {
2136
3.05k
          zend_string_release_ex(op2_string, false);
2137
3.05k
          free_op2_string = false;
2138
3.05k
        }
2139
35.5k
        op2_string = result_str;
2140
35.5k
      }
2141
1.10M
    } else {
2142
26.1k
      result_str = zend_string_alloc(result_len, 0);
2143
26.1k
      memcpy(ZSTR_VAL(result_str), ZSTR_VAL(op1_string), op1_len);
2144
26.1k
      if (result == orig_op1) {
2145
0
        i_zval_ptr_dtor(result);
2146
0
      }
2147
26.1k
    }
2148
1.13M
    GC_ADD_FLAGS(result_str, flags);
2149
2150
1.13M
    ZVAL_NEW_STR(result, result_str);
2151
1.13M
    memcpy(ZSTR_VAL(result_str) + op1_len, ZSTR_VAL(op2_string), op2_len);
2152
1.13M
    ZSTR_VAL(result_str)[result_len] = '\0';
2153
1.13M
  }
2154
2155
1.28M
  if (free_op1_string) zend_string_release_ex(op1_string, false);
2156
1.28M
  if (free_op2_string) zend_string_release_ex(op2_string, false);
2157
2158
1.28M
  return SUCCESS;
2159
1.28M
}
2160
/* }}} */
2161
2162
ZEND_API int ZEND_FASTCALL string_compare_function_ex(const zval *op1, const zval *op2, bool case_insensitive) /* {{{ */
2163
0
{
2164
0
  zend_string *tmp_str1, *tmp_str2;
2165
0
  zend_string *str1 = zval_get_tmp_string(op1, &tmp_str1);
2166
0
  zend_string *str2 = zval_get_tmp_string(op2, &tmp_str2);
2167
0
  int ret;
2168
2169
0
  if (case_insensitive) {
2170
0
    ret = zend_binary_strcasecmp(ZSTR_VAL(str1), ZSTR_LEN(str1), ZSTR_VAL(str2), ZSTR_LEN(str2));
2171
0
  } else {
2172
0
    ret = zend_binary_strcmp(ZSTR_VAL(str1), ZSTR_LEN(str1), ZSTR_VAL(str2), ZSTR_LEN(str2));
2173
0
  }
2174
2175
0
  zend_tmp_string_release(tmp_str1);
2176
0
  zend_tmp_string_release(tmp_str2);
2177
0
  return ret;
2178
0
}
2179
/* }}} */
2180
2181
ZEND_API int ZEND_FASTCALL string_compare_function(const zval *op1, const zval *op2) /* {{{ */
2182
84.7k
{
2183
84.7k
  if (EXPECTED(Z_TYPE_P(op1) == IS_STRING) &&
2184
40.5k
      EXPECTED(Z_TYPE_P(op2) == IS_STRING)) {
2185
39.0k
    if (Z_STR_P(op1) == Z_STR_P(op2)) {
2186
4.57k
      return 0;
2187
34.5k
    } else {
2188
34.5k
      return zend_binary_strcmp(Z_STRVAL_P(op1), Z_STRLEN_P(op1), Z_STRVAL_P(op2), Z_STRLEN_P(op2));
2189
34.5k
    }
2190
45.6k
  } else {
2191
45.6k
    zend_string *tmp_str1, *tmp_str2;
2192
45.6k
    zend_string *str1 = zval_get_tmp_string(op1, &tmp_str1);
2193
45.6k
    zend_string *str2 = zval_get_tmp_string(op2, &tmp_str2);
2194
45.6k
    int ret = zend_binary_strcmp(ZSTR_VAL(str1), ZSTR_LEN(str1), ZSTR_VAL(str2), ZSTR_LEN(str2));
2195
2196
45.6k
    zend_tmp_string_release(tmp_str1);
2197
45.6k
    zend_tmp_string_release(tmp_str2);
2198
45.6k
    return ret;
2199
45.6k
  }
2200
84.7k
}
2201
/* }}} */
2202
2203
ZEND_API int ZEND_FASTCALL string_case_compare_function(const zval *op1, const zval *op2) /* {{{ */
2204
65
{
2205
65
  if (EXPECTED(Z_TYPE_P(op1) == IS_STRING) &&
2206
63
      EXPECTED(Z_TYPE_P(op2) == IS_STRING)) {
2207
42
    if (Z_STR_P(op1) == Z_STR_P(op2)) {
2208
0
      return 0;
2209
42
    } else {
2210
42
      return zend_binary_strcasecmp(Z_STRVAL_P(op1), Z_STRLEN_P(op1), Z_STRVAL_P(op2), Z_STRLEN_P(op2));
2211
42
    }
2212
42
  } else {
2213
23
    zend_string *tmp_str1, *tmp_str2;
2214
23
    zend_string *str1 = zval_get_tmp_string(op1, &tmp_str1);
2215
23
    zend_string *str2 = zval_get_tmp_string(op2, &tmp_str2);
2216
23
    int ret = zend_binary_strcasecmp(ZSTR_VAL(str1), ZSTR_LEN(str1), ZSTR_VAL(str2), ZSTR_LEN(str2));
2217
2218
23
    zend_tmp_string_release(tmp_str1);
2219
23
    zend_tmp_string_release(tmp_str2);
2220
23
    return ret;
2221
23
  }
2222
65
}
2223
/* }}} */
2224
2225
ZEND_API int ZEND_FASTCALL string_locale_compare_function(const zval *op1, const zval *op2) /* {{{ */
2226
4.37k
{
2227
4.37k
  zend_string *tmp_str1, *tmp_str2;
2228
4.37k
  zend_string *str1 = zval_get_tmp_string(op1, &tmp_str1);
2229
4.37k
  zend_string *str2 = zval_get_tmp_string(op2, &tmp_str2);
2230
4.37k
  int ret = strcoll(ZSTR_VAL(str1), ZSTR_VAL(str2));
2231
2232
4.37k
  zend_tmp_string_release(tmp_str1);
2233
4.37k
  zend_tmp_string_release(tmp_str2);
2234
4.37k
  return ret;
2235
4.37k
}
2236
/* }}} */
2237
2238
ZEND_API int ZEND_FASTCALL numeric_compare_function(const zval *op1, const zval *op2) /* {{{ */
2239
13.9k
{
2240
13.9k
  double d1, d2;
2241
2242
13.9k
  d1 = zval_get_double(op1);
2243
13.9k
  d2 = zval_get_double(op2);
2244
2245
13.9k
  return ZEND_THREEWAY_COMPARE(d1, d2);
2246
13.9k
}
2247
/* }}} */
2248
2249
ZEND_API zend_result ZEND_FASTCALL compare_function(zval *result, zval *op1, zval *op2) /* {{{ */
2250
678
{
2251
678
  ZVAL_LONG(result, zend_compare(op1, op2));
2252
678
  return SUCCESS;
2253
678
}
2254
/* }}} */
2255
2256
static int compare_long_to_string(zend_long lval, const zend_string *str) /* {{{ */
2257
151k
{
2258
151k
  zend_long str_lval;
2259
151k
  double str_dval;
2260
151k
  uint8_t type = is_numeric_string(ZSTR_VAL(str), ZSTR_LEN(str), &str_lval, &str_dval, 0);
2261
2262
151k
  if (type == IS_LONG) {
2263
116k
    return lval > str_lval ? 1 : lval < str_lval ? -1 : 0;
2264
116k
  }
2265
2266
35.2k
  if (type == IS_DOUBLE) {
2267
24.1k
    return ZEND_THREEWAY_COMPARE((double) lval, str_dval);
2268
24.1k
  }
2269
2270
11.0k
  zend_string *lval_as_str = zend_long_to_str(lval);
2271
11.0k
  int cmp_result = zend_binary_strcmp(
2272
11.0k
    ZSTR_VAL(lval_as_str), ZSTR_LEN(lval_as_str), ZSTR_VAL(str), ZSTR_LEN(str));
2273
11.0k
  zend_string_release(lval_as_str);
2274
11.0k
  return ZEND_NORMALIZE_BOOL(cmp_result);
2275
35.2k
}
2276
/* }}} */
2277
2278
static int compare_double_to_string(double dval, const zend_string *str) /* {{{ */
2279
4.76k
{
2280
4.76k
  zend_long str_lval;
2281
4.76k
  double str_dval;
2282
4.76k
  uint8_t type = is_numeric_string(ZSTR_VAL(str), ZSTR_LEN(str), &str_lval, &str_dval, 0);
2283
2284
4.76k
  ZEND_ASSERT(!zend_isnan(dval));
2285
2286
4.76k
  if (type == IS_LONG) {
2287
1.08k
    return ZEND_THREEWAY_COMPARE(dval, (double) str_lval);
2288
1.08k
  }
2289
2290
3.68k
  if (type == IS_DOUBLE) {
2291
925
    return ZEND_THREEWAY_COMPARE(dval, str_dval);
2292
925
  }
2293
2294
2.75k
  zend_string *dval_as_str = zend_double_to_str(dval);
2295
2.75k
  int cmp_result = zend_binary_strcmp(
2296
2.75k
    ZSTR_VAL(dval_as_str), ZSTR_LEN(dval_as_str), ZSTR_VAL(str), ZSTR_LEN(str));
2297
2.75k
  zend_string_release(dval_as_str);
2298
2.75k
  return ZEND_NORMALIZE_BOOL(cmp_result);
2299
3.68k
}
2300
/* }}} */
2301
2302
ZEND_API int ZEND_FASTCALL zend_compare(zval *op1, zval *op2) /* {{{ */
2303
888k
{
2304
888k
  bool converted = false;
2305
888k
  zval op1_copy, op2_copy;
2306
2307
919k
  while (1) {
2308
919k
    switch (TYPE_PAIR(Z_TYPE_P(op1), Z_TYPE_P(op2))) {
2309
210k
      case TYPE_PAIR(IS_LONG, IS_LONG):
2310
210k
        return Z_LVAL_P(op1)>Z_LVAL_P(op2)?1:(Z_LVAL_P(op1)<Z_LVAL_P(op2)?-1:0);
2311
2312
3.18k
      case TYPE_PAIR(IS_DOUBLE, IS_LONG):
2313
3.18k
        return ZEND_THREEWAY_COMPARE(Z_DVAL_P(op1), (double)Z_LVAL_P(op2));
2314
2315
2.28k
      case TYPE_PAIR(IS_LONG, IS_DOUBLE):
2316
2.28k
        return ZEND_THREEWAY_COMPARE((double)Z_LVAL_P(op1), Z_DVAL_P(op2));
2317
2318
1.62k
      case TYPE_PAIR(IS_DOUBLE, IS_DOUBLE):
2319
1.62k
        return ZEND_THREEWAY_COMPARE(Z_DVAL_P(op1), Z_DVAL_P(op2));
2320
2321
9.83k
      case TYPE_PAIR(IS_ARRAY, IS_ARRAY):
2322
9.83k
        return zend_compare_arrays(op1, op2);
2323
2324
5.91k
      case TYPE_PAIR(IS_NULL, IS_NULL):
2325
8.60k
      case TYPE_PAIR(IS_NULL, IS_FALSE):
2326
28.2k
      case TYPE_PAIR(IS_FALSE, IS_NULL):
2327
29.2k
      case TYPE_PAIR(IS_FALSE, IS_FALSE):
2328
30.1k
      case TYPE_PAIR(IS_TRUE, IS_TRUE):
2329
30.1k
        return 0;
2330
2331
3.39k
      case TYPE_PAIR(IS_NULL, IS_TRUE):
2332
3.39k
        return -1;
2333
2334
1.02k
      case TYPE_PAIR(IS_TRUE, IS_NULL):
2335
1.02k
        return 1;
2336
2337
22.3k
      case TYPE_PAIR(IS_STRING, IS_STRING):
2338
22.3k
        if (Z_STR_P(op1) == Z_STR_P(op2)) {
2339
2.18k
          return 0;
2340
2.18k
        }
2341
20.1k
        return zendi_smart_strcmp(Z_STR_P(op1), Z_STR_P(op2));
2342
2343
11.7k
      case TYPE_PAIR(IS_NULL, IS_STRING):
2344
11.7k
        return Z_STRLEN_P(op2) == 0 ? 0 : -1;
2345
2346
19.7k
      case TYPE_PAIR(IS_STRING, IS_NULL):
2347
19.7k
        return Z_STRLEN_P(op1) == 0 ? 0 : 1;
2348
2349
9.83k
      case TYPE_PAIR(IS_LONG, IS_STRING):
2350
9.83k
        return compare_long_to_string(Z_LVAL_P(op1), Z_STR_P(op2));
2351
2352
142k
      case TYPE_PAIR(IS_STRING, IS_LONG):
2353
142k
        return -compare_long_to_string(Z_LVAL_P(op2), Z_STR_P(op1));
2354
2355
3.12k
      case TYPE_PAIR(IS_DOUBLE, IS_STRING):
2356
3.12k
        if (zend_isnan(Z_DVAL_P(op1))) {
2357
128
          return 1;
2358
128
        }
2359
2360
3.00k
        return compare_double_to_string(Z_DVAL_P(op1), Z_STR_P(op2));
2361
2362
1.85k
      case TYPE_PAIR(IS_STRING, IS_DOUBLE):
2363
1.85k
        if (zend_isnan(Z_DVAL_P(op2))) {
2364
92
          return 1;
2365
92
        }
2366
2367
1.76k
        return -compare_double_to_string(Z_DVAL_P(op2), Z_STR_P(op1));
2368
2369
685
      case TYPE_PAIR(IS_OBJECT, IS_NULL):
2370
685
        return 1;
2371
2372
1.92k
      case TYPE_PAIR(IS_NULL, IS_OBJECT):
2373
1.92k
        return -1;
2374
2375
444k
      default:
2376
444k
        if (Z_ISREF_P(op1)) {
2377
5.74k
          op1 = Z_REFVAL_P(op1);
2378
5.74k
          continue;
2379
438k
        } else if (Z_ISREF_P(op2)) {
2380
3.95k
          op2 = Z_REFVAL_P(op2);
2381
3.95k
          continue;
2382
3.95k
        }
2383
2384
434k
        if (Z_TYPE_P(op1) == IS_OBJECT
2385
432k
         || Z_TYPE_P(op2) == IS_OBJECT) {
2386
2.37k
          zval *object, *other;
2387
2.37k
          if (Z_TYPE_P(op1) == IS_OBJECT) {
2388
2.07k
            object = op1;
2389
2.07k
            other = op2;
2390
2.07k
          } else {
2391
299
            object = op2;
2392
299
            other = op1;
2393
299
          }
2394
2.37k
          if (EXPECTED(Z_TYPE_P(other) == IS_OBJECT)) {
2395
1.16k
            if (Z_OBJ_P(object) == Z_OBJ_P(other)) {
2396
178
              return 0;
2397
178
            }
2398
1.20k
          } else if (Z_TYPE_P(other) == IS_TRUE || Z_TYPE_P(other) == IS_FALSE) {
2399
585
            zval casted;
2400
585
            if (Z_OBJ_HANDLER_P(object, cast_object)(Z_OBJ_P(object), &casted, _IS_BOOL) == FAILURE) {
2401
0
              return object == op1 ? 1 : -1;
2402
0
            }
2403
585
            int ret = object == op1 ? zend_compare(&casted, other) : zend_compare(other, &casted);
2404
585
            ZEND_ASSERT(!Z_REFCOUNTED_P(&casted));
2405
585
            return ret;
2406
585
          }
2407
1.60k
          return Z_OBJ_HANDLER_P(object, compare)(op1, op2);
2408
2.37k
        }
2409
2410
432k
        if (!converted) {
2411
          /* Handle NAN */
2412
410k
          if (UNEXPECTED(
2413
410k
            (Z_TYPE_P(op1) == IS_DOUBLE && zend_isnan(Z_DVAL_P(op1)))
2414
410k
            || (Z_TYPE_P(op2) == IS_DOUBLE && zend_isnan(Z_DVAL_P(op2)))
2415
410k
          )) {
2416
            // TODO: NAN should always be uncomparable
2417
            /* NAN used be cast to TRUE so handle this manually for the time being */
2418
762
            if (Z_TYPE_P(op1) < IS_TRUE) {
2419
191
              return -1;
2420
571
            } else if (Z_TYPE_P(op1) == IS_TRUE || Z_TYPE_P(op2) == IS_TRUE) {
2421
184
              return 0;
2422
387
            } else if (Z_TYPE_P(op2) < IS_TRUE) {
2423
279
              return 1;
2424
279
            } else if (Z_TYPE_P(op1) != IS_DOUBLE) {
2425
32
              op1 = _zendi_convert_scalar_to_number_silent(op1, &op1_copy);
2426
32
              converted = true;
2427
76
            } else if (Z_TYPE_P(op2) != IS_DOUBLE) {
2428
76
              op2 = _zendi_convert_scalar_to_number_silent(op2, &op2_copy);
2429
76
              converted = true;
2430
76
            }
2431
409k
          } else if (Z_TYPE_P(op1) < IS_TRUE) {
2432
231k
            return zend_is_true(op2) ? -1 : 0;
2433
231k
          } else if (Z_TYPE_P(op1) == IS_TRUE) {
2434
3.23k
            return zend_is_true(op2) ? 0 : 1;
2435
175k
          } else if (Z_TYPE_P(op2) < IS_TRUE) {
2436
20.8k
            return zend_is_true(op1) ? 1 : 0;
2437
154k
          } else if (Z_TYPE_P(op2) == IS_TRUE) {
2438
133k
            return zend_is_true(op1) ? 0 : -1;
2439
133k
          } else {
2440
21.4k
            op1 = _zendi_convert_scalar_to_number_silent(op1, &op1_copy);
2441
21.4k
            op2 = _zendi_convert_scalar_to_number_silent(op2, &op2_copy);
2442
21.4k
            if (EG(exception)) {
2443
0
              return 1; /* to stop comparison of arrays */
2444
0
            }
2445
21.4k
            converted = true;
2446
21.4k
          }
2447
410k
        } else if (Z_TYPE_P(op1)==IS_ARRAY) {
2448
8.22k
          return 1;
2449
13.3k
        } else if (Z_TYPE_P(op2)==IS_ARRAY) {
2450
13.3k
          return -1;
2451
13.3k
        } else {
2452
0
          ZEND_UNREACHABLE();
2453
0
          zend_throw_error(NULL, "Unsupported operand types");
2454
0
          return 1;
2455
0
        }
2456
919k
    }
2457
919k
  }
2458
888k
}
2459
/* }}} */
2460
2461
/* return int to be compatible with compare_func_t */
2462
static int hash_zval_identical_function(const zval *z1, const zval *z2) /* {{{ */
2463
3.21k
{
2464
  /* is_identical_function() returns 1 in case of identity and 0 in case
2465
   * of a difference;
2466
   * whereas this comparison function is expected to return 0 on identity,
2467
   * and non zero otherwise.
2468
   */
2469
3.21k
  ZVAL_DEREF(z1);
2470
3.21k
  ZVAL_DEREF(z2);
2471
3.21k
  return fast_is_not_identical_function(z1, z2);
2472
3.21k
}
2473
/* }}} */
2474
2475
ZEND_API bool ZEND_FASTCALL zend_is_identical(const zval *op1, const zval *op2) /* {{{ */
2476
208k
{
2477
208k
  if (Z_TYPE_P(op1) != Z_TYPE_P(op2)) {
2478
30.2k
    return 0;
2479
30.2k
  }
2480
178k
  switch (Z_TYPE_P(op1)) {
2481
189
    case IS_NULL:
2482
297
    case IS_FALSE:
2483
117k
    case IS_TRUE:
2484
117k
      return 1;
2485
15.0k
    case IS_LONG:
2486
15.0k
      return (Z_LVAL_P(op1) == Z_LVAL_P(op2));
2487
0
    case IS_RESOURCE:
2488
0
      return (Z_RES_P(op1) == Z_RES_P(op2));
2489
1.12k
    case IS_DOUBLE:
2490
1.12k
      return (Z_DVAL_P(op1) == Z_DVAL_P(op2));
2491
39.5k
    case IS_STRING:
2492
39.5k
      return zend_string_equals(Z_STR_P(op1), Z_STR_P(op2));
2493
4.65k
    case IS_ARRAY:
2494
4.65k
      return (Z_ARRVAL_P(op1) == Z_ARRVAL_P(op2) ||
2495
4.37k
        zend_hash_compare(Z_ARRVAL_P(op1), Z_ARRVAL_P(op2), (compare_func_t) hash_zval_identical_function, 1) == 0);
2496
576
    case IS_OBJECT:
2497
576
      return (Z_OBJ_P(op1) == Z_OBJ_P(op2));
2498
0
    default:
2499
0
      return 0;
2500
178k
  }
2501
178k
}
2502
/* }}} */
2503
2504
ZEND_API zend_result ZEND_FASTCALL is_identical_function(zval *result, const zval *op1, const zval *op2) /* {{{ */
2505
55.7k
{
2506
55.7k
  ZVAL_BOOL(result, zend_is_identical(op1, op2));
2507
55.7k
  return SUCCESS;
2508
55.7k
}
2509
/* }}} */
2510
2511
ZEND_API zend_result ZEND_FASTCALL is_not_identical_function(zval *result, const zval *op1, const zval *op2) /* {{{ */
2512
96.9k
{
2513
96.9k
  ZVAL_BOOL(result, !zend_is_identical(op1, op2));
2514
96.9k
  return SUCCESS;
2515
96.9k
}
2516
/* }}} */
2517
2518
ZEND_API zend_result ZEND_FASTCALL is_equal_function(zval *result, zval *op1, zval *op2) /* {{{ */
2519
6.90k
{
2520
6.90k
  ZVAL_BOOL(result, zend_compare(op1, op2) == 0);
2521
6.90k
  return SUCCESS;
2522
6.90k
}
2523
/* }}} */
2524
2525
ZEND_API zend_result ZEND_FASTCALL is_not_equal_function(zval *result, zval *op1, zval *op2) /* {{{ */
2526
2.27k
{
2527
2.27k
  ZVAL_BOOL(result, (zend_compare(op1, op2) != 0));
2528
2.27k
  return SUCCESS;
2529
2.27k
}
2530
/* }}} */
2531
2532
ZEND_API zend_result ZEND_FASTCALL is_smaller_function(zval *result, zval *op1, zval *op2) /* {{{ */
2533
338k
{
2534
338k
  ZVAL_BOOL(result, (zend_compare(op1, op2) < 0));
2535
338k
  return SUCCESS;
2536
338k
}
2537
/* }}} */
2538
2539
ZEND_API zend_result ZEND_FASTCALL is_smaller_or_equal_function(zval *result, zval *op1, zval *op2) /* {{{ */
2540
91.6k
{
2541
91.6k
  ZVAL_BOOL(result, (zend_compare(op1, op2) <= 0));
2542
91.6k
  return SUCCESS;
2543
91.6k
}
2544
/* }}} */
2545
2546
ZEND_API bool ZEND_FASTCALL zend_class_implements_interface(const zend_class_entry *class_ce, const zend_class_entry *interface_ce) /* {{{ */
2547
4.89k
{
2548
4.89k
  uint32_t i;
2549
4.89k
  ZEND_ASSERT(interface_ce->ce_flags & ZEND_ACC_INTERFACE);
2550
2551
4.89k
  if (class_ce->num_interfaces) {
2552
4.84k
    ZEND_ASSERT(class_ce->ce_flags & ZEND_ACC_RESOLVED_INTERFACES);
2553
13.8k
    for (i = 0; i < class_ce->num_interfaces; i++) {
2554
10.8k
      if (class_ce->interfaces[i] == interface_ce) {
2555
1.83k
        return 1;
2556
1.83k
      }
2557
10.8k
    }
2558
4.84k
  }
2559
3.05k
  return 0;
2560
4.89k
}
2561
/* }}} */
2562
2563
ZEND_API bool ZEND_FASTCALL instanceof_function_slow(const zend_class_entry *instance_ce, const zend_class_entry *ce) /* {{{ */
2564
30.5M
{
2565
30.5M
  ZEND_ASSERT(instance_ce != ce && "Should have been checked already");
2566
30.5M
  if (ce->ce_flags & ZEND_ACC_INTERFACE) {
2567
1.21M
    uint32_t i;
2568
2569
1.21M
    if (instance_ce->num_interfaces) {
2570
1.21M
      ZEND_ASSERT(instance_ce->ce_flags & ZEND_ACC_RESOLVED_INTERFACES);
2571
2.41M
      for (i = 0; i < instance_ce->num_interfaces; i++) {
2572
2.41M
        if (instance_ce->interfaces[i] == ce) {
2573
1.21M
          return 1;
2574
1.21M
        }
2575
2.41M
      }
2576
1.21M
    }
2577
1.95k
    return 0;
2578
29.2M
  } else {
2579
84.2M
    while (1) {
2580
84.2M
      instance_ce = instance_ce->parent;
2581
84.2M
      if (instance_ce == ce) {
2582
1.65M
        return 1;
2583
1.65M
      }
2584
82.5M
      if (instance_ce == NULL) {
2585
27.6M
        return 0;
2586
27.6M
      }
2587
82.5M
    }
2588
29.2M
  }
2589
30.5M
}
2590
/* }}} */
2591
2592
384
#define LOWER_CASE 1
2593
253
#define UPPER_CASE 2
2594
919
#define NUMERIC 3
2595
2596
ZEND_API bool zend_string_only_has_ascii_alphanumeric(const zend_string *str)
2597
0
{
2598
0
  const char *p = ZSTR_VAL(str);
2599
0
  const char *e = ZSTR_VAL(str) + ZSTR_LEN(str);
2600
0
  while (p < e) {
2601
0
    char c = *p++;
2602
0
    if (UNEXPECTED( c < '0' || c > 'z' || (c < 'a' && c > 'Z') || (c < 'A' && c > '9') ) ) {
2603
0
      return false;
2604
0
    }
2605
0
  }
2606
0
  return true;
2607
0
}
2608
2609
static bool ZEND_FASTCALL increment_string(zval *str) /* {{{ */
2610
1.73k
{
2611
1.73k
  int carry=0;
2612
1.73k
  size_t pos=Z_STRLEN_P(str)-1;
2613
1.73k
  char *s;
2614
1.73k
  zend_string *t;
2615
1.73k
  int last=0; /* Shut up the compiler warning */
2616
1.73k
  int ch;
2617
2618
1.73k
  zend_string *zstr = Z_STR_P(str);
2619
1.73k
  zend_string_addref(zstr);
2620
1.73k
  zend_error(E_DEPRECATED, "Increment on non-numeric string is deprecated, use str_increment() instead");
2621
1.73k
  if (EG(exception)) {
2622
0
    zend_string_release(zstr);
2623
0
    return false;
2624
0
  }
2625
  /* A userland error handler can change the type from string to something else */
2626
1.73k
  zval_ptr_dtor(str);
2627
1.73k
  ZVAL_STR(str, zstr);
2628
2629
1.73k
  if (UNEXPECTED(Z_STRLEN_P(str) == 0)) {
2630
23
    zval_ptr_dtor(str);
2631
23
    ZVAL_CHAR(str, '1');
2632
23
    return true;
2633
23
  }
2634
2635
1.71k
  if (!Z_REFCOUNTED_P(str)) {
2636
502
    Z_STR_P(str) = zend_string_init(Z_STRVAL_P(str), Z_STRLEN_P(str), 0);
2637
502
    Z_TYPE_INFO_P(str) = IS_STRING_EX;
2638
1.21k
  } else if (Z_REFCOUNT_P(str) > 1) {
2639
    /* Only release string after allocation succeeded. */
2640
283
    zend_string *orig_str = Z_STR_P(str);
2641
283
    Z_STR_P(str) = zend_string_init(Z_STRVAL_P(str), Z_STRLEN_P(str), 0);
2642
283
    GC_DELREF(orig_str);
2643
929
  } else {
2644
929
    zend_string_forget_hash_val(Z_STR_P(str));
2645
929
  }
2646
1.71k
  s = Z_STRVAL_P(str);
2647
2648
1.89k
  do {
2649
1.89k
    ch = s[pos];
2650
1.89k
    if (ch >= 'a' && ch <= 'z') {
2651
371
      if (ch == 'z') {
2652
53
        s[pos] = 'a';
2653
53
        carry=1;
2654
318
      } else {
2655
318
        s[pos]++;
2656
318
        carry=0;
2657
318
      }
2658
371
      last=LOWER_CASE;
2659
1.52k
    } else if (ch >= 'A' && ch <= 'Z') {
2660
238
      if (ch == 'Z') {
2661
71
        s[pos] = 'A';
2662
71
        carry=1;
2663
167
      } else {
2664
167
        s[pos]++;
2665
167
        carry=0;
2666
167
      }
2667
238
      last=UPPER_CASE;
2668
1.29k
    } else if (ch >= '0' && ch <= '9') {
2669
901
      if (ch == '9') {
2670
107
        s[pos] = '0';
2671
107
        carry=1;
2672
794
      } else {
2673
794
        s[pos]++;
2674
794
        carry=0;
2675
794
      }
2676
901
      last = NUMERIC;
2677
901
    } else {
2678
389
      carry=0;
2679
389
      break;
2680
389
    }
2681
1.51k
    if (carry == 0) {
2682
1.27k
      break;
2683
1.27k
    }
2684
1.51k
  } while (pos-- > 0);
2685
2686
1.71k
  if (carry) {
2687
46
    t = zend_string_alloc(Z_STRLEN_P(str)+1, 0);
2688
46
    memcpy(ZSTR_VAL(t) + 1, Z_STRVAL_P(str), Z_STRLEN_P(str));
2689
46
    ZSTR_VAL(t)[Z_STRLEN_P(str) + 1] = '\0';
2690
46
    switch (last) {
2691
18
      case NUMERIC:
2692
18
        ZSTR_VAL(t)[0] = '1';
2693
18
        break;
2694
15
      case UPPER_CASE:
2695
15
        ZSTR_VAL(t)[0] = 'A';
2696
15
        break;
2697
13
      case LOWER_CASE:
2698
13
        ZSTR_VAL(t)[0] = 'a';
2699
13
        break;
2700
46
    }
2701
46
    zend_string_free(Z_STR_P(str));
2702
46
    ZVAL_NEW_STR(str, t);
2703
46
  }
2704
1.71k
  return true;
2705
1.71k
}
2706
/* }}} */
2707
2708
ZEND_API zend_result ZEND_FASTCALL increment_function(zval *op1) /* {{{ */
2709
15.2k
{
2710
15.2k
try_again:
2711
15.2k
  switch (Z_TYPE_P(op1)) {
2712
4.95k
    case IS_LONG:
2713
4.95k
      fast_long_increment_function(op1);
2714
4.95k
      break;
2715
2.88k
    case IS_DOUBLE:
2716
2.88k
      Z_DVAL_P(op1) = Z_DVAL_P(op1) + 1;
2717
2.88k
      break;
2718
3.61k
    case IS_NULL:
2719
3.61k
      ZVAL_LONG(op1, 1);
2720
3.61k
      break;
2721
2.47k
    case IS_STRING: {
2722
2.47k
        zend_long lval;
2723
2.47k
        double dval;
2724
2725
2.47k
        switch (is_numeric_str_function(Z_STR_P(op1), &lval, &dval)) {
2726
644
          case IS_LONG:
2727
644
            zval_ptr_dtor_str(op1);
2728
644
            if (lval == ZEND_LONG_MAX) {
2729
              /* switch to double */
2730
4
              double d = (double)lval;
2731
4
              ZVAL_DOUBLE(op1, d+1);
2732
640
            } else {
2733
640
              ZVAL_LONG(op1, lval+1);
2734
640
            }
2735
644
            break;
2736
95
          case IS_DOUBLE:
2737
95
            zval_ptr_dtor_str(op1);
2738
95
            ZVAL_DOUBLE(op1, dval+1);
2739
95
            break;
2740
1.73k
          default:
2741
            /* Perl style string increment */
2742
1.73k
            increment_string(op1);
2743
1.73k
            if (EG(exception)) {
2744
0
              return FAILURE;
2745
0
            }
2746
1.73k
            break;
2747
2.47k
        }
2748
2.47k
      }
2749
2.47k
      break;
2750
2.47k
    case IS_FALSE:
2751
1.24k
    case IS_TRUE: {
2752
      /* Error handler can undef/change type of op1, save it and reset it in case those cases */
2753
1.24k
      zval copy;
2754
1.24k
      ZVAL_COPY_VALUE(&copy, op1);
2755
1.24k
      zend_error(E_WARNING, "Increment on type bool has no effect, this will change in the next major version of PHP");
2756
1.24k
      zval_ptr_dtor(op1);
2757
1.24k
      ZVAL_COPY_VALUE(op1, &copy);
2758
1.24k
      if (EG(exception)) {
2759
0
        return FAILURE;
2760
0
      }
2761
1.24k
      break;
2762
1.24k
    }
2763
1.24k
    case IS_REFERENCE:
2764
0
      op1 = Z_REFVAL_P(op1);
2765
0
      goto try_again;
2766
65
    case IS_OBJECT: {
2767
65
      if (Z_OBJ_HANDLER_P(op1, do_operation)) {
2768
0
        zval op2;
2769
0
        ZVAL_LONG(&op2, 1);
2770
0
        if (Z_OBJ_HANDLER_P(op1, do_operation)(ZEND_ADD, op1, op1, &op2) == SUCCESS) {
2771
0
          return SUCCESS;
2772
0
        }
2773
0
      }
2774
65
      zval tmp;
2775
65
      if (Z_OBJ_HT_P(op1)->cast_object(Z_OBJ_P(op1), &tmp, _IS_NUMBER) == SUCCESS) {
2776
0
        ZEND_ASSERT(Z_TYPE(tmp) == IS_LONG || Z_TYPE(tmp) == IS_DOUBLE);
2777
0
        zval_ptr_dtor(op1);
2778
0
        ZVAL_COPY_VALUE(op1, &tmp);
2779
0
        goto try_again;
2780
0
      }
2781
65
      ZEND_FALLTHROUGH;
2782
65
    }
2783
65
    case IS_RESOURCE:
2784
65
    case IS_ARRAY:
2785
65
      zend_type_error("Cannot increment %s", zend_zval_value_name(op1));
2786
65
      return FAILURE;
2787
0
    default: ZEND_UNREACHABLE();
2788
15.2k
  }
2789
15.1k
  return SUCCESS;
2790
15.2k
}
2791
/* }}} */
2792
2793
ZEND_API zend_result ZEND_FASTCALL decrement_function(zval *op1) /* {{{ */
2794
26.5k
{
2795
26.5k
  zend_long lval;
2796
26.5k
  double dval;
2797
2798
26.5k
try_again:
2799
26.5k
  switch (Z_TYPE_P(op1)) {
2800
2.66k
    case IS_LONG:
2801
2.66k
      fast_long_decrement_function(op1);
2802
2.66k
      break;
2803
9.18k
    case IS_DOUBLE:
2804
9.18k
      Z_DVAL_P(op1) = Z_DVAL_P(op1) - 1;
2805
9.18k
      break;
2806
3.58k
    case IS_STRING:   /* Like perl we only support string increment */
2807
3.58k
      if (Z_STRLEN_P(op1) == 0) { /* consider as 0 */
2808
12
        zend_error(E_DEPRECATED, "Decrement on empty string is deprecated as non-numeric");
2809
12
        if (EG(exception)) {
2810
0
          return FAILURE;
2811
0
        }
2812
        /* A userland error handler can change the type from string to something else */
2813
12
        zval_ptr_dtor(op1);
2814
12
        ZVAL_LONG(op1, -1);
2815
12
        break;
2816
12
      }
2817
3.57k
      switch (is_numeric_str_function(Z_STR_P(op1), &lval, &dval)) {
2818
2.25k
        case IS_LONG:
2819
2.25k
          zval_ptr_dtor_str(op1);
2820
2.25k
          if (lval == ZEND_LONG_MIN) {
2821
9
            double d = (double)lval;
2822
9
            ZVAL_DOUBLE(op1, d-1);
2823
2.24k
          } else {
2824
2.24k
            ZVAL_LONG(op1, lval-1);
2825
2.24k
          }
2826
2.25k
          break;
2827
219
        case IS_DOUBLE:
2828
219
          zval_ptr_dtor_str(op1);
2829
219
          ZVAL_DOUBLE(op1, dval - 1);
2830
219
          break;
2831
1.10k
        default: {
2832
          /* Error handler can unset the variable */
2833
1.10k
          zend_string *zstr = Z_STR_P(op1);
2834
1.10k
          zend_string_addref(zstr);
2835
1.10k
          zend_error(E_DEPRECATED, "Decrement on non-numeric string has no effect and is deprecated");
2836
1.10k
          if (EG(exception)) {
2837
0
            zend_string_release(zstr);
2838
0
            return FAILURE;
2839
0
          }
2840
1.10k
          zval_ptr_dtor(op1);
2841
1.10k
          ZVAL_STR(op1, zstr);
2842
1.10k
        }
2843
3.57k
      }
2844
3.57k
      break;
2845
7.82k
    case IS_NULL: {
2846
      /* Error handler can undef/change type of op1, save it and reset it in case those cases */
2847
7.82k
      zval copy;
2848
7.82k
      ZVAL_COPY_VALUE(&copy, op1);
2849
7.82k
      zend_error(E_WARNING, "Decrement on type null has no effect, this will change in the next major version of PHP");
2850
7.82k
      zval_ptr_dtor(op1);
2851
7.82k
      ZVAL_COPY_VALUE(op1, &copy);
2852
7.82k
      if (EG(exception)) {
2853
0
        return FAILURE;
2854
0
      }
2855
7.82k
      break;
2856
7.82k
    }
2857
7.82k
    case IS_FALSE:
2858
3.19k
    case IS_TRUE: {
2859
      /* Error handler can undef/change type of op1, save it and reset it in case those cases */
2860
3.19k
      zval copy;
2861
3.19k
      ZVAL_COPY_VALUE(&copy, op1);
2862
3.19k
      zend_error(E_WARNING, "Decrement on type bool has no effect, this will change in the next major version of PHP");
2863
3.19k
      zval_ptr_dtor(op1);
2864
3.19k
      ZVAL_COPY_VALUE(op1, &copy);
2865
3.19k
      if (EG(exception)) {
2866
0
        return FAILURE;
2867
0
      }
2868
3.19k
      break;
2869
3.19k
    }
2870
3.19k
    case IS_REFERENCE:
2871
0
      op1 = Z_REFVAL_P(op1);
2872
0
      goto try_again;
2873
57
    case IS_OBJECT: {
2874
57
      if (Z_OBJ_HANDLER_P(op1, do_operation)) {
2875
0
        zval op2;
2876
0
        ZVAL_LONG(&op2, 1);
2877
0
        if (Z_OBJ_HANDLER_P(op1, do_operation)(ZEND_SUB, op1, op1, &op2) == SUCCESS) {
2878
0
          return SUCCESS;
2879
0
        }
2880
0
      }
2881
57
      zval tmp;
2882
57
      if (Z_OBJ_HT_P(op1)->cast_object(Z_OBJ_P(op1), &tmp, _IS_NUMBER) == SUCCESS) {
2883
0
        ZEND_ASSERT(Z_TYPE(tmp) == IS_LONG || Z_TYPE(tmp) == IS_DOUBLE);
2884
0
        zval_ptr_dtor(op1);
2885
0
        ZVAL_COPY_VALUE(op1, &tmp);
2886
0
        goto try_again;
2887
0
      }
2888
57
      ZEND_FALLTHROUGH;
2889
57
    }
2890
57
    case IS_RESOURCE:
2891
73
    case IS_ARRAY:
2892
73
      zend_type_error("Cannot decrement %s", zend_zval_value_name(op1));
2893
73
      return FAILURE;
2894
0
    default: ZEND_UNREACHABLE();
2895
26.5k
  }
2896
2897
26.4k
  return SUCCESS;
2898
26.5k
}
2899
/* }}} */
2900
2901
ZEND_API bool ZEND_FASTCALL zend_is_true(const zval *op) /* {{{ */
2902
691k
{
2903
691k
  return i_zend_is_true(op);
2904
691k
}
2905
/* }}} */
2906
2907
ZEND_API bool ZEND_FASTCALL zend_object_is_true(const zval *op) /* {{{ */
2908
0
{
2909
0
  zend_object *zobj = Z_OBJ_P(op);
2910
0
  zval tmp;
2911
0
  if (zobj->handlers->cast_object(zobj, &tmp, _IS_BOOL) == SUCCESS) {
2912
0
    return Z_TYPE(tmp) == IS_TRUE;
2913
0
  }
2914
0
  zend_error(E_RECOVERABLE_ERROR, "Object of class %s could not be converted to bool", ZSTR_VAL(zobj->ce->name));
2915
0
  return false;
2916
0
}
2917
/* }}} */
2918
2919
ZEND_API void zend_update_current_locale(void) /* {{{ */
2920
38
{
2921
#ifdef ZEND_USE_TOLOWER_L
2922
# if defined(ZEND_WIN32) && defined(_MSC_VER)
2923
  current_locale = _get_current_locale();
2924
# else
2925
  current_locale = uselocale(0);
2926
# endif
2927
#endif
2928
#if defined(ZEND_WIN32) && defined(_MSC_VER)
2929
  if (MB_CUR_MAX > 1) {
2930
    unsigned int cp = ___lc_codepage_func();
2931
    CG(variable_width_locale) = 1;
2932
    // TODO: EUC-* are also ASCII compatible ???
2933
    CG(ascii_compatible_locale) =
2934
      cp == 65001; /* UTF-8 */
2935
  } else {
2936
    CG(variable_width_locale) = 0;
2937
    CG(ascii_compatible_locale) = 1;
2938
  }
2939
#elif defined(MB_CUR_MAX)
2940
  /* Check if current locale uses variable width encoding */
2941
38
  if (MB_CUR_MAX > 1) {
2942
27
#ifdef HAVE_NL_LANGINFO
2943
27
    const char *charmap = nl_langinfo(CODESET);
2944
#else
2945
    char buf[16];
2946
    const char *charmap = NULL;
2947
    const char *locale = setlocale(LC_CTYPE, NULL);
2948
2949
    if (locale) {
2950
      const char *dot = strchr(locale, '.');
2951
      const char *modifier;
2952
2953
      if (dot) {
2954
        dot++;
2955
        modifier = strchr(dot, '@');
2956
        if (!modifier) {
2957
          charmap = dot;
2958
        } else if (modifier - dot < sizeof(buf)) {
2959
          memcpy(buf, dot, modifier - dot);
2960
                    buf[modifier - dot] = '\0';
2961
                    charmap = buf;
2962
        }
2963
      }
2964
    }
2965
#endif
2966
27
    CG(variable_width_locale) = 1;
2967
27
    CG(ascii_compatible_locale) = 0;
2968
2969
27
    if (charmap) {
2970
27
      size_t len = strlen(charmap);
2971
27
      static const char *ascii_compatible_charmaps[] = {
2972
27
        "utf-8",
2973
27
        "utf8",
2974
        // TODO: EUC-* are also ASCII compatible ???
2975
27
        NULL
2976
27
      };
2977
27
      const char **p;
2978
      /* Check if current locale is ASCII compatible */
2979
27
      for (p = ascii_compatible_charmaps; *p; p++) {
2980
27
        if (zend_binary_strcasecmp(charmap, len, *p, strlen(*p)) == 0) {
2981
27
          CG(ascii_compatible_locale) = 1;
2982
27
          break;
2983
27
        }
2984
27
      }
2985
27
    }
2986
2987
27
  } else {
2988
11
    CG(variable_width_locale) = 0;
2989
11
    CG(ascii_compatible_locale) = 1;
2990
11
  }
2991
#else
2992
  /* We can't determine current charset. Assume the worst case */
2993
  CG(variable_width_locale) = 1;
2994
  CG(ascii_compatible_locale) = 0;
2995
#endif
2996
38
}
2997
/* }}} */
2998
2999
ZEND_API void zend_reset_lc_ctype_locale(void)
3000
27
{
3001
  /* Use the C.UTF-8 locale so that readline can process UTF-8 input, while not interfering
3002
   * with single-byte locale-dependent functions used by PHP. */
3003
27
  if (!setlocale(LC_CTYPE, "C.UTF-8")) {
3004
0
    setlocale(LC_CTYPE, "C");
3005
0
  }
3006
27
}
3007
3008
20.2M
static zend_always_inline void zend_str_tolower_impl(char *dest, const char *str, size_t length) /* {{{ */ {
3009
20.2M
  unsigned char *p = (unsigned char*)str;
3010
20.2M
  unsigned char *q = (unsigned char*)dest;
3011
20.2M
  unsigned char *end = p + length;
3012
20.2M
#ifdef HAVE_BLOCKCONV
3013
20.2M
  if (length >= BLOCKCONV_STRIDE) {
3014
1.92M
    BLOCKCONV_INIT_RANGE('A', 'Z');
3015
1.92M
    BLOCKCONV_INIT_DELTA('a' - 'A');
3016
3.34M
    do {
3017
3.34M
      BLOCKCONV_LOAD(p);
3018
3.34M
      BLOCKCONV_STORE(q);
3019
3.34M
      p += BLOCKCONV_STRIDE;
3020
3.34M
      q += BLOCKCONV_STRIDE;
3021
3.34M
    } while (p + BLOCKCONV_STRIDE <= end);
3022
1.92M
  }
3023
20.2M
#endif
3024
159M
  while (p < end) {
3025
139M
    *q++ = zend_tolower_ascii(*p++);
3026
139M
  }
3027
20.2M
}
3028
/* }}} */
3029
3030
37
static zend_always_inline void zend_str_toupper_impl(char *dest, const char *str, size_t length) /* {{{ */ {
3031
37
  unsigned char *p = (unsigned char*)str;
3032
37
  unsigned char *q = (unsigned char*)dest;
3033
37
  unsigned char *end = p + length;
3034
37
#ifdef HAVE_BLOCKCONV
3035
37
  if (length >= BLOCKCONV_STRIDE) {
3036
24
    BLOCKCONV_INIT_RANGE('a', 'z');
3037
24
    BLOCKCONV_INIT_DELTA('A' - 'a');
3038
1.10k
    do {
3039
1.10k
      BLOCKCONV_LOAD(p);
3040
1.10k
      BLOCKCONV_STORE(q);
3041
1.10k
      p += BLOCKCONV_STRIDE;
3042
1.10k
      q += BLOCKCONV_STRIDE;
3043
1.10k
    } while (p + BLOCKCONV_STRIDE <= end);
3044
24
  }
3045
37
#endif
3046
207
  while (p < end) {
3047
170
    *q++ = zend_toupper_ascii(*p++);
3048
170
  }
3049
37
}
3050
/* }}} */
3051
3052
ZEND_API char* ZEND_FASTCALL zend_str_tolower_copy(char *dest, const char *source, size_t length) /* {{{ */
3053
4.29M
{
3054
4.29M
  zend_str_tolower_impl(dest, source, length);
3055
4.29M
  dest[length] = '\0';
3056
4.29M
  return dest;
3057
4.29M
}
3058
/* }}} */
3059
3060
ZEND_API char* ZEND_FASTCALL zend_str_toupper_copy(char *dest, const char *source, size_t length) /* {{{ */
3061
0
{
3062
0
  zend_str_toupper_impl(dest, source, length);
3063
0
  dest[length] = '\0';
3064
0
  return dest;
3065
0
}
3066
/* }}} */
3067
3068
ZEND_API char* ZEND_FASTCALL zend_str_tolower_dup(const char *source, size_t length) /* {{{ */
3069
1.14k
{
3070
1.14k
  return zend_str_tolower_copy((char *)emalloc(length+1), source, length);
3071
1.14k
}
3072
/* }}} */
3073
3074
ZEND_API char* ZEND_FASTCALL zend_str_toupper_dup(const char *source, size_t length) /* {{{ */
3075
0
{
3076
0
  return zend_str_toupper_copy((char *)emalloc(length+1), source, length);
3077
0
}
3078
/* }}} */
3079
3080
ZEND_API void ZEND_FASTCALL zend_str_tolower(char *str, size_t length) /* {{{ */
3081
10.4M
{
3082
10.4M
  zend_str_tolower_impl(str, (const char*)str, length);
3083
10.4M
}
3084
/* }}} */
3085
3086
ZEND_API void ZEND_FASTCALL zend_str_toupper(char *str, size_t length) /* {{{ */
3087
0
{
3088
0
  zend_str_toupper_impl(str, (const char*)str, length);
3089
0
}
3090
/* }}} */
3091
3092
3093
ZEND_API char* ZEND_FASTCALL zend_str_tolower_dup_ex(const char *source, size_t length) /* {{{ */
3094
84
{
3095
84
  const unsigned char *p = (const unsigned char*)source;
3096
84
  const unsigned char *end = p + length;
3097
3098
3.29k
  while (p < end) {
3099
3.28k
    if (*p != zend_tolower_ascii(*p)) {
3100
78
      char *res = (char*)emalloc(length + 1);
3101
78
      unsigned char *r;
3102
3103
78
      if (p != (const unsigned char*)source) {
3104
78
        memcpy(res, source, p - (const unsigned char*)source);
3105
78
      }
3106
78
      r = (unsigned char*)p + (res - source);
3107
78
      zend_str_tolower_impl((char *)r, (const char*)p, end - p);
3108
78
      res[length] = '\0';
3109
78
      return res;
3110
78
    }
3111
3.20k
    p++;
3112
3.20k
  }
3113
6
  return NULL;
3114
84
}
3115
/* }}} */
3116
3117
ZEND_API char* ZEND_FASTCALL zend_str_toupper_dup_ex(const char *source, size_t length) /* {{{ */
3118
0
{
3119
0
  const unsigned char *p = (const unsigned char*)source;
3120
0
  const unsigned char *end = p + length;
3121
3122
0
  while (p < end) {
3123
0
    if (*p != zend_toupper_ascii(*p)) {
3124
0
      char *res = (char*)emalloc(length + 1);
3125
0
      unsigned char *r;
3126
3127
0
      if (p != (const unsigned char*)source) {
3128
0
        memcpy(res, source, p - (const unsigned char*)source);
3129
0
      }
3130
0
      r = (unsigned char*)p + (res - source);
3131
0
      zend_str_toupper_impl((char *)r, (const char*)p, end - p);
3132
0
      res[length] = '\0';
3133
0
      return res;
3134
0
    }
3135
0
    p++;
3136
0
  }
3137
0
  return NULL;
3138
0
}
3139
/* }}} */
3140
3141
ZEND_API zend_string* ZEND_FASTCALL zend_string_tolower_ex(zend_string *str, bool persistent) /* {{{ */
3142
13.7M
{
3143
13.7M
  size_t length = ZSTR_LEN(str);
3144
13.7M
  unsigned char *p = (unsigned char *) ZSTR_VAL(str);
3145
13.7M
  unsigned char *end = p + length;
3146
3147
13.7M
#ifdef HAVE_BLOCKCONV
3148
13.7M
  BLOCKCONV_INIT_RANGE('A', 'Z');
3149
36.1M
  while (p + BLOCKCONV_STRIDE <= end) {
3150
27.9M
    BLOCKCONV_LOAD(p);
3151
27.9M
    if (BLOCKCONV_FOUND()) {
3152
5.50M
      zend_string *res = zend_string_alloc(length, persistent);
3153
5.50M
      memcpy(ZSTR_VAL(res), ZSTR_VAL(str), p - (unsigned char *) ZSTR_VAL(str));
3154
5.50M
      unsigned char *q = (unsigned char*) ZSTR_VAL(res) + (p - (unsigned char*) ZSTR_VAL(str));
3155
3156
      /* Lowercase the chunk we already compared. */
3157
5.50M
      BLOCKCONV_INIT_DELTA('a' - 'A');
3158
5.50M
      BLOCKCONV_STORE(q);
3159
3160
      /* Lowercase the rest of the string. */
3161
5.50M
      p += BLOCKCONV_STRIDE;
3162
5.50M
      q += BLOCKCONV_STRIDE;
3163
5.50M
      zend_str_tolower_impl((char *) q, (const char *) p, end - p);
3164
5.50M
      ZSTR_VAL(res)[length] = '\0';
3165
5.50M
      return res;
3166
5.50M
    }
3167
22.4M
    p += BLOCKCONV_STRIDE;
3168
22.4M
  }
3169
8.20M
#endif
3170
3171
41.1M
  while (p < end) {
3172
38.1M
    if (*p != zend_tolower_ascii(*p)) {
3173
5.25M
      zend_string *res = zend_string_alloc(length, persistent);
3174
5.25M
      memcpy(ZSTR_VAL(res), ZSTR_VAL(str), p - (unsigned char*) ZSTR_VAL(str));
3175
3176
5.25M
      unsigned char *q = (unsigned char*) ZSTR_VAL(res) + (p - (unsigned char*) ZSTR_VAL(str));
3177
48.1M
      while (p < end) {
3178
42.9M
        *q++ = zend_tolower_ascii(*p++);
3179
42.9M
      }
3180
5.25M
      ZSTR_VAL(res)[length] = '\0';
3181
5.25M
      return res;
3182
5.25M
    }
3183
32.9M
    p++;
3184
32.9M
  }
3185
3186
2.94M
  return zend_string_copy(str);
3187
8.20M
}
3188
/* }}} */
3189
3190
ZEND_API zend_string* ZEND_FASTCALL zend_string_toupper_ex(zend_string *str, bool persistent) /* {{{ */
3191
1.36k
{
3192
1.36k
  size_t length = ZSTR_LEN(str);
3193
1.36k
  unsigned char *p = (unsigned char *) ZSTR_VAL(str);
3194
1.36k
  unsigned char *end = p + length;
3195
3196
1.36k
#ifdef HAVE_BLOCKCONV
3197
1.36k
  BLOCKCONV_INIT_RANGE('a', 'z');
3198
1.36k
  while (p + BLOCKCONV_STRIDE <= end) {
3199
40
    BLOCKCONV_LOAD(p);
3200
40
    if (BLOCKCONV_FOUND()) {
3201
37
      zend_string *res = zend_string_alloc(length, persistent);
3202
37
      memcpy(ZSTR_VAL(res), ZSTR_VAL(str), p - (unsigned char *) ZSTR_VAL(str));
3203
37
      unsigned char *q = (unsigned char *) ZSTR_VAL(res) + (p - (unsigned char *) ZSTR_VAL(str));
3204
3205
      /* Uppercase the chunk we already compared. */
3206
37
      BLOCKCONV_INIT_DELTA('A' - 'a');
3207
37
      BLOCKCONV_STORE(q);
3208
3209
      /* Uppercase the rest of the string. */
3210
37
      p += BLOCKCONV_STRIDE;
3211
37
      q += BLOCKCONV_STRIDE;
3212
37
      zend_str_toupper_impl((char *) q, (const char *) p, end - p);
3213
37
      ZSTR_VAL(res)[length] = '\0';
3214
37
      return res;
3215
37
    }
3216
3
    p += BLOCKCONV_STRIDE;
3217
3
  }
3218
1.32k
#endif
3219
3220
1.36k
  while (p < end) {
3221
1.21k
    if (*p != zend_toupper_ascii(*p)) {
3222
1.17k
      zend_string *res = zend_string_alloc(length, persistent);
3223
1.17k
      memcpy(ZSTR_VAL(res), ZSTR_VAL(str), p - (unsigned char*) ZSTR_VAL(str));
3224
3225
1.17k
      unsigned char *q = (unsigned char *) ZSTR_VAL(res) + (p - (unsigned char *) ZSTR_VAL(str));
3226
9.70k
      while (p < end) {
3227
8.53k
        *q++ = zend_toupper_ascii(*p++);
3228
8.53k
      }
3229
1.17k
      ZSTR_VAL(res)[length] = '\0';
3230
1.17k
      return res;
3231
1.17k
    }
3232
40
    p++;
3233
40
  }
3234
3235
157
  return zend_string_copy(str);
3236
1.32k
}
3237
/* }}} */
3238
3239
ZEND_API int ZEND_FASTCALL zend_binary_strcmp(const char *s1, size_t len1, const char *s2, size_t len2) /* {{{ */
3240
110k
{
3241
110k
  int retval;
3242
3243
110k
  if (s1 == s2) {
3244
12.9k
    return 0;
3245
12.9k
  }
3246
97.5k
  retval = memcmp(s1, s2, MIN(len1, len2));
3247
97.5k
  if (!retval) {
3248
17.7k
    return ZEND_THREEWAY_COMPARE(len1, len2);
3249
79.7k
  } else {
3250
79.7k
    return retval;
3251
79.7k
  }
3252
97.5k
}
3253
/* }}} */
3254
3255
ZEND_API int ZEND_FASTCALL zend_binary_strncmp(const char *s1, size_t len1, const char *s2, size_t len2, size_t length) /* {{{ */
3256
47
{
3257
47
  int retval;
3258
3259
47
  if (s1 == s2) {
3260
14
    return 0;
3261
14
  }
3262
33
  retval = memcmp(s1, s2, MIN(length, MIN(len1, len2)));
3263
33
  if (!retval) {
3264
25
    return ZEND_THREEWAY_COMPARE(MIN(length, len1), MIN(length, len2));
3265
25
  } else {
3266
8
    return retval;
3267
8
  }
3268
33
}
3269
/* }}} */
3270
3271
ZEND_API int ZEND_FASTCALL zend_binary_strcasecmp(const char *s1, size_t len1, const char *s2, size_t len2) /* {{{ */
3272
2.21M
{
3273
2.21M
  size_t len;
3274
2.21M
  int c1, c2;
3275
3276
2.21M
  if (s1 == s2) {
3277
20.4k
    return 0;
3278
20.4k
  }
3279
3280
2.19M
  len = MIN(len1, len2);
3281
7.41M
  while (len--) {
3282
7.01M
    c1 = zend_tolower_ascii(*(unsigned char *)s1++);
3283
7.01M
    c2 = zend_tolower_ascii(*(unsigned char *)s2++);
3284
7.01M
    if (c1 != c2) {
3285
1.80M
      return c1 - c2;
3286
1.80M
    }
3287
7.01M
  }
3288
3289
393k
  return ZEND_THREEWAY_COMPARE(len1, len2);
3290
2.19M
}
3291
/* }}} */
3292
3293
ZEND_API int ZEND_FASTCALL zend_binary_strncasecmp(const char *s1, size_t len1, const char *s2, size_t len2, size_t length) /* {{{ */
3294
76
{
3295
76
  size_t len;
3296
76
  int c1, c2;
3297
3298
76
  if (s1 == s2) {
3299
7
    return 0;
3300
7
  }
3301
69
  len = MIN(length, MIN(len1, len2));
3302
210
  while (len--) {
3303
160
    c1 = zend_tolower_ascii(*(unsigned char *)s1++);
3304
160
    c2 = zend_tolower_ascii(*(unsigned char *)s2++);
3305
160
    if (c1 != c2) {
3306
19
      return c1 - c2;
3307
19
    }
3308
160
  }
3309
3310
50
  return ZEND_THREEWAY_COMPARE(MIN(length, len1), MIN(length, len2));
3311
69
}
3312
/* }}} */
3313
3314
ZEND_API int ZEND_FASTCALL zend_binary_strcasecmp_l(const char *s1, size_t len1, const char *s2, size_t len2) /* {{{ */
3315
2
{
3316
2
  size_t len;
3317
2
  int c1, c2;
3318
3319
2
  if (s1 == s2) {
3320
0
    return 0;
3321
0
  }
3322
3323
2
  len = MIN(len1, len2);
3324
2
  while (len--) {
3325
2
    c1 = zend_tolower((unsigned char)*(s1++));
3326
2
    c2 = zend_tolower((unsigned char)*(s2++));
3327
2
    if (c1 != c2) {
3328
2
      return c1 - c2;
3329
2
    }
3330
2
  }
3331
3332
0
  return ZEND_THREEWAY_COMPARE(len1, len2);
3333
2
}
3334
/* }}} */
3335
3336
ZEND_API int ZEND_FASTCALL zend_binary_strncasecmp_l(const char *s1, size_t len1, const char *s2, size_t len2, size_t length) /* {{{ */
3337
0
{
3338
0
  size_t len;
3339
0
  int c1, c2;
3340
3341
0
  if (s1 == s2) {
3342
0
    return 0;
3343
0
  }
3344
0
  len = MIN(length, MIN(len1, len2));
3345
0
  while (len--) {
3346
0
    c1 = zend_tolower((unsigned char)*(s1++));
3347
0
    c2 = zend_tolower((unsigned char)*(s2++));
3348
0
    if (c1 != c2) {
3349
0
      return c1 - c2;
3350
0
    }
3351
0
  }
3352
3353
0
  return ZEND_THREEWAY_COMPARE(MIN(length, len1), MIN(length, len2));
3354
0
}
3355
/* }}} */
3356
3357
ZEND_API bool ZEND_FASTCALL zendi_smart_streq(const zend_string *s1, const zend_string *s2) /* {{{ */
3358
1.12k
{
3359
1.12k
  uint8_t ret1, ret2;
3360
1.12k
  int oflow1, oflow2;
3361
1.12k
  zend_long lval1 = 0, lval2 = 0;
3362
1.12k
  double dval1 = 0.0, dval2 = 0.0;
3363
3364
1.12k
  if ((ret1 = is_numeric_string_ex(s1->val, s1->len, &lval1, &dval1, false, &oflow1, NULL)) &&
3365
695
    (ret2 = is_numeric_string_ex(s2->val, s2->len, &lval2, &dval2, false, &oflow2, NULL))) {
3366
#if ZEND_ULONG_MAX == 0xFFFFFFFF
3367
    if (oflow1 != 0 && oflow1 == oflow2 && dval1 - dval2 == 0. &&
3368
      ((oflow1 == 1 && dval1 > 9007199254740991. /*0x1FFFFFFFFFFFFF*/)
3369
      || (oflow1 == -1 && dval1 < -9007199254740991.))) {
3370
#else
3371
514
    if (oflow1 != 0 && oflow1 == oflow2 && dval1 - dval2 == 0.) {
3372
0
#endif
3373
      /* both values are integers overflown to the same side, and the
3374
       * double comparison may have resulted in crucial accuracy lost */
3375
0
      goto string_cmp;
3376
0
    }
3377
514
    if ((ret1 == IS_DOUBLE) || (ret2 == IS_DOUBLE)) {
3378
198
      if (ret1 != IS_DOUBLE) {
3379
65
        if (oflow2) {
3380
          /* 2nd operand is integer > LONG_MAX (oflow2==1) or < LONG_MIN (-1) */
3381
28
          return 0;
3382
28
        }
3383
37
        dval1 = (double) lval1;
3384
133
      } else if (ret2 != IS_DOUBLE) {
3385
56
        if (oflow1) {
3386
12
          return 0;
3387
12
        }
3388
44
        dval2 = (double) lval2;
3389
77
      } else if (dval1 == dval2 && !zend_finite(dval1)) {
3390
        /* Both values overflowed and have the same sign,
3391
         * so a numeric comparison would be inaccurate */
3392
30
        goto string_cmp;
3393
30
      }
3394
128
      return dval1 == dval2;
3395
316
    } else { /* they both have to be long's */
3396
316
      return lval1 == lval2;
3397
316
    }
3398
610
  } else {
3399
640
string_cmp:
3400
640
    return zend_string_equal_content(s1, s2);
3401
610
  }
3402
1.12k
}
3403
/* }}} */
3404
3405
ZEND_API int ZEND_FASTCALL zendi_smart_strcmp(const zend_string *s1, const zend_string *s2) /* {{{ */
3406
20.1k
{
3407
20.1k
  uint8_t ret1, ret2;
3408
20.1k
  int oflow1, oflow2;
3409
20.1k
  zend_long lval1 = 0, lval2 = 0;
3410
20.1k
  double dval1 = 0.0, dval2 = 0.0;
3411
3412
20.1k
  if ((ret1 = is_numeric_string_ex(s1->val, s1->len, &lval1, &dval1, false, &oflow1, NULL)) &&
3413
4.79k
    (ret2 = is_numeric_string_ex(s2->val, s2->len, &lval2, &dval2, false, &oflow2, NULL))) {
3414
#if ZEND_ULONG_MAX == 0xFFFFFFFF
3415
    if (oflow1 != 0 && oflow1 == oflow2 && dval1 - dval2 == 0. &&
3416
      ((oflow1 == 1 && dval1 > 9007199254740991. /*0x1FFFFFFFFFFFFF*/)
3417
      || (oflow1 == -1 && dval1 < -9007199254740991.))) {
3418
#else
3419
3.81k
    if (oflow1 != 0 && oflow1 == oflow2 && dval1 - dval2 == 0.) {
3420
25
#endif
3421
      /* both values are integers overflowed to the same side, and the
3422
       * double comparison may have resulted in crucial accuracy lost */
3423
25
      goto string_cmp;
3424
25
    }
3425
3.78k
    if ((ret1 == IS_DOUBLE) || (ret2 == IS_DOUBLE)) {
3426
1.68k
      if (ret1 != IS_DOUBLE) {
3427
329
        if (oflow2) {
3428
          /* 2nd operand is integer > LONG_MAX (oflow2==1) or < LONG_MIN (-1) */
3429
232
          return -1 * oflow2;
3430
232
        }
3431
97
        dval1 = (double) lval1;
3432
1.35k
      } else if (ret2 != IS_DOUBLE) {
3433
723
        if (oflow1) {
3434
429
          return oflow1;
3435
429
        }
3436
294
        dval2 = (double) lval2;
3437
636
      } else if (dval1 == dval2 && !zend_finite(dval1)) {
3438
        /* Both values overflowed and have the same sign,
3439
         * so a numeric comparison would be inaccurate */
3440
107
        goto string_cmp;
3441
107
      }
3442
920
      dval1 = dval1 - dval2;
3443
920
      return ZEND_NORMALIZE_BOOL(dval1);
3444
2.09k
    } else { /* they both have to be long's */
3445
2.09k
      return lval1 > lval2 ? 1 : (lval1 < lval2 ? -1 : 0);
3446
2.09k
    }
3447
16.3k
  } else {
3448
16.3k
    int strcmp_ret;
3449
16.5k
string_cmp:
3450
16.5k
    strcmp_ret = zend_binary_strcmp(s1->val, s1->len, s2->val, s2->len);
3451
16.5k
    return ZEND_NORMALIZE_BOOL(strcmp_ret);
3452
16.3k
  }
3453
20.1k
}
3454
/* }}} */
3455
3456
static int hash_zval_compare_function(zval *z1, zval *z2) /* {{{ */
3457
603
{
3458
603
  return zend_compare(z1, z2);
3459
603
}
3460
/* }}} */
3461
3462
ZEND_API int ZEND_FASTCALL zend_compare_symbol_tables(HashTable *ht1, HashTable *ht2) /* {{{ */
3463
9.89k
{
3464
9.89k
  if (ht1 == ht2) {
3465
3.96k
    return 0;
3466
3.96k
  }
3467
3468
5.93k
  GC_TRY_ADDREF(ht1);
3469
5.93k
  GC_TRY_ADDREF(ht2);
3470
3471
5.93k
  int ret = zend_hash_compare(ht1, ht2, (compare_func_t) hash_zval_compare_function, 0);
3472
3473
5.93k
  GC_TRY_DTOR_NO_REF(ht1);
3474
5.93k
  GC_TRY_DTOR_NO_REF(ht2);
3475
3476
5.93k
  return ret;
3477
9.89k
}
3478
/* }}} */
3479
3480
ZEND_API int ZEND_FASTCALL zend_compare_arrays(const zval *a1, const zval *a2) /* {{{ */
3481
9.83k
{
3482
9.83k
  return zend_compare_symbol_tables(Z_ARRVAL_P(a1), Z_ARRVAL_P(a2));
3483
9.83k
}
3484
/* }}} */
3485
3486
ZEND_API int ZEND_FASTCALL zend_compare_objects(zval *o1, zval *o2) /* {{{ */
3487
0
{
3488
0
  if (Z_OBJ_P(o1) == Z_OBJ_P(o2)) {
3489
0
    return 0;
3490
0
  }
3491
3492
0
  if (Z_OBJ_HT_P(o1)->compare == NULL) {
3493
0
    return 1;
3494
0
  } else {
3495
0
    return Z_OBJ_HT_P(o1)->compare(o1, o2);
3496
0
  }
3497
0
}
3498
/* }}} */
3499
3500
ZEND_API zend_string* ZEND_FASTCALL zend_long_to_str(zend_long num) /* {{{ */
3501
1.25M
{
3502
1.25M
  if ((zend_ulong)num <= 9) {
3503
1.09M
    return ZSTR_CHAR((zend_uchar)'0' + (zend_uchar)num);
3504
1.09M
  } else {
3505
157k
    char buf[MAX_LENGTH_OF_LONG + 1];
3506
157k
    char *res = zend_print_long_to_buf(buf + sizeof(buf) - 1, num);
3507
157k
    zend_string *str =  zend_string_init(res, buf + sizeof(buf) - 1 - res, 0);
3508
157k
    GC_ADD_FLAGS(str, IS_STR_VALID_UTF8);
3509
157k
    return str;
3510
157k
  }
3511
1.25M
}
3512
/* }}} */
3513
3514
ZEND_API zend_string* ZEND_FASTCALL zend_ulong_to_str(zend_ulong num)
3515
0
{
3516
0
  if (num <= 9) {
3517
0
    return ZSTR_CHAR((zend_uchar)'0' + (zend_uchar)num);
3518
0
  } else {
3519
0
    char buf[MAX_LENGTH_OF_LONG + 1];
3520
0
    char *res = zend_print_ulong_to_buf(buf + sizeof(buf) - 1, num);
3521
0
    zend_string *str =  zend_string_init(res, buf + sizeof(buf) - 1 - res, 0);
3522
0
    GC_ADD_FLAGS(str, IS_STR_VALID_UTF8);
3523
0
    return str;
3524
0
  }
3525
0
}
3526
3527
/* buf points to the END of the buffer */
3528
0
static zend_always_inline char *zend_print_u64_to_buf(char *buf, uint64_t num64) {
3529
0
#if SIZEOF_ZEND_LONG == 8
3530
0
  return zend_print_ulong_to_buf(buf, num64);
3531
#else
3532
  *buf = '\0';
3533
  while (num64 > ZEND_ULONG_MAX) {
3534
    *--buf = (char) (num64 % 10) + '0';
3535
    num64 /= 10;
3536
  }
3537
3538
  zend_ulong num = (zend_ulong) num64;
3539
  do {
3540
    *--buf = (char) (num % 10) + '0';
3541
    num /= 10;
3542
  } while (num > 0);
3543
  return buf;
3544
#endif
3545
0
}
3546
3547
/* buf points to the END of the buffer */
3548
0
static zend_always_inline char *zend_print_i64_to_buf(char *buf, int64_t num) {
3549
0
  if (num < 0) {
3550
0
      char *result = zend_print_u64_to_buf(buf, ~((uint64_t) num) + 1);
3551
0
      *--result = '-';
3552
0
    return result;
3553
0
  } else {
3554
0
      return zend_print_u64_to_buf(buf, num);
3555
0
  }
3556
0
}
3557
3558
ZEND_API zend_string* ZEND_FASTCALL zend_u64_to_str(uint64_t num)
3559
0
{
3560
0
  if (num <= 9) {
3561
0
    return ZSTR_CHAR((zend_uchar)'0' + (zend_uchar)num);
3562
0
  } else {
3563
0
    char buf[20 + 1];
3564
0
    char *res = zend_print_u64_to_buf(buf + sizeof(buf) - 1, num);
3565
0
    zend_string *str =  zend_string_init(res, buf + sizeof(buf) - 1 - res, 0);
3566
0
    GC_ADD_FLAGS(str, IS_STR_VALID_UTF8);
3567
0
    return str;
3568
0
  }
3569
0
}
3570
3571
ZEND_API zend_string* ZEND_FASTCALL zend_i64_to_str(int64_t num)
3572
0
{
3573
0
  if ((uint64_t)num <= 9) {
3574
0
    return ZSTR_CHAR((zend_uchar)'0' + (zend_uchar)num);
3575
0
  } else {
3576
0
    char buf[20 + 1];
3577
0
    char *res = zend_print_i64_to_buf(buf + sizeof(buf) - 1, num);
3578
0
    zend_string *str =  zend_string_init(res, buf + sizeof(buf) - 1 - res, 0);
3579
0
    GC_ADD_FLAGS(str, IS_STR_VALID_UTF8);
3580
0
    return str;
3581
0
  }
3582
0
}
3583
3584
ZEND_API zend_string* ZEND_FASTCALL zend_double_to_str(double num)
3585
127k
{
3586
127k
  char buf[ZEND_DOUBLE_MAX_LENGTH];
3587
  /* Model snprintf precision behavior. */
3588
127k
  int precision = (int) EG(precision);
3589
127k
  zend_gcvt(num, precision ? precision : 1, '.', 'E', buf);
3590
127k
  zend_string *str =  zend_string_init(buf, strlen(buf), 0);
3591
127k
  if (UNEXPECTED(zend_isnan(num))) {
3592
344
    zend_nan_coerced_to_type_warning(IS_STRING);
3593
344
  }
3594
127k
  GC_ADD_FLAGS(str, IS_STR_VALID_UTF8);
3595
127k
  return str;
3596
127k
}
3597
3598
ZEND_API uint8_t ZEND_FASTCALL is_numeric_str_function(const zend_string *str, zend_long *lval, double *dval) /* {{{ */
3599
23.5k
{
3600
23.5k
  return is_numeric_string(ZSTR_VAL(str), ZSTR_LEN(str), lval, dval, false);
3601
23.5k
}
3602
/* }}} */
3603
3604
ZEND_API uint8_t ZEND_FASTCALL _is_numeric_string_ex(const char *str, size_t length, zend_long *lval,
3605
  double *dval, bool allow_errors, int *oflow_info, bool *trailing_data) /* {{{ */
3606
392k
{
3607
392k
  const char *ptr;
3608
392k
  int digits = 0, dp_or_e = 0;
3609
392k
  double local_dval = 0.0;
3610
392k
  uint8_t type;
3611
392k
  zend_ulong tmp_lval = 0;
3612
392k
  int neg = 0;
3613
3614
392k
  if (!length) {
3615
7.03k
    return 0;
3616
7.03k
  }
3617
3618
385k
  if (oflow_info != NULL) {
3619
23.7k
    *oflow_info = 0;
3620
23.7k
  }
3621
385k
  if (trailing_data != NULL) {
3622
55.9k
    *trailing_data = false;
3623
55.9k
  }
3624
3625
  /* Skip any whitespace
3626
   * This is much faster than the isspace() function */
3627
405k
  while (*str == ' ' || *str == '\t' || *str == '\n' || *str == '\r' || *str == '\v' || *str == '\f') {
3628
20.6k
    str++;
3629
20.6k
    length--;
3630
20.6k
  }
3631
385k
  ptr = str;
3632
3633
385k
  if (*ptr == '-') {
3634
56.6k
    neg = 1;
3635
56.6k
    ptr++;
3636
328k
  } else if (*ptr == '+') {
3637
1.02k
    ptr++;
3638
1.02k
  }
3639
3640
385k
  if (ZEND_IS_DIGIT(*ptr)) {
3641
    /* Skip any leading 0s */
3642
1.09G
    while (*ptr == '0') {
3643
1.09G
      ptr++;
3644
1.09G
    }
3645
3646
    /* Count the number of digits. If a decimal point/exponent is found,
3647
     * it's a double. Otherwise, if there's a dval or no need to check for
3648
     * a full match, stop when there are too many digits for a long */
3649
1.83M
    for (type = IS_LONG; !(digits >= MAX_LENGTH_OF_LONG && (dval || allow_errors)); digits++, ptr++) {
3650
1.87M
check_digits:
3651
1.87M
      if (ZEND_IS_DIGIT(*ptr)) {
3652
1.48M
        tmp_lval = tmp_lval * 10 + (*ptr) - '0';
3653
1.48M
        continue;
3654
1.48M
      } else if (*ptr == '.' && dp_or_e < 1) {
3655
41.9k
        goto process_double;
3656
345k
      } else if ((*ptr == 'e' || *ptr == 'E') && dp_or_e < 2) {
3657
68.7k
        const char *e = ptr + 1;
3658
3659
68.7k
        if (*e == '-' || *e == '+') {
3660
27.8k
          ptr = e++;
3661
27.8k
        }
3662
68.7k
        if (ZEND_IS_DIGIT(*e)) {
3663
58.7k
          goto process_double;
3664
58.7k
        }
3665
68.7k
      }
3666
3667
287k
      break;
3668
1.87M
    }
3669
3670
295k
    if (digits >= MAX_LENGTH_OF_LONG) {
3671
13.1k
      if (oflow_info != NULL) {
3672
787
        *oflow_info = *str == '-' ? -1 : 1;
3673
787
      }
3674
13.1k
      dp_or_e = -1;
3675
13.1k
      goto process_double;
3676
13.1k
    }
3677
295k
  } else if (*ptr == '.' && ZEND_IS_DIGIT(ptr[1])) {
3678
119k
process_double:
3679
119k
    type = IS_DOUBLE;
3680
3681
    /* If there's a dval, do the conversion; else continue checking
3682
     * the digits if we need to check for a full match */
3683
119k
    if (dval) {
3684
62.3k
      local_dval = zend_strtod(str, &ptr);
3685
62.3k
    } else if (!allow_errors && dp_or_e != -1) {
3686
51.5k
      dp_or_e = (*ptr++ == '.') ? 1 : 2;
3687
51.5k
      goto check_digits;
3688
51.5k
    }
3689
119k
  } else {
3690
35.3k
    return 0;
3691
35.3k
  }
3692
3693
349k
  if (ptr != str + length) {
3694
101k
    const char *endptr = ptr;
3695
122k
    while (*endptr == ' ' || *endptr == '\t' || *endptr == '\n' || *endptr == '\r' || *endptr == '\v' || *endptr == '\f') {
3696
20.5k
      endptr++;
3697
20.5k
      length--;
3698
20.5k
    }
3699
101k
    if (ptr != str + length) {
3700
99.6k
      if (!allow_errors) {
3701
59.1k
        return 0;
3702
59.1k
      }
3703
40.4k
      if (trailing_data != NULL) {
3704
23.9k
        *trailing_data = true;
3705
23.9k
      }
3706
40.4k
    }
3707
101k
  }
3708
3709
290k
  if (type == IS_LONG) {
3710
209k
    if (digits == MAX_LENGTH_OF_LONG - 1) {
3711
17.4k
      int cmp = strcmp(&ptr[-digits], long_min_digits);
3712
3713
17.4k
      if (!(cmp < 0 || (cmp == 0 && *str == '-'))) {
3714
8.75k
        if (dval) {
3715
5.62k
          *dval = zend_strtod(str, NULL);
3716
5.62k
        }
3717
8.75k
        if (oflow_info != NULL) {
3718
612
          *oflow_info = *str == '-' ? -1 : 1;
3719
612
        }
3720
3721
8.75k
        return IS_DOUBLE;
3722
8.75k
      }
3723
17.4k
    }
3724
3725
201k
    if (lval) {
3726
186k
      if (neg) {
3727
26.4k
        tmp_lval = -tmp_lval;
3728
26.4k
      }
3729
186k
      *lval = (zend_long) tmp_lval;
3730
186k
    }
3731
3732
201k
    return IS_LONG;
3733
209k
  } else {
3734
80.7k
    if (dval) {
3735
60.5k
      *dval = local_dval;
3736
60.5k
    }
3737
3738
80.7k
    return IS_DOUBLE;
3739
80.7k
  }
3740
290k
}
3741
/* }}} */
3742
3743
/*
3744
 * String matching - Sunday algorithm
3745
 * http://www.iti.fh-flensburg.de/lang/algorithmen/pattern/sundayen.htm
3746
 */
3747
89
static zend_always_inline void zend_memnstr_ex_pre(unsigned int td[], const char *needle, size_t needle_len, int reverse) /* {{{ */ {
3748
89
  int i;
3749
3750
22.8k
  for (i = 0; i < 256; i++) {
3751
22.7k
    td[i] = needle_len + 1;
3752
22.7k
  }
3753
3754
89
  if (reverse) {
3755
0
    for (i = needle_len - 1; i >= 0; i--) {
3756
0
      td[(unsigned char)needle[i]] = i + 1;
3757
0
    }
3758
89
  } else {
3759
89
    size_t i;
3760
3761
9.99k
    for (i = 0; i < needle_len; i++) {
3762
9.90k
      td[(unsigned char)needle[i]] = (int)needle_len - i;
3763
9.90k
    }
3764
89
  }
3765
89
}
3766
/* }}} */
3767
3768
ZEND_API const char* ZEND_FASTCALL zend_memnstr_ex(const char *haystack, const char *needle, size_t needle_len, const char *end) /* {{{ */
3769
89
{
3770
89
  unsigned int td[256];
3771
89
  size_t i;
3772
89
  const char *p;
3773
3774
89
  if (needle_len == 0 || (end - haystack) < needle_len) {
3775
0
    return NULL;
3776
0
  }
3777
3778
89
  zend_memnstr_ex_pre(td, needle, needle_len, 0);
3779
3780
89
  p = haystack;
3781
89
  end -= needle_len;
3782
3783
2.42k
  while (p <= end) {
3784
2.48k
    for (i = 0; i < needle_len; i++) {
3785
2.48k
      if (needle[i] != p[i]) {
3786
2.34k
        break;
3787
2.34k
      }
3788
2.48k
    }
3789
2.34k
    if (i == needle_len) {
3790
0
      return p;
3791
0
    }
3792
2.34k
    if (UNEXPECTED(p == end)) {
3793
3
      return NULL;
3794
3
    }
3795
2.34k
    p += td[(unsigned char)(p[needle_len])];
3796
2.34k
  }
3797
3798
86
  return NULL;
3799
89
}
3800
/* }}} */
3801
3802
ZEND_API const char* ZEND_FASTCALL zend_memnrstr_ex(const char *haystack, const char *needle, size_t needle_len, const char *end) /* {{{ */
3803
0
{
3804
0
  unsigned int td[256];
3805
0
  size_t i;
3806
0
  const char *p;
3807
3808
0
  if (needle_len == 0 || (end - haystack) < needle_len) {
3809
0
    return NULL;
3810
0
  }
3811
3812
0
  zend_memnstr_ex_pre(td, needle, needle_len, 1);
3813
3814
0
  p = end;
3815
0
  p -= needle_len;
3816
3817
0
  while (p >= haystack) {
3818
0
    for (i = 0; i < needle_len; i++) {
3819
0
      if (needle[i] != p[i]) {
3820
0
        break;
3821
0
      }
3822
0
    }
3823
3824
0
    if (i == needle_len) {
3825
0
      return (const char *)p;
3826
0
    }
3827
3828
0
    if (UNEXPECTED(p == haystack)) {
3829
0
      return NULL;
3830
0
    }
3831
3832
0
    p -= td[(unsigned char)(p[-1])];
3833
0
  }
3834
3835
0
  return NULL;
3836
0
}
3837
/* }}} */
3838
3839
#if SIZEOF_ZEND_LONG == 4
3840
ZEND_API zend_long ZEND_FASTCALL zend_dval_to_lval_slow(double d) /* {{{ */
3841
{
3842
  double  two_pow_32 = pow(2., 32.),
3843
      dmod;
3844
3845
  dmod = fmod(d, two_pow_32);
3846
  if (dmod < 0) {
3847
    /* we're going to make this number positive; call ceil()
3848
     * to simulate rounding towards 0 of the negative number */
3849
    dmod = ceil(dmod) + two_pow_32;
3850
  }
3851
  return (zend_long)(zend_ulong)dmod;
3852
}
3853
#else
3854
ZEND_API zend_long ZEND_FASTCALL zend_dval_to_lval_slow(double d)
3855
55.0k
{
3856
55.0k
  double  two_pow_64 = pow(2., 64.),
3857
55.0k
      dmod;
3858
3859
55.0k
  dmod = fmod(d, two_pow_64);
3860
55.0k
  if (dmod < 0) {
3861
    /* no need to call ceil; original double must have had no
3862
     * fractional part, hence dmod does not have one either */
3863
6.16k
    dmod += two_pow_64;
3864
6.16k
  }
3865
55.0k
  return (zend_long)(zend_ulong)dmod;
3866
55.0k
}
3867
/* }}} */
3868
#endif