Coverage Report

Created: 2025-09-27 06:26

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