Coverage Report

Created: 2025-12-31 07:28

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
494k
#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
13.3M
  const __m128i blconv_offset = _mm_set1_epi8((signed char)(SCHAR_MIN - start)); \
93
13.3M
  const __m128i blconv_threshold = _mm_set1_epi8(SCHAR_MIN + (end - start) + 1);
94
95
72.0M
#define BLOCKCONV_STRIDE sizeof(__m128i)
96
97
#define BLOCKCONV_INIT_DELTA(delta) \
98
6.22M
  const __m128i blconv_delta = _mm_set1_epi8(delta);
99
100
#define BLOCKCONV_LOAD(input) \
101
20.5M
  __m128i blconv_operand = _mm_loadu_si128((__m128i*)(input)); \
102
20.5M
  __m128i blconv_mask = _mm_cmplt_epi8(_mm_add_epi8(blconv_operand, blconv_offset), blconv_threshold);
103
104
18.9M
#define BLOCKCONV_FOUND() _mm_movemask_epi8(blconv_mask)
105
106
#define BLOCKCONV_STORE(dest) \
107
6.51M
  __m128i blconv_add = _mm_and_si128(blconv_mask, blconv_delta); \
108
6.51M
  __m128i blconv_result = _mm_add_epi8(blconv_operand, blconv_add); \
109
6.51M
  _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
337
  ZVAL_UNDEF(dst);                                    \
234
337
  if (Z_OBJ_HT_P(op)->cast_object(Z_OBJ_P(op), dst, ctype) == FAILURE) {         \
235
337
    zend_error(E_WARNING,                               \
236
337
      "Object of class %s could not be converted to %s", ZSTR_VAL(Z_OBJCE_P(op)->name),\
237
337
    zend_get_type_by_const(ctype));                           \
238
337
  }                                            \
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
4.45k
{
294
4.45k
  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
1.88k
    case IS_STRING:
303
1.88k
      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
1.05k
        ZVAL_LONG(holder, 0);
305
1.05k
      }
306
1.88k
      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
341
    case IS_LONG:
318
342
    case IS_DOUBLE:
319
2.57k
    default:
320
2.57k
      return op;
321
4.45k
  }
322
4.45k
}
323
/* }}} */
324
325
static zend_never_inline zend_result ZEND_FASTCALL _zendi_try_convert_scalar_to_number(zval *op, zval *holder) /* {{{ */
326
60.7k
{
327
60.7k
  switch (Z_TYPE_P(op)) {
328
28.9k
    case IS_NULL:
329
36.8k
    case IS_FALSE:
330
36.8k
      ZVAL_LONG(holder, 0);
331
36.8k
      return SUCCESS;
332
4.16k
    case IS_TRUE:
333
4.16k
      ZVAL_LONG(holder, 1);
334
4.16k
      return SUCCESS;
335
19.6k
    case IS_STRING:
336
19.6k
    {
337
19.6k
      bool trailing_data = false;
338
      /* For BC reasons we allow errors so that we can warn on leading numeric string */
339
19.6k
      if (0 == (Z_TYPE_INFO_P(holder) = is_numeric_string_ex(Z_STRVAL_P(op), Z_STRLEN_P(op),
340
19.6k
          &Z_LVAL_P(holder), &Z_DVAL_P(holder),  /* allow errors */ true, NULL, &trailing_data))) {
341
        /* Will lead to invalid OP type error */
342
230
        return FAILURE;
343
230
      }
344
19.4k
      if (UNEXPECTED(trailing_data)) {
345
8.48k
        zend_error(E_WARNING, "A non-numeric value encountered");
346
8.48k
        if (UNEXPECTED(EG(exception))) {
347
0
          return FAILURE;
348
0
        }
349
8.48k
      }
350
19.4k
      return SUCCESS;
351
19.4k
    }
352
46
    case IS_OBJECT:
353
46
      if (Z_OBJ_HT_P(op)->cast_object(Z_OBJ_P(op), holder, _IS_NUMBER) == FAILURE
354
46
          || EG(exception)) {
355
46
        return FAILURE;
356
46
      }
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
25
    case IS_ARRAY:
361
25
      return FAILURE;
362
60.7k
    EMPTY_SWITCH_DEFAULT_CASE()
363
60.7k
  }
364
60.7k
}
365
/* }}} */
366
367
static zend_always_inline zend_result zendi_try_convert_scalar_to_number(zval *op, zval *holder) /* {{{ */
368
108k
{
369
108k
  if (Z_TYPE_P(op) == IS_LONG || Z_TYPE_P(op) == IS_DOUBLE) {
370
47.6k
    ZVAL_COPY_VALUE(holder, op);
371
47.6k
    return SUCCESS;
372
60.7k
  } else {
373
60.7k
    return _zendi_try_convert_scalar_to_number(op, holder);
374
60.7k
  }
375
108k
}
376
/* }}} */
377
378
static zend_never_inline zend_long ZEND_FASTCALL zendi_try_get_long(const zval *op, bool *failed) /* {{{ */
379
32.9k
{
380
32.9k
  *failed = false;
381
32.9k
try_again:
382
32.9k
  switch (Z_TYPE_P(op)) {
383
3.73k
    case IS_NULL:
384
9.04k
    case IS_FALSE:
385
9.04k
      return 0;
386
1.75k
    case IS_TRUE:
387
1.75k
      return 1;
388
15.0k
    case IS_DOUBLE: {
389
15.0k
      double dval = Z_DVAL_P(op);
390
15.0k
      zend_long lval = zend_dval_to_lval_safe(dval);
391
15.0k
      if (UNEXPECTED(EG(exception))) {
392
0
        *failed = true;
393
0
      }
394
15.0k
      return lval;
395
3.73k
    }
396
7.05k
    case IS_STRING:
397
7.05k
      {
398
7.05k
        uint8_t type;
399
7.05k
        zend_long lval;
400
7.05k
        double dval;
401
7.05k
        bool trailing_data = false;
402
7.05k
        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
7.05k
        type = is_numeric_string_ex(Z_STRVAL_P(op), Z_STRLEN_P(op), &lval, &dval,
406
7.05k
          /* allow errors */ true, NULL, &trailing_data);
407
7.05k
        if (type == 0) {
408
1.05k
          *failed = true;
409
1.05k
          return 0;
410
1.05k
        }
411
6.00k
        if (UNEXPECTED(trailing_data)) {
412
1.84k
          if (type != IS_LONG) {
413
580
            op_str = zend_string_copy(Z_STR_P(op));
414
580
          }
415
1.84k
          zend_error(E_WARNING, "A non-numeric value encountered");
416
1.84k
          if (UNEXPECTED(EG(exception))) {
417
0
            *failed = true;
418
0
            zend_tmp_string_release(op_str);
419
0
            return 0;
420
0
          }
421
1.84k
        }
422
6.00k
        if (EXPECTED(type == IS_LONG)) {
423
4.68k
          return lval;
424
4.68k
        } 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.31k
          if (op_str == NULL) {
431
            /* zend_dval_to_lval_cap() can emit a warning so always do the copy here */
432
735
            op_str = zend_string_copy(Z_STR_P(op));
433
735
          }
434
1.31k
          lval = zend_dval_to_lval_cap(dval, op_str);
435
1.31k
          if (!zend_is_long_compatible(dval, lval)) {
436
1.09k
            zend_incompatible_string_to_long_error(op_str);
437
1.09k
            if (UNEXPECTED(EG(exception))) {
438
0
              *failed = true;
439
0
            }
440
1.09k
          }
441
1.31k
          zend_string_release(op_str);
442
1.31k
          return lval;
443
1.31k
        }
444
6.00k
      }
445
23
    case IS_OBJECT:
446
23
      {
447
23
        zval dst;
448
23
        if (Z_OBJ_HT_P(op)->cast_object(Z_OBJ_P(op), &dst, IS_LONG) == FAILURE
449
23
            || EG(exception)) {
450
23
          *failed = true;
451
23
          return 0;
452
23
        }
453
0
        ZEND_ASSERT(Z_TYPE(dst) == IS_LONG);
454
0
        return Z_LVAL(dst);
455
0
      }
456
0
    case IS_RESOURCE:
457
4
    case IS_ARRAY:
458
4
      *failed = true;
459
4
      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
32.9k
    EMPTY_SWITCH_DEFAULT_CASE()
469
32.9k
  }
470
32.9k
}
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
165k
  if (UNEXPECTED(Z_TYPE_P(op1) == IS_OBJECT) \
484
165k
    && 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
221k
  if (UNEXPECTED(Z_TYPE_P(op2) == IS_OBJECT) \
492
221k
    && UNEXPECTED(Z_OBJ_HANDLER_P(op2, do_operation)) \
493
221k
    && 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
143k
  ZEND_TRY_BINARY_OP1_OBJECT_OPERATION(opcode) \
499
143k
  else \
500
143k
  ZEND_TRY_BINARY_OP2_OBJECT_OPERATION(opcode)
501
502
#define ZEND_TRY_UNARY_OBJECT_OPERATION(opcode) \
503
53.0k
  if (UNEXPECTED(Z_TYPE_P(op1) == IS_OBJECT) \
504
53.0k
    && UNEXPECTED(Z_OBJ_HANDLER_P(op1, do_operation)) \
505
53.0k
    && 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
12.9k
  do {                               \
511
12.9k
    if (UNEXPECTED(Z_TYPE_P(op1) != IS_LONG)) {           \
512
6.27k
      bool failed;                      \
513
6.27k
      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
6.27k
      ZEND_TRY_BINARY_OP1_OBJECT_OPERATION(opcode);       \
521
6.27k
      op1_lval = zendi_try_get_long(op1, &failed);        \
522
6.27k
      if (UNEXPECTED(failed)) {                 \
523
336
        zend_binop_error(sigil, op1, op2);            \
524
336
        if (result != op1) {                 \
525
315
          ZVAL_UNDEF(result);                 \
526
315
        }                            \
527
336
        return FAILURE;                     \
528
336
      }                              \
529
6.65k
    } else {                           \
530
6.65k
      op1_lval = Z_LVAL_P(op1);                  \
531
6.65k
    }                                \
532
12.9k
  } while (0);                            \
533
12.9k
  do {                               \
534
12.6k
    if (UNEXPECTED(Z_TYPE_P(op2) != IS_LONG)) {           \
535
4.53k
      bool failed;                      \
536
4.53k
      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
4.53k
      ZEND_TRY_BINARY_OP2_OBJECT_OPERATION(opcode);       \
544
4.53k
      op2_lval = zendi_try_get_long(op2, &failed);        \
545
4.53k
      if (UNEXPECTED(failed)) {                 \
546
304
        zend_binop_error(sigil, op1, op2);            \
547
304
        if (result != op1) {                 \
548
304
          ZVAL_UNDEF(result);                 \
549
304
        }                           \
550
304
        return FAILURE;                     \
551
304
      }                              \
552
8.06k
    } else {                           \
553
8.06k
      op2_lval = Z_LVAL_P(op2);                  \
554
8.06k
    }                                \
555
12.6k
  } while (0);
556
557
ZEND_API void ZEND_FASTCALL convert_to_long(zval *op) /* {{{ */
558
1
{
559
1
  zend_long tmp;
560
561
1
try_again:
562
1
  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
1
    case IS_LONG:
576
1
      break;
577
0
    case IS_DOUBLE: {
578
      /* NAN might emit a warning */
579
0
      zend_long new_value = zend_dval_to_lval(Z_DVAL_P(op));
580
0
      zval_ptr_dtor(op);
581
0
      ZVAL_LONG(op, new_value);
582
0
      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
1
    EMPTY_SWITCH_DEFAULT_CASE()
614
1
  }
615
1
}
616
/* }}} */
617
618
ZEND_API void ZEND_FASTCALL convert_to_double(zval *op) /* {{{ */
619
1.28k
{
620
1.28k
  double tmp;
621
622
1.28k
try_again:
623
1.28k
  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
1.28k
    case IS_LONG:
638
1.28k
      ZVAL_DOUBLE(op, (double) Z_LVAL_P(op));
639
1.28k
      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
1.28k
    EMPTY_SWITCH_DEFAULT_CASE()
673
1.28k
  }
674
1.28k
}
675
/* }}} */
676
677
ZEND_API void ZEND_FASTCALL convert_to_null(zval *op) /* {{{ */
678
0
{
679
0
  if (UNEXPECTED(Z_TYPE_P(op) == IS_DOUBLE && zend_isnan(Z_DVAL_P(op)))) {
680
0
    zend_nan_coerced_to_type_warning(IS_NULL);
681
0
  }
682
0
  zval_ptr_dtor(op);
683
0
  ZVAL_NULL(op);
684
0
}
685
/* }}} */
686
687
ZEND_API void ZEND_FASTCALL convert_to_boolean(zval *op) /* {{{ */
688
0
{
689
0
  bool tmp;
690
691
0
try_again:
692
0
  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
0
    case IS_DOUBLE: {
710
      /* We compute the new value before emitting the warning as the zval may change */
711
0
      bool new_value = Z_DVAL_P(op) ? true : false;
712
0
      if (UNEXPECTED(zend_isnan(Z_DVAL_P(op)))) {
713
0
        zend_nan_coerced_to_type_warning(_IS_BOOL);
714
0
        zval_ptr_dtor(op);
715
0
      }
716
0
      ZVAL_BOOL(op, new_value);
717
0
      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
0
    EMPTY_SWITCH_DEFAULT_CASE()
755
0
  }
756
0
}
757
/* }}} */
758
759
ZEND_API void ZEND_FASTCALL _convert_to_string(zval *op) /* {{{ */
760
928k
{
761
928k
try_again:
762
928k
  switch (Z_TYPE_P(op)) {
763
0
    case IS_UNDEF:
764
2.79k
    case IS_NULL:
765
3.47k
    case IS_FALSE: {
766
3.47k
      ZVAL_EMPTY_STRING(op);
767
3.47k
      break;
768
2.79k
    }
769
989
    case IS_TRUE:
770
989
      ZVAL_CHAR(op, '1');
771
989
      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
2.79k
    }
780
920k
    case IS_LONG:
781
920k
      ZVAL_STR(op, zend_long_to_str(Z_LVAL_P(op)));
782
920k
      break;
783
3.15k
    case IS_DOUBLE: {
784
      /* Casting NAN will cause a warning */
785
3.15k
      zend_string *new_value = zend_double_to_str(Z_DVAL_P(op));
786
3.15k
      zval_ptr_dtor(op);
787
3.15k
      ZVAL_NEW_STR(op, new_value);
788
3.15k
      break;
789
2.79k
    }
790
164
    case IS_ARRAY:
791
164
      zend_error(E_WARNING, "Array to string conversion");
792
164
      zval_ptr_dtor(op);
793
164
      ZVAL_INTERNED_STR(op, ZSTR_KNOWN(ZEND_STR_ARRAY_CAPITALIZED));
794
164
      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
928k
    EMPTY_SWITCH_DEFAULT_CASE()
813
928k
  }
814
928k
}
815
/* }}} */
816
817
ZEND_API bool ZEND_FASTCALL _try_convert_to_string(zval *op) /* {{{ */
818
4
{
819
4
  zend_string *str;
820
821
4
  ZEND_ASSERT(Z_TYPE_P(op) != IS_STRING);
822
4
  str = zval_try_get_string_func(op);
823
4
  if (UNEXPECTED(!str)) {
824
4
    return 0;
825
4
  }
826
0
  zval_ptr_dtor(op);
827
0
  ZVAL_STR(op, str);
828
0
  return 1;
829
4
}
830
/* }}} */
831
832
static void convert_scalar_to_array(zval *op) /* {{{ */
833
242
{
834
242
  if (UNEXPECTED(Z_TYPE_P(op) == IS_DOUBLE && zend_isnan(Z_DVAL_P(op)))) {
835
0
    zend_nan_coerced_to_type_warning(IS_ARRAY);
836
0
  }
837
242
  HashTable *ht = zend_new_array(1);
838
242
  zend_hash_index_add_new(ht, 0, op);
839
242
  ZVAL_ARR(op, ht);
840
242
}
841
/* }}} */
842
843
ZEND_API void ZEND_FASTCALL convert_to_array(zval *op) /* {{{ */
844
281
{
845
281
try_again:
846
281
  switch (Z_TYPE_P(op)) {
847
39
    case IS_ARRAY:
848
39
      break;
849
/* OBJECTS_OPTIMIZE */
850
0
    case IS_OBJECT:
851
0
      if (Z_OBJCE_P(op) == zend_ce_closure) {
852
0
        convert_scalar_to_array(op);
853
0
      } else if (ZEND_STD_BUILD_OBJECT_PROPERTIES_ARRAY_COMPATIBLE(op)) {
854
        /* Optimized version without rebuilding properties HashTable */
855
0
        HashTable *ht = zend_std_build_object_properties_array(Z_OBJ_P(op));
856
0
        OBJ_RELEASE(Z_OBJ_P(op));
857
0
        ZVAL_ARR(op, ht);
858
0
      } else {
859
0
        HashTable *obj_ht = zend_get_properties_for(op, ZEND_PROP_PURPOSE_ARRAY_CAST);
860
0
        if (obj_ht) {
861
0
          HashTable *new_obj_ht = zend_proptable_to_symtable(obj_ht,
862
0
            (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
0
          zval_ptr_dtor(op);
866
0
          ZVAL_ARR(op, new_obj_ht);
867
0
          zend_release_properties(obj_ht);
868
0
        } else {
869
0
          zval_ptr_dtor(op);
870
          /*ZVAL_EMPTY_ARRAY(op);*/
871
0
          array_init(op);
872
0
        }
873
0
      }
874
0
      break;
875
0
    case IS_NULL:
876
      /*ZVAL_EMPTY_ARRAY(op);*/
877
0
      array_init(op);
878
0
      break;
879
0
    case IS_REFERENCE:
880
0
      zend_unwrap_reference(op);
881
0
      goto try_again;
882
242
    default:
883
242
      convert_scalar_to_array(op);
884
242
      break;
885
281
  }
886
281
}
887
/* }}} */
888
889
ZEND_API void ZEND_FASTCALL convert_to_object(zval *op) /* {{{ */
890
0
{
891
0
try_again:
892
0
  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
0
    case IS_DOUBLE:
920
0
      if (UNEXPECTED(zend_isnan(Z_DVAL_P(op)))) {
921
0
        zend_nan_coerced_to_type_warning(IS_OBJECT);
922
0
      }
923
0
      ZEND_FALLTHROUGH;
924
0
    default: {
925
0
      zval tmp;
926
0
      ZVAL_COPY_VALUE(&tmp, op);
927
0
      object_init(op);
928
0
      zend_hash_add_new(Z_OBJPROP_P(op), ZSTR_KNOWN(ZEND_STR_SCALAR), &tmp);
929
0
      break;
930
0
    }
931
0
  }
932
0
}
933
/* }}} */
934
935
ZEND_API void ZEND_COLD zend_incompatible_double_to_long_error(double d)
936
7.94k
{
937
7.94k
  zend_error_unchecked(E_DEPRECATED, "Implicit conversion from float %.*H to int loses precision", -1, d);
938
7.94k
}
939
ZEND_API void ZEND_COLD zend_incompatible_string_to_long_error(const zend_string *s)
940
1.09k
{
941
1.09k
  zend_error(E_DEPRECATED, "Implicit conversion from float-string \"%s\" to int loses precision", ZSTR_VAL(s));
942
1.09k
}
943
ZEND_API void ZEND_COLD zend_oob_double_to_long_error(double d)
944
39.9k
{
945
39.9k
  zend_error_unchecked(E_WARNING, "The float %.*H is not representable as an int, cast occurred", -1, d);
946
39.9k
}
947
ZEND_API void ZEND_COLD zend_oob_string_to_long_error(const zend_string *s)
948
0
{
949
0
  zend_error_unchecked(E_WARNING, "The float-string \"%s\" is not representable as an int, cast occurred", ZSTR_VAL(s));
950
0
}
951
952
ZEND_API void ZEND_COLD zend_nan_coerced_to_type_warning(uint8_t type)
953
351
{
954
351
  zend_error(E_WARNING, "unexpected NAN value was coerced to %s", zend_get_type_by_const(type));
955
351
}
956
957
ZEND_API zend_long ZEND_FASTCALL zval_get_long_func(const zval *op, bool is_strict) /* {{{ */
958
53.3k
{
959
53.3k
try_again:
960
53.3k
  switch (Z_TYPE_P(op)) {
961
3
    case IS_UNDEF:
962
1.10k
    case IS_NULL:
963
1.91k
    case IS_FALSE:
964
1.91k
      return 0;
965
505
    case IS_TRUE:
966
505
      return 1;
967
0
    case IS_RESOURCE:
968
0
      return Z_RES_HANDLE_P(op);
969
0
    case IS_LONG:
970
0
      return Z_LVAL_P(op);
971
30.0k
    case IS_DOUBLE: {
972
30.0k
      double dval = Z_DVAL_P(op);
973
30.0k
      zend_long lval = zend_dval_to_lval(dval);
974
30.0k
      if (UNEXPECTED(is_strict)) {
975
51
        if (!zend_is_long_compatible(dval, lval)) {
976
51
          zend_incompatible_double_to_long_error(dval);
977
51
        }
978
51
      }
979
30.0k
      return lval;
980
1.10k
    }
981
20.7k
    case IS_STRING:
982
20.7k
      {
983
20.7k
        uint8_t type;
984
20.7k
        zend_long lval;
985
20.7k
        double dval;
986
20.7k
        if (0 == (type = is_numeric_string(Z_STRVAL_P(op), Z_STRLEN_P(op), &lval, &dval, true))) {
987
1.56k
          return 0;
988
19.2k
        } else if (EXPECTED(type == IS_LONG)) {
989
12.7k
          return lval;
990
12.7k
        } 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.48k
          lval = zend_dval_to_lval_cap(dval, Z_STR_P(op));
998
6.48k
          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.48k
          return lval;
1004
6.48k
        }
1005
20.7k
      }
1006
19
    case IS_ARRAY:
1007
19
      return zend_hash_num_elements(Z_ARRVAL_P(op)) ? 1 : 0;
1008
4
    case IS_OBJECT:
1009
4
      {
1010
4
        zval dst;
1011
4
        convert_object_to_type(op, &dst, IS_LONG);
1012
4
        if (Z_TYPE(dst) == IS_LONG) {
1013
0
          return Z_LVAL(dst);
1014
4
        } else {
1015
4
          return 1;
1016
4
        }
1017
4
      }
1018
0
    case IS_REFERENCE:
1019
0
      op = Z_REFVAL_P(op);
1020
0
      goto try_again;
1021
53.3k
    EMPTY_SWITCH_DEFAULT_CASE()
1022
53.3k
  }
1023
0
  return 0;
1024
53.3k
}
1025
/* }}} */
1026
1027
ZEND_API double ZEND_FASTCALL zval_get_double_func(const zval *op) /* {{{ */
1028
34.9k
{
1029
42.1k
try_again:
1030
42.1k
  switch (Z_TYPE_P(op)) {
1031
401
    case IS_NULL:
1032
1.16k
    case IS_FALSE:
1033
1.16k
      return 0.0;
1034
640
    case IS_TRUE:
1035
640
      return 1.0;
1036
0
    case IS_RESOURCE:
1037
0
      return (double) Z_RES_HANDLE_P(op);
1038
23.5k
    case IS_LONG:
1039
23.5k
      return (double) Z_LVAL_P(op);
1040
3.42k
    case IS_DOUBLE:
1041
3.42k
      return Z_DVAL_P(op);
1042
1.82k
    case IS_STRING:
1043
1.82k
      return zend_strtod(Z_STRVAL_P(op), NULL);
1044
4.02k
    case IS_ARRAY:
1045
4.02k
      return zend_hash_num_elements(Z_ARRVAL_P(op)) ? 1.0 : 0.0;
1046
333
    case IS_OBJECT:
1047
333
      {
1048
333
        zval dst;
1049
333
        convert_object_to_type(op, &dst, IS_DOUBLE);
1050
1051
333
        if (Z_TYPE(dst) == IS_DOUBLE) {
1052
0
          return Z_DVAL(dst);
1053
333
        } else {
1054
333
          return 1.0;
1055
333
        }
1056
333
      }
1057
7.21k
    case IS_REFERENCE:
1058
7.21k
      op = Z_REFVAL_P(op);
1059
7.21k
      goto try_again;
1060
42.1k
    EMPTY_SWITCH_DEFAULT_CASE()
1061
42.1k
  }
1062
0
  return 0.0;
1063
42.1k
}
1064
/* }}} */
1065
1066
static zend_always_inline zend_string* __zval_get_string_func(zval *op, bool try) /* {{{ */
1067
560k
{
1068
563k
try_again:
1069
563k
  switch (Z_TYPE_P(op)) {
1070
182k
    case IS_UNDEF:
1071
304k
    case IS_NULL:
1072
323k
    case IS_FALSE:
1073
323k
      return ZSTR_EMPTY_ALLOC();
1074
13.0k
    case IS_TRUE:
1075
13.0k
      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
53.9k
    case IS_LONG:
1079
53.9k
      return zend_long_to_str(Z_LVAL_P(op));
1080
164k
    case IS_DOUBLE:
1081
164k
      return zend_double_to_str(Z_DVAL_P(op));
1082
562
    case IS_ARRAY:
1083
562
      zend_error(E_WARNING, "Array to string conversion");
1084
562
      return (try && UNEXPECTED(EG(exception))) ?
1085
562
        NULL : ZSTR_KNOWN(ZEND_STR_ARRAY_CAPITALIZED);
1086
4.93k
    case IS_OBJECT: {
1087
4.93k
      zval tmp;
1088
4.93k
      if (Z_OBJ_HT_P(op)->cast_object(Z_OBJ_P(op), &tmp, IS_STRING) == SUCCESS) {
1089
2.15k
        return Z_STR(tmp);
1090
2.15k
      }
1091
2.78k
      if (!EG(exception)) {
1092
709
        zend_throw_error(NULL, "Object of class %s could not be converted to string", ZSTR_VAL(Z_OBJCE_P(op)->name));
1093
709
      }
1094
2.78k
      return try ? NULL : ZSTR_EMPTY_ALLOC();
1095
4.93k
    }
1096
2.74k
    case IS_REFERENCE:
1097
2.74k
      op = Z_REFVAL_P(op);
1098
2.74k
      goto try_again;
1099
329
    case IS_STRING:
1100
329
      return zend_string_copy(Z_STR_P(op));
1101
563k
    EMPTY_SWITCH_DEFAULT_CASE()
1102
563k
  }
1103
0
  return NULL;
1104
563k
}
1105
/* }}} */
1106
1107
ZEND_API zend_string* ZEND_FASTCALL zval_get_string_func(zval *op) /* {{{ */
1108
383k
{
1109
383k
  return __zval_get_string_func(op, false);
1110
383k
}
1111
/* }}} */
1112
1113
ZEND_API zend_string* ZEND_FASTCALL zval_try_get_string_func(zval *op) /* {{{ */
1114
177k
{
1115
177k
  return __zval_get_string_func(op, true);
1116
177k
}
1117
/* }}} */
1118
1119
1.38k
static ZEND_COLD zend_never_inline void ZEND_FASTCALL zend_binop_error(const char *operator, zval *op1, zval *op2) /* {{{ */ {
1120
1.38k
  if (EG(exception)) {
1121
0
    return;
1122
0
  }
1123
1124
1.38k
  zend_type_error("Unsupported operand types: %s %s %s",
1125
1.38k
    zend_zval_type_name(op1), operator, zend_zval_type_name(op2));
1126
1.38k
}
1127
/* }}} */
1128
1129
static zend_never_inline void ZEND_FASTCALL add_function_array(zval *result, zval *op1, zval *op2) /* {{{ */
1130
4.95k
{
1131
4.95k
  if (result == op1 && Z_ARR_P(op1) == Z_ARR_P(op2)) {
1132
    /* $a += $a */
1133
0
    return;
1134
0
  }
1135
4.95k
  if (result != op1) {
1136
4.94k
    ZVAL_ARR(result, zend_array_dup(Z_ARR_P(op1)));
1137
4.94k
  } else {
1138
7
    SEPARATE_ARRAY(result);
1139
7
  }
1140
4.95k
  zend_hash_merge(Z_ARRVAL_P(result), Z_ARRVAL_P(op2), zval_add_ref, 0);
1141
4.95k
}
1142
/* }}} */
1143
1144
static zend_always_inline zend_result add_function_fast(zval *result, zval *op1, zval *op2) /* {{{ */
1145
73.2k
{
1146
73.2k
  uint8_t type_pair = TYPE_PAIR(Z_TYPE_P(op1), Z_TYPE_P(op2));
1147
1148
73.2k
  if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_LONG))) {
1149
18.0k
    fast_long_add_function(result, op1, op2);
1150
18.0k
    return SUCCESS;
1151
55.1k
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_DOUBLE, IS_DOUBLE))) {
1152
3.46k
    ZVAL_DOUBLE(result, Z_DVAL_P(op1) + Z_DVAL_P(op2));
1153
3.46k
    return SUCCESS;
1154
51.7k
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_DOUBLE))) {
1155
2.55k
    ZVAL_DOUBLE(result, ((double)Z_LVAL_P(op1)) + Z_DVAL_P(op2));
1156
2.55k
    return SUCCESS;
1157
49.1k
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_DOUBLE, IS_LONG))) {
1158
3.77k
    ZVAL_DOUBLE(result, Z_DVAL_P(op1) + ((double)Z_LVAL_P(op2)));
1159
3.77k
    return SUCCESS;
1160
45.3k
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_ARRAY, IS_ARRAY))) {
1161
4.95k
    add_function_array(result, op1, op2);
1162
4.95k
    return SUCCESS;
1163
40.4k
  } else {
1164
40.4k
    return FAILURE;
1165
40.4k
  }
1166
73.2k
} /* }}} */
1167
1168
static zend_never_inline zend_result ZEND_FASTCALL add_function_slow(zval *result, zval *op1, zval *op2) /* {{{ */
1169
21.2k
{
1170
21.2k
  ZVAL_DEREF(op1);
1171
21.2k
  ZVAL_DEREF(op2);
1172
21.2k
  if (add_function_fast(result, op1, op2) == SUCCESS) {
1173
2.03k
    return SUCCESS;
1174
2.03k
  }
1175
1176
21.2k
  ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_ADD);
1177
1178
19.2k
  zval op1_copy, op2_copy;
1179
19.2k
  if (UNEXPECTED(zendi_try_convert_scalar_to_number(op1, &op1_copy) == FAILURE)
1180
19.1k
      || UNEXPECTED(zendi_try_convert_scalar_to_number(op2, &op2_copy) == FAILURE)) {
1181
78
    zend_binop_error("+", op1, op2);
1182
78
    if (result != op1) {
1183
56
      ZVAL_UNDEF(result);
1184
56
    }
1185
78
    return FAILURE;
1186
78
  }
1187
1188
19.1k
  if (result == op1) {
1189
3.16k
    zval_ptr_dtor(result);
1190
3.16k
  }
1191
1192
19.1k
  if (add_function_fast(result, &op1_copy, &op2_copy) == SUCCESS) {
1193
19.1k
    return SUCCESS;
1194
19.1k
  }
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
32.8k
{
1202
32.8k
  if (add_function_fast(result, op1, op2) == SUCCESS) {
1203
11.6k
    return SUCCESS;
1204
21.2k
  } else {
1205
21.2k
    return add_function_slow(result, op1, op2);
1206
21.2k
  }
1207
32.8k
}
1208
/* }}} */
1209
1210
static zend_always_inline zend_result sub_function_fast(zval *result, zval *op1, zval *op2) /* {{{ */
1211
29.7k
{
1212
29.7k
  uint8_t type_pair = TYPE_PAIR(Z_TYPE_P(op1), Z_TYPE_P(op2));
1213
1214
29.7k
  if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_LONG))) {
1215
6.35k
    fast_long_sub_function(result, op1, op2);
1216
6.35k
    return SUCCESS;
1217
23.4k
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_DOUBLE, IS_DOUBLE))) {
1218
2.99k
    ZVAL_DOUBLE(result, Z_DVAL_P(op1) - Z_DVAL_P(op2));
1219
2.99k
    return SUCCESS;
1220
20.4k
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_DOUBLE))) {
1221
4.96k
    ZVAL_DOUBLE(result, ((double)Z_LVAL_P(op1)) - Z_DVAL_P(op2));
1222
4.96k
    return SUCCESS;
1223
15.4k
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_DOUBLE, IS_LONG))) {
1224
2.43k
    ZVAL_DOUBLE(result, Z_DVAL_P(op1) - ((double)Z_LVAL_P(op2)));
1225
2.43k
    return SUCCESS;
1226
13.0k
  } else {
1227
13.0k
    return FAILURE;
1228
13.0k
  }
1229
29.7k
}
1230
/* }}} */
1231
1232
static zend_never_inline zend_result ZEND_FASTCALL sub_function_slow(zval *result, zval *op1, zval *op2) /* {{{ */
1233
6.57k
{
1234
6.57k
  ZVAL_DEREF(op1);
1235
6.57k
  ZVAL_DEREF(op2);
1236
6.57k
  if (sub_function_fast(result, op1, op2) == SUCCESS) {
1237
120
    return SUCCESS;
1238
120
  }
1239
1240
6.57k
  ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_SUB);
1241
1242
6.45k
  zval op1_copy, op2_copy;
1243
6.45k
  if (UNEXPECTED(zendi_try_convert_scalar_to_number(op1, &op1_copy) == FAILURE)
1244
6.38k
      || UNEXPECTED(zendi_try_convert_scalar_to_number(op2, &op2_copy) == FAILURE)) {
1245
69
    zend_binop_error("-", op1, op2);
1246
69
    if (result != op1) {
1247
57
      ZVAL_UNDEF(result);
1248
57
    }
1249
69
    return FAILURE;
1250
69
  }
1251
1252
6.38k
  if (result == op1) {
1253
1.19k
    zval_ptr_dtor(result);
1254
1.19k
  }
1255
1256
6.38k
  if (sub_function_fast(result, &op1_copy, &op2_copy) == SUCCESS) {
1257
6.38k
    return SUCCESS;
1258
6.38k
  }
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
16.8k
{
1267
16.8k
  if (sub_function_fast(result, op1, op2) == SUCCESS) {
1268
10.2k
    return SUCCESS;
1269
10.2k
  } else {
1270
6.57k
    return sub_function_slow(result, op1, op2);
1271
6.57k
  }
1272
16.8k
}
1273
/* }}} */
1274
1275
static zend_always_inline zend_result mul_function_fast(zval *result, zval *op1, zval *op2) /* {{{ */
1276
112k
{
1277
112k
  uint8_t type_pair = TYPE_PAIR(Z_TYPE_P(op1), Z_TYPE_P(op2));
1278
1279
112k
  if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_LONG))) {
1280
49.7k
    zend_long overflow;
1281
49.7k
    ZEND_SIGNED_MULTIPLY_LONG(
1282
49.7k
      Z_LVAL_P(op1), Z_LVAL_P(op2),
1283
49.7k
      Z_LVAL_P(result), Z_DVAL_P(result), overflow);
1284
49.7k
    Z_TYPE_INFO_P(result) = overflow ? IS_DOUBLE : IS_LONG;
1285
49.7k
    return SUCCESS;
1286
62.4k
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_DOUBLE, IS_DOUBLE))) {
1287
1.61k
    ZVAL_DOUBLE(result, Z_DVAL_P(op1) * Z_DVAL_P(op2));
1288
1.61k
    return SUCCESS;
1289
60.8k
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_DOUBLE))) {
1290
1.21k
    ZVAL_DOUBLE(result, ((double)Z_LVAL_P(op1)) * Z_DVAL_P(op2));
1291
1.21k
    return SUCCESS;
1292
59.5k
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_DOUBLE, IS_LONG))) {
1293
8.65k
    ZVAL_DOUBLE(result, Z_DVAL_P(op1) * ((double)Z_LVAL_P(op2)));
1294
8.65k
    return SUCCESS;
1295
50.9k
  } else {
1296
50.9k
    return FAILURE;
1297
50.9k
  }
1298
112k
}
1299
/* }}} */
1300
1301
static zend_never_inline zend_result ZEND_FASTCALL mul_function_slow(zval *result, zval *op1, zval *op2) /* {{{ */
1302
25.4k
{
1303
25.4k
  ZVAL_DEREF(op1);
1304
25.4k
  ZVAL_DEREF(op2);
1305
25.4k
  if (mul_function_fast(result, op1, op2) == SUCCESS) {
1306
15
    return SUCCESS;
1307
15
  }
1308
1309
25.4k
  ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_MUL);
1310
1311
25.4k
  zval op1_copy, op2_copy;
1312
25.4k
  if (UNEXPECTED(zendi_try_convert_scalar_to_number(op1, &op1_copy) == FAILURE)
1313
25.3k
      || UNEXPECTED(zendi_try_convert_scalar_to_number(op2, &op2_copy) == FAILURE)) {
1314
102
    zend_binop_error("*", op1, op2);
1315
102
    if (result != op1) {
1316
78
      ZVAL_UNDEF(result);
1317
78
    }
1318
102
    return FAILURE;
1319
102
  }
1320
1321
25.3k
  if (result == op1) {
1322
1.08k
    zval_ptr_dtor(result);
1323
1.08k
  }
1324
1325
25.3k
  if (mul_function_fast(result, &op1_copy, &op2_copy) == SUCCESS) {
1326
25.3k
    return SUCCESS;
1327
25.3k
  }
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
61.3k
{
1336
61.3k
  if (mul_function_fast(result, op1, op2) == SUCCESS) {
1337
35.8k
    return SUCCESS;
1338
35.8k
  } else {
1339
25.4k
    return mul_function_slow(result, op1, op2);
1340
25.4k
  }
1341
61.3k
}
1342
/* }}} */
1343
1344
static void ZEND_COLD zend_power_base_0_exponent_lt_0_error(void)
1345
0
{
1346
0
  zend_error(E_DEPRECATED, "Power of base 0 and negative exponent is deprecated");
1347
0
}
1348
1349
static double safe_pow(double base, double exponent)
1350
9.60k
{
1351
9.60k
  if (UNEXPECTED(base == 0.0 && exponent < 0.0)) {
1352
0
    zend_power_base_0_exponent_lt_0_error();
1353
0
  }
1354
1355
9.60k
  return pow(base, exponent);
1356
9.60k
}
1357
1358
static zend_result ZEND_FASTCALL pow_function_base(zval *result, zval *op1, zval *op2) /* {{{ */
1359
16.6k
{
1360
16.6k
  uint8_t type_pair = TYPE_PAIR(Z_TYPE_P(op1), Z_TYPE_P(op2));
1361
1362
16.6k
  if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_LONG))) {
1363
9.93k
    if (Z_LVAL_P(op2) >= 0) {
1364
8.66k
      zend_long l1 = 1, l2 = Z_LVAL_P(op1), i = Z_LVAL_P(op2);
1365
1366
8.66k
      if (i == 0) {
1367
2.38k
        ZVAL_LONG(result, 1L);
1368
2.38k
        return SUCCESS;
1369
6.27k
      } else if (l2 == 0) {
1370
2.13k
        ZVAL_LONG(result, 0);
1371
2.13k
        return SUCCESS;
1372
2.13k
      }
1373
1374
24.8k
      while (i >= 1) {
1375
23.2k
        zend_long overflow;
1376
23.2k
        double dval = 0.0;
1377
1378
23.2k
        if (i % 2) {
1379
9.32k
          --i;
1380
9.32k
          ZEND_SIGNED_MULTIPLY_LONG(l1, l2, l1, dval, overflow);
1381
9.32k
          if (overflow) {
1382
1.76k
            ZVAL_DOUBLE(result, dval * safe_pow(l2, i));
1383
1.76k
            return SUCCESS;
1384
1.76k
          }
1385
13.9k
        } else {
1386
13.9k
          i /= 2;
1387
13.9k
          ZEND_SIGNED_MULTIPLY_LONG(l2, l2, l2, dval, overflow);
1388
13.9k
          if (overflow) {
1389
755
            ZVAL_DOUBLE(result, (double)l1 * safe_pow(dval, i));
1390
755
            return SUCCESS;
1391
755
          }
1392
13.9k
        }
1393
23.2k
      }
1394
      /* i == 0 */
1395
1.62k
      ZVAL_LONG(result, l1);
1396
1.62k
    } else {
1397
1.27k
      ZVAL_DOUBLE(result, safe_pow((double)Z_LVAL_P(op1), (double)Z_LVAL_P(op2)));
1398
1.27k
    }
1399
2.89k
    return SUCCESS;
1400
9.93k
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_DOUBLE, IS_DOUBLE))) {
1401
1.77k
    ZVAL_DOUBLE(result, safe_pow(Z_DVAL_P(op1), Z_DVAL_P(op2)));
1402
1.77k
    return SUCCESS;
1403
4.90k
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_DOUBLE))) {
1404
3.43k
    ZVAL_DOUBLE(result, safe_pow((double)Z_LVAL_P(op1), Z_DVAL_P(op2)));
1405
3.43k
    return SUCCESS;
1406
3.43k
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_DOUBLE, IS_LONG))) {
1407
610
    ZVAL_DOUBLE(result, safe_pow(Z_DVAL_P(op1), (double)Z_LVAL_P(op2)));
1408
610
    return SUCCESS;
1409
854
  } else {
1410
854
    return FAILURE;
1411
854
  }
1412
16.6k
}
1413
/* }}} */
1414
1415
ZEND_API zend_result ZEND_FASTCALL pow_function(zval *result, zval *op1, zval *op2) /* {{{ */
1416
15.7k
{
1417
15.7k
  ZVAL_DEREF(op1);
1418
15.7k
  ZVAL_DEREF(op2);
1419
15.7k
  if (pow_function_base(result, op1, op2) == SUCCESS) {
1420
14.9k
    return SUCCESS;
1421
14.9k
  }
1422
1423
15.7k
  ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_POW);
1424
1425
854
  zval op1_copy, op2_copy;
1426
854
  if (UNEXPECTED(zendi_try_convert_scalar_to_number(op1, &op1_copy) == FAILURE)
1427
842
      || UNEXPECTED(zendi_try_convert_scalar_to_number(op2, &op2_copy) == FAILURE)) {
1428
12
    zend_binop_error("**", op1, op2);
1429
12
    if (result != op1) {
1430
6
      ZVAL_UNDEF(result);
1431
6
    }
1432
12
    return FAILURE;
1433
12
  }
1434
1435
842
  if (result == op1) {
1436
7
    zval_ptr_dtor(result);
1437
7
  }
1438
1439
842
  if (pow_function_base(result, &op1_copy, &op2_copy) == SUCCESS) {
1440
842
    return SUCCESS;
1441
842
  }
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
13.2k
{
1456
13.2k
  uint8_t type_pair = TYPE_PAIR(Z_TYPE_P(op1), Z_TYPE_P(op2));
1457
1458
13.2k
  if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_LONG))) {
1459
7.15k
    if (Z_LVAL_P(op2) == 0) {
1460
16
      return DIV_BY_ZERO;
1461
7.13k
    } else if (Z_LVAL_P(op2) == -1 && Z_LVAL_P(op1) == ZEND_LONG_MIN) {
1462
      /* Prevent overflow error/crash */
1463
108
      ZVAL_DOUBLE(result, (double) ZEND_LONG_MIN / -1);
1464
108
      return DIV_SUCCESS;
1465
108
    }
1466
7.02k
    if (Z_LVAL_P(op1) % Z_LVAL_P(op2) == 0) { /* integer */
1467
1.55k
      ZVAL_LONG(result, Z_LVAL_P(op1) / Z_LVAL_P(op2));
1468
5.47k
    } else {
1469
5.47k
      ZVAL_DOUBLE(result, ((double) Z_LVAL_P(op1)) / Z_LVAL_P(op2));
1470
5.47k
    }
1471
7.02k
    return DIV_SUCCESS;
1472
7.15k
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_DOUBLE, IS_DOUBLE))) {
1473
649
    if (Z_DVAL_P(op2) == 0) {
1474
2
      return DIV_BY_ZERO;
1475
2
    }
1476
647
    ZVAL_DOUBLE(result, Z_DVAL_P(op1) / Z_DVAL_P(op2));
1477
647
    return DIV_SUCCESS;
1478
5.42k
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_DOUBLE, IS_LONG))) {
1479
2.21k
    if (Z_LVAL_P(op2) == 0) {
1480
5
      return DIV_BY_ZERO;
1481
5
    }
1482
2.20k
    ZVAL_DOUBLE(result, Z_DVAL_P(op1) / (double)Z_LVAL_P(op2));
1483
2.20k
    return DIV_SUCCESS;
1484
3.21k
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_DOUBLE))) {
1485
819
    if (Z_DVAL_P(op2) == 0) {
1486
8
      return DIV_BY_ZERO;
1487
8
    }
1488
811
    ZVAL_DOUBLE(result, (double)Z_LVAL_P(op1) / Z_DVAL_P(op2));
1489
811
    return DIV_SUCCESS;
1490
2.39k
  } else {
1491
2.39k
    return DIV_TYPES_NOT_HANDLED;
1492
2.39k
  }
1493
13.2k
}
1494
/* }}} */
1495
1496
ZEND_API zend_result ZEND_FASTCALL div_function(zval *result, zval *op1, zval *op2) /* {{{ */
1497
10.8k
{
1498
10.8k
  ZVAL_DEREF(op1);
1499
10.8k
  ZVAL_DEREF(op2);
1500
1501
10.8k
  zend_div_status retval = div_function_base(result, op1, op2);
1502
10.8k
  if (EXPECTED(retval == DIV_SUCCESS)) {
1503
8.46k
    return SUCCESS;
1504
8.46k
  }
1505
1506
2.41k
  if (UNEXPECTED(retval == DIV_BY_ZERO)) {
1507
15
    goto div_by_zero;
1508
15
  }
1509
1510
2.41k
  ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_DIV);
1511
1512
2.39k
  zval result_copy, op1_copy, op2_copy;
1513
2.39k
  if (UNEXPECTED(zendi_try_convert_scalar_to_number(op1, &op1_copy) == FAILURE)
1514
2.36k
      || UNEXPECTED(zendi_try_convert_scalar_to_number(op2, &op2_copy) == FAILURE)) {
1515
40
    zend_binop_error("/", op1, op2);
1516
40
    if (result != op1) {
1517
27
      ZVAL_UNDEF(result);
1518
27
    }
1519
40
    return FAILURE;
1520
40
  }
1521
1522
2.35k
  retval = div_function_base(&result_copy, &op1_copy, &op2_copy);
1523
2.35k
  if (retval == DIV_SUCCESS) {
1524
2.34k
    if (result == op1) {
1525
201
      zval_ptr_dtor(result);
1526
201
    }
1527
2.34k
    ZVAL_COPY_VALUE(result, &result_copy);
1528
2.34k
    return SUCCESS;
1529
2.34k
  }
1530
1531
31
div_by_zero:
1532
31
  ZEND_ASSERT(retval == DIV_BY_ZERO && "DIV_TYPES_NOT_HANDLED should not occur here");
1533
31
  if (result != op1) {
1534
24
    ZVAL_UNDEF(result);
1535
24
  }
1536
31
  zend_throw_error(zend_ce_division_by_zero_error, "Division by zero");
1537
31
  return FAILURE;
1538
31
}
1539
/* }}} */
1540
1541
ZEND_API zend_result ZEND_FASTCALL mod_function(zval *result, zval *op1, zval *op2) /* {{{ */
1542
6.28k
{
1543
6.28k
  zend_long op1_lval, op2_lval;
1544
1545
6.28k
  convert_op1_op2_long(op1, op1_lval, op2, op2_lval, result, ZEND_MOD, "%");
1546
1547
6.24k
  if (op2_lval == 0) {
1548
    /* modulus by zero */
1549
24
    if (EG(current_execute_data) && !CG(in_compilation)) {
1550
24
      zend_throw_exception_ex(zend_ce_division_by_zero_error, 0, "Modulo by zero");
1551
24
    } else {
1552
0
      zend_error_noreturn(E_ERROR, "Modulo by zero");
1553
0
    }
1554
24
    if (op1 != result) {
1555
21
      ZVAL_UNDEF(result);
1556
21
    }
1557
24
    return FAILURE;
1558
24
  }
1559
1560
6.21k
  if (op1 == result) {
1561
8
    zval_ptr_dtor(result);
1562
8
  }
1563
1564
6.21k
  if (op2_lval == -1) {
1565
    /* Prevent overflow error/crash if op1==LONG_MIN */
1566
269
    ZVAL_LONG(result, 0);
1567
269
    return SUCCESS;
1568
269
  }
1569
1570
5.95k
  ZVAL_LONG(result, op1_lval % op2_lval);
1571
5.95k
  return SUCCESS;
1572
6.21k
}
1573
/* }}} */
1574
1575
ZEND_API zend_result ZEND_FASTCALL boolean_xor_function(zval *result, zval *op1, zval *op2) /* {{{ */
1576
8.83k
{
1577
8.83k
  int op1_val, op2_val;
1578
1579
8.83k
  do {
1580
8.83k
    if (Z_TYPE_P(op1) == IS_FALSE) {
1581
954
      op1_val = 0;
1582
7.87k
    } else if (EXPECTED(Z_TYPE_P(op1) == IS_TRUE)) {
1583
694
      op1_val = 1;
1584
7.18k
    } else {
1585
7.18k
      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.18k
      ZEND_TRY_BINARY_OP1_OBJECT_OPERATION(ZEND_BOOL_XOR);
1596
7.18k
      op1_val = zend_is_true(op1);
1597
7.18k
    }
1598
8.83k
  } while (0);
1599
8.83k
  do {
1600
8.83k
    if (Z_TYPE_P(op2) == IS_FALSE) {
1601
512
      op2_val = 0;
1602
8.32k
    } else if (EXPECTED(Z_TYPE_P(op2) == IS_TRUE)) {
1603
462
      op2_val = 1;
1604
7.85k
    } else {
1605
7.85k
      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
7.85k
      ZEND_TRY_BINARY_OP2_OBJECT_OPERATION(ZEND_BOOL_XOR);
1616
7.85k
      op2_val = zend_is_true(op2);
1617
7.85k
    }
1618
8.83k
  } while (0);
1619
1620
8.83k
  ZVAL_BOOL(result, op1_val ^ op2_val);
1621
8.83k
  return SUCCESS;
1622
8.83k
}
1623
/* }}} */
1624
1625
ZEND_API zend_result ZEND_FASTCALL boolean_not_function(zval *result, zval *op1) /* {{{ */
1626
84.8k
{
1627
84.8k
  if (Z_TYPE_P(op1) < IS_TRUE) {
1628
30.0k
    ZVAL_TRUE(result);
1629
54.8k
  } else if (EXPECTED(Z_TYPE_P(op1) == IS_TRUE)) {
1630
1.80k
    ZVAL_FALSE(result);
1631
53.0k
  } else {
1632
53.0k
    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
53.0k
    ZEND_TRY_UNARY_OBJECT_OPERATION(ZEND_BOOL_NOT);
1643
1644
53.0k
    ZVAL_BOOL(result, !zend_is_true(op1));
1645
53.0k
  }
1646
84.8k
  return SUCCESS;
1647
84.8k
}
1648
/* }}} */
1649
1650
ZEND_API zend_result ZEND_FASTCALL bitwise_not_function(zval *result, zval *op1) /* {{{ */
1651
8.13k
{
1652
8.13k
try_again:
1653
8.13k
  switch (Z_TYPE_P(op1)) {
1654
2.76k
    case IS_LONG:
1655
2.76k
      ZVAL_LONG(result, ~Z_LVAL_P(op1));
1656
2.76k
      return SUCCESS;
1657
1.47k
    case IS_DOUBLE: {
1658
1.47k
      zend_long lval = zend_dval_to_lval_safe(Z_DVAL_P(op1));
1659
1.47k
      if (EG(exception)) {
1660
0
        if (result != op1) {
1661
0
          ZVAL_UNDEF(result);
1662
0
        }
1663
0
        return FAILURE;
1664
0
      }
1665
1.47k
      ZVAL_LONG(result, ~lval);
1666
1.47k
      return SUCCESS;
1667
1.47k
    }
1668
3.86k
    case IS_STRING: {
1669
3.86k
      size_t i;
1670
1671
3.86k
      if (Z_STRLEN_P(op1) == 1) {
1672
117
        zend_uchar not = (zend_uchar) ~*Z_STRVAL_P(op1);
1673
117
        ZVAL_CHAR(result, not);
1674
3.75k
      } else {
1675
3.75k
        ZVAL_NEW_STR(result, zend_string_alloc(Z_STRLEN_P(op1), 0));
1676
20.0M
        for (i = 0; i < Z_STRLEN_P(op1); i++) {
1677
20.0M
          Z_STRVAL_P(result)[i] = ~Z_STRVAL_P(op1)[i];
1678
20.0M
        }
1679
3.75k
        Z_STRVAL_P(result)[i] = 0;
1680
3.75k
      }
1681
3.86k
      return SUCCESS;
1682
1.47k
    }
1683
1
    case IS_REFERENCE:
1684
1
      op1 = Z_REFVAL_P(op1);
1685
1
      goto try_again;
1686
29
    default:
1687
29
      ZEND_TRY_UNARY_OBJECT_OPERATION(ZEND_BW_NOT);
1688
1689
29
      if (result != op1) {
1690
29
        ZVAL_UNDEF(result);
1691
29
      }
1692
29
      zend_type_error("Cannot perform bitwise not on %s", zend_zval_value_name(op1));
1693
29
      return FAILURE;
1694
8.13k
  }
1695
8.13k
}
1696
/* }}} */
1697
1698
ZEND_API zend_result ZEND_FASTCALL bitwise_or_function(zval *result, zval *op1, zval *op2) /* {{{ */
1699
13.9k
{
1700
13.9k
  zend_long op1_lval, op2_lval;
1701
1702
13.9k
  if (EXPECTED(Z_TYPE_P(op1) == IS_LONG) && EXPECTED(Z_TYPE_P(op2) == IS_LONG)) {
1703
935
    ZVAL_LONG(result, Z_LVAL_P(op1) | Z_LVAL_P(op2));
1704
935
    return SUCCESS;
1705
935
  }
1706
1707
13.0k
  ZVAL_DEREF(op1);
1708
13.0k
  ZVAL_DEREF(op2);
1709
1710
13.0k
  if (Z_TYPE_P(op1) == IS_STRING && EXPECTED(Z_TYPE_P(op2) == IS_STRING)) {
1711
10.6k
    zval *longer, *shorter;
1712
10.6k
    zend_string *str;
1713
10.6k
    size_t i;
1714
1715
10.6k
    if (EXPECTED(Z_STRLEN_P(op1) >= Z_STRLEN_P(op2))) {
1716
7.27k
      if (EXPECTED(Z_STRLEN_P(op1) == Z_STRLEN_P(op2)) && Z_STRLEN_P(op1) == 1) {
1717
90
        zend_uchar or = (zend_uchar) (*Z_STRVAL_P(op1) | *Z_STRVAL_P(op2));
1718
90
        if (result==op1) {
1719
0
          zval_ptr_dtor_str(result);
1720
0
        }
1721
90
        ZVAL_CHAR(result, or);
1722
90
        return SUCCESS;
1723
90
      }
1724
7.18k
      longer = op1;
1725
7.18k
      shorter = op2;
1726
7.18k
    } else {
1727
3.35k
      longer = op2;
1728
3.35k
      shorter = op1;
1729
3.35k
    }
1730
1731
10.5k
    str = zend_string_alloc(Z_STRLEN_P(longer), 0);
1732
3.11M
    for (i = 0; i < Z_STRLEN_P(shorter); i++) {
1733
3.10M
      ZSTR_VAL(str)[i] = Z_STRVAL_P(longer)[i] | Z_STRVAL_P(shorter)[i];
1734
3.10M
    }
1735
10.5k
    memcpy(ZSTR_VAL(str) + i, Z_STRVAL_P(longer) + i, Z_STRLEN_P(longer) - i + 1);
1736
10.5k
    if (result==op1) {
1737
1
      zval_ptr_dtor_str(result);
1738
1
    }
1739
10.5k
    ZVAL_NEW_STR(result, str);
1740
10.5k
    return SUCCESS;
1741
10.6k
  }
1742
1743
2.38k
  if (UNEXPECTED(Z_TYPE_P(op1) != IS_LONG)) {
1744
1.37k
    bool failed;
1745
1.37k
    ZEND_TRY_BINARY_OP1_OBJECT_OPERATION(ZEND_BW_OR);
1746
1.37k
    op1_lval = zendi_try_get_long(op1, &failed);
1747
1.37k
    if (UNEXPECTED(failed)) {
1748
164
      zend_binop_error("|", op1, op2);
1749
164
      if (result != op1) {
1750
158
        ZVAL_UNDEF(result);
1751
158
      }
1752
164
      return FAILURE;
1753
164
    }
1754
1.37k
  } else {
1755
1.01k
    op1_lval = Z_LVAL_P(op1);
1756
1.01k
  }
1757
2.22k
  if (UNEXPECTED(Z_TYPE_P(op2) != IS_LONG)) {
1758
1.14k
    bool failed;
1759
1.14k
    ZEND_TRY_BINARY_OP2_OBJECT_OPERATION(ZEND_BW_OR);
1760
1.14k
    op2_lval = zendi_try_get_long(op2, &failed);
1761
1.14k
    if (UNEXPECTED(failed)) {
1762
13
      zend_binop_error("|", op1, op2);
1763
13
      if (result != op1) {
1764
10
        ZVAL_UNDEF(result);
1765
10
      }
1766
13
      return FAILURE;
1767
13
    }
1768
1.14k
  } else {
1769
1.07k
    op2_lval = Z_LVAL_P(op2);
1770
1.07k
  }
1771
1772
2.20k
  if (op1 == result) {
1773
14
    zval_ptr_dtor(result);
1774
14
  }
1775
2.20k
  ZVAL_LONG(result, op1_lval | op2_lval);
1776
2.20k
  return SUCCESS;
1777
2.22k
}
1778
/* }}} */
1779
1780
ZEND_API zend_result ZEND_FASTCALL bitwise_and_function(zval *result, zval *op1, zval *op2) /* {{{ */
1781
189k
{
1782
189k
  zend_long op1_lval, op2_lval;
1783
1784
189k
  if (EXPECTED(Z_TYPE_P(op1) == IS_LONG) && EXPECTED(Z_TYPE_P(op2) == IS_LONG)) {
1785
2.85k
    ZVAL_LONG(result, Z_LVAL_P(op1) & Z_LVAL_P(op2));
1786
2.85k
    return SUCCESS;
1787
2.85k
  }
1788
1789
186k
  ZVAL_DEREF(op1);
1790
186k
  ZVAL_DEREF(op2);
1791
1792
186k
  if (Z_TYPE_P(op1) == IS_STRING && Z_TYPE_P(op2) == IS_STRING) {
1793
173k
    zval *longer, *shorter;
1794
173k
    zend_string *str;
1795
173k
    size_t i;
1796
1797
173k
    if (EXPECTED(Z_STRLEN_P(op1) >= Z_STRLEN_P(op2))) {
1798
41.6k
      if (EXPECTED(Z_STRLEN_P(op1) == Z_STRLEN_P(op2)) && Z_STRLEN_P(op1) == 1) {
1799
92
        zend_uchar and = (zend_uchar) (*Z_STRVAL_P(op1) & *Z_STRVAL_P(op2));
1800
92
        if (result==op1) {
1801
17
          zval_ptr_dtor_str(result);
1802
17
        }
1803
92
        ZVAL_CHAR(result, and);
1804
92
        return SUCCESS;
1805
92
      }
1806
41.5k
      longer = op1;
1807
41.5k
      shorter = op2;
1808
131k
    } else {
1809
131k
      longer = op2;
1810
131k
      shorter = op1;
1811
131k
    }
1812
1813
173k
    str = zend_string_alloc(Z_STRLEN_P(shorter), 0);
1814
1.60G
    for (i = 0; i < Z_STRLEN_P(shorter); i++) {
1815
1.60G
      ZSTR_VAL(str)[i] = Z_STRVAL_P(shorter)[i] & Z_STRVAL_P(longer)[i];
1816
1.60G
    }
1817
173k
    ZSTR_VAL(str)[i] = 0;
1818
173k
    if (result==op1) {
1819
120k
      zval_ptr_dtor_str(result);
1820
120k
    }
1821
173k
    ZVAL_NEW_STR(result, str);
1822
173k
    return SUCCESS;
1823
173k
  }
1824
1825
12.8k
  if (UNEXPECTED(Z_TYPE_P(op1) != IS_LONG)) {
1826
7.32k
    bool failed;
1827
7.32k
    ZEND_TRY_BINARY_OP1_OBJECT_OPERATION(ZEND_BW_AND);
1828
7.32k
    op1_lval = zendi_try_get_long(op1, &failed);
1829
7.32k
    if (UNEXPECTED(failed)) {
1830
16
      zend_binop_error("&", op1, op2);
1831
16
      if (result != op1) {
1832
7
        ZVAL_UNDEF(result);
1833
7
      }
1834
16
      return FAILURE;
1835
16
    }
1836
7.32k
  } else {
1837
5.53k
    op1_lval = Z_LVAL_P(op1);
1838
5.53k
  }
1839
12.8k
  if (UNEXPECTED(Z_TYPE_P(op2) != IS_LONG)) {
1840
8.54k
    bool failed;
1841
8.54k
    ZEND_TRY_BINARY_OP2_OBJECT_OPERATION(ZEND_BW_AND);
1842
8.54k
    op2_lval = zendi_try_get_long(op2, &failed);
1843
8.54k
    if (UNEXPECTED(failed)) {
1844
233
      zend_binop_error("&", op1, op2);
1845
233
      if (result != op1) {
1846
214
        ZVAL_UNDEF(result);
1847
214
      }
1848
233
      return FAILURE;
1849
233
    }
1850
8.54k
  } else {
1851
4.29k
    op2_lval = Z_LVAL_P(op2);
1852
4.29k
  }
1853
1854
12.6k
  if (op1 == result) {
1855
4.20k
    zval_ptr_dtor(result);
1856
4.20k
  }
1857
12.6k
  ZVAL_LONG(result, op1_lval & op2_lval);
1858
12.6k
  return SUCCESS;
1859
12.8k
}
1860
/* }}} */
1861
1862
ZEND_API zend_result ZEND_FASTCALL bitwise_xor_function(zval *result, zval *op1, zval *op2) /* {{{ */
1863
40.9k
{
1864
40.9k
  zend_long op1_lval, op2_lval;
1865
1866
40.9k
  if (EXPECTED(Z_TYPE_P(op1) == IS_LONG) && EXPECTED(Z_TYPE_P(op2) == IS_LONG)) {
1867
920
    ZVAL_LONG(result, Z_LVAL_P(op1) ^ Z_LVAL_P(op2));
1868
920
    return SUCCESS;
1869
920
  }
1870
1871
39.9k
  ZVAL_DEREF(op1);
1872
39.9k
  ZVAL_DEREF(op2);
1873
1874
39.9k
  if (Z_TYPE_P(op1) == IS_STRING && Z_TYPE_P(op2) == IS_STRING) {
1875
36.3k
    zval *longer, *shorter;
1876
36.3k
    zend_string *str;
1877
36.3k
    size_t i;
1878
1879
36.3k
    if (EXPECTED(Z_STRLEN_P(op1) >= Z_STRLEN_P(op2))) {
1880
26.5k
      if (EXPECTED(Z_STRLEN_P(op1) == Z_STRLEN_P(op2)) && Z_STRLEN_P(op1) == 1) {
1881
81
        zend_uchar xor = (zend_uchar) (*Z_STRVAL_P(op1) ^ *Z_STRVAL_P(op2));
1882
81
        if (result==op1) {
1883
0
          zval_ptr_dtor_str(result);
1884
0
        }
1885
81
        ZVAL_CHAR(result, xor);
1886
81
        return SUCCESS;
1887
81
      }
1888
26.4k
      longer = op1;
1889
26.4k
      shorter = op2;
1890
26.4k
    } else {
1891
9.74k
      longer = op2;
1892
9.74k
      shorter = op1;
1893
9.74k
    }
1894
1895
36.2k
    str = zend_string_alloc(Z_STRLEN_P(shorter), 0);
1896
145M
    for (i = 0; i < Z_STRLEN_P(shorter); i++) {
1897
145M
      ZSTR_VAL(str)[i] = Z_STRVAL_P(shorter)[i] ^ Z_STRVAL_P(longer)[i];
1898
145M
    }
1899
36.2k
    ZSTR_VAL(str)[i] = 0;
1900
36.2k
    if (result==op1) {
1901
0
      zval_ptr_dtor_str(result);
1902
0
    }
1903
36.2k
    ZVAL_NEW_STR(result, str);
1904
36.2k
    return SUCCESS;
1905
36.3k
  }
1906
1907
3.67k
  if (UNEXPECTED(Z_TYPE_P(op1) != IS_LONG)) {
1908
546
    bool failed;
1909
546
    ZEND_TRY_BINARY_OP1_OBJECT_OPERATION(ZEND_BW_XOR);
1910
546
    op1_lval = zendi_try_get_long(op1, &failed);
1911
546
    if (UNEXPECTED(failed)) {
1912
10
      zend_binop_error("^", op1, op2);
1913
10
      if (result != op1) {
1914
4
        ZVAL_UNDEF(result);
1915
4
      }
1916
10
      return FAILURE;
1917
10
    }
1918
3.12k
  } else {
1919
3.12k
    op1_lval = Z_LVAL_P(op1);
1920
3.12k
  }
1921
3.66k
  if (UNEXPECTED(Z_TYPE_P(op2) != IS_LONG)) {
1922
3.22k
    bool failed;
1923
3.22k
    ZEND_TRY_BINARY_OP2_OBJECT_OPERATION(ZEND_BW_XOR);
1924
3.22k
    op2_lval = zendi_try_get_long(op2, &failed);
1925
3.22k
    if (UNEXPECTED(failed)) {
1926
6
      zend_binop_error("^", op1, op2);
1927
6
      if (result != op1) {
1928
3
        ZVAL_UNDEF(result);
1929
3
      }
1930
6
      return FAILURE;
1931
6
    }
1932
3.22k
  } else {
1933
442
    op2_lval = Z_LVAL_P(op2);
1934
442
  }
1935
1936
3.65k
  if (op1 == result) {
1937
6
    zval_ptr_dtor(result);
1938
6
  }
1939
3.65k
  ZVAL_LONG(result, op1_lval ^ op2_lval);
1940
3.65k
  return SUCCESS;
1941
3.66k
}
1942
/* }}} */
1943
1944
ZEND_API zend_result ZEND_FASTCALL shift_left_function(zval *result, zval *op1, zval *op2) /* {{{ */
1945
3.34k
{
1946
3.34k
  zend_long op1_lval, op2_lval;
1947
1948
3.34k
  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
2.77k
  if (UNEXPECTED((zend_ulong)op2_lval >= SIZEOF_ZEND_LONG * 8)) {
1952
351
    if (EXPECTED(op2_lval > 0)) {
1953
344
      if (op1 == result) {
1954
0
        zval_ptr_dtor(result);
1955
0
      }
1956
344
      ZVAL_LONG(result, 0);
1957
344
      return SUCCESS;
1958
344
    } else {
1959
7
      if (EG(current_execute_data) && !CG(in_compilation)) {
1960
7
        zend_throw_exception_ex(zend_ce_arithmetic_error, 0, "Bit shift by negative number");
1961
7
      } else {
1962
0
        zend_error_noreturn(E_ERROR, "Bit shift by negative number");
1963
0
      }
1964
7
      if (op1 != result) {
1965
4
        ZVAL_UNDEF(result);
1966
4
      }
1967
7
      return FAILURE;
1968
7
    }
1969
351
  }
1970
1971
2.42k
  if (op1 == result) {
1972
3
    zval_ptr_dtor(result);
1973
3
  }
1974
1975
  /* Perform shift on unsigned numbers to get well-defined wrap behavior. */
1976
2.42k
  ZVAL_LONG(result, (zend_long) ((zend_ulong) op1_lval << op2_lval));
1977
2.42k
  return SUCCESS;
1978
2.77k
}
1979
/* }}} */
1980
1981
ZEND_API zend_result ZEND_FASTCALL shift_right_function(zval *result, zval *op1, zval *op2) /* {{{ */
1982
3.30k
{
1983
3.30k
  zend_long op1_lval, op2_lval;
1984
1985
3.30k
  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
3.28k
  if (UNEXPECTED((zend_ulong)op2_lval >= SIZEOF_ZEND_LONG * 8)) {
1989
991
    if (EXPECTED(op2_lval > 0)) {
1990
972
      if (op1 == result) {
1991
0
        zval_ptr_dtor(result);
1992
0
      }
1993
972
      ZVAL_LONG(result, (op1_lval < 0) ? -1 : 0);
1994
972
      return SUCCESS;
1995
972
    } else {
1996
19
      if (EG(current_execute_data) && !CG(in_compilation)) {
1997
19
        zend_throw_exception_ex(zend_ce_arithmetic_error, 0, "Bit shift by negative number");
1998
19
      } else {
1999
0
        zend_error_noreturn(E_ERROR, "Bit shift by negative number");
2000
0
      }
2001
19
      if (op1 != result) {
2002
16
        ZVAL_UNDEF(result);
2003
16
      }
2004
19
      return FAILURE;
2005
19
    }
2006
991
  }
2007
2008
2.29k
  if (op1 == result) {
2009
1.39k
    zval_ptr_dtor(result);
2010
1.39k
  }
2011
2012
2.29k
  ZVAL_LONG(result, op1_lval >> op2_lval);
2013
2.29k
  return SUCCESS;
2014
3.28k
}
2015
/* }}} */
2016
2017
ZEND_API zend_result ZEND_FASTCALL concat_function(zval *result, zval *op1, zval *op2) /* {{{ */
2018
777k
{
2019
777k
  zval *orig_op1 = op1;
2020
777k
  zend_string *op1_string, *op2_string;
2021
777k
  bool free_op1_string = false;
2022
777k
  bool free_op2_string = false;
2023
2024
777k
  do {
2025
777k
    if (EXPECTED(Z_TYPE_P(op1) == IS_STRING)) {
2026
688k
      op1_string = Z_STR_P(op1);
2027
688k
    } else {
2028
88.9k
      if (Z_ISREF_P(op1)) {
2029
46
        op1 = Z_REFVAL_P(op1);
2030
46
        if (Z_TYPE_P(op1) == IS_STRING) {
2031
12
          op1_string = Z_STR_P(op1);
2032
12
          break;
2033
12
        }
2034
46
      }
2035
88.9k
      ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_CONCAT);
2036
88.8k
      op1_string = zval_try_get_string_func(op1);
2037
88.8k
      if (UNEXPECTED(!op1_string)) {
2038
17
        if (orig_op1 != result) {
2039
10
          ZVAL_UNDEF(result);
2040
10
        }
2041
17
        return FAILURE;
2042
17
      }
2043
88.8k
      free_op1_string = true;
2044
88.8k
      if (result == op1) {
2045
68.4k
        if (UNEXPECTED(op1 == op2)) {
2046
2
          op2_string = op1_string;
2047
2
          goto has_op2_string;
2048
2
        }
2049
68.4k
      }
2050
88.8k
    }
2051
777k
  } while (0);
2052
777k
  do {
2053
777k
    if (EXPECTED(Z_TYPE_P(op2) == IS_STRING)) {
2054
724k
      op2_string = Z_STR_P(op2);
2055
724k
    } else {
2056
52.5k
      if (Z_ISREF_P(op2)) {
2057
5
        op2 = Z_REFVAL_P(op2);
2058
5
        if (Z_TYPE_P(op2) == IS_STRING) {
2059
2
          op2_string = Z_STR_P(op2);
2060
2
          break;
2061
2
        }
2062
5
      }
2063
      /* hold an additional reference because a userland function could free this */
2064
52.5k
      if (!free_op1_string) {
2065
29.4k
        op1_string = zend_string_copy(op1_string);
2066
29.4k
        free_op1_string = true;
2067
29.4k
      }
2068
52.5k
      ZEND_TRY_BINARY_OP2_OBJECT_OPERATION(ZEND_CONCAT);
2069
52.5k
      op2_string = zval_try_get_string_func(op2);
2070
52.5k
      if (UNEXPECTED(!op2_string)) {
2071
18
        zend_string_release_ex(op1_string, false);
2072
18
        if (orig_op1 != result) {
2073
9
          ZVAL_UNDEF(result);
2074
9
        }
2075
18
        return FAILURE;
2076
18
      }
2077
52.5k
      free_op2_string = true;
2078
52.5k
    }
2079
777k
  } while (0);
2080
2081
777k
has_op2_string:;
2082
777k
  if (UNEXPECTED(ZSTR_LEN(op1_string) == 0)) {
2083
80.0k
    if (EXPECTED(result != op2 || Z_TYPE_P(result) != IS_STRING)) {
2084
79.9k
      if (result == orig_op1) {
2085
70.4k
        i_zval_ptr_dtor(result);
2086
70.4k
      }
2087
79.9k
      if (free_op2_string) {
2088
        /* transfer ownership of op2_string */
2089
16.6k
        ZVAL_STR(result, op2_string);
2090
16.6k
        free_op2_string = false;
2091
63.3k
      } else {
2092
63.3k
        ZVAL_STR_COPY(result, op2_string);
2093
63.3k
      }
2094
79.9k
    }
2095
697k
  } else if (UNEXPECTED(ZSTR_LEN(op2_string) == 0)) {
2096
17.4k
    if (EXPECTED(result != op1 || Z_TYPE_P(result) != IS_STRING)) {
2097
6.71k
      if (result == orig_op1) {
2098
1.04k
        i_zval_ptr_dtor(result);
2099
1.04k
      }
2100
6.71k
      if (free_op1_string) {
2101
        /* transfer ownership of op1_string */
2102
6.16k
        ZVAL_STR(result, op1_string);
2103
6.16k
        free_op1_string = false;
2104
6.16k
      } else {
2105
548
        ZVAL_STR_COPY(result, op1_string);
2106
548
      }
2107
6.71k
    }
2108
679k
  } else {
2109
679k
    size_t op1_len = ZSTR_LEN(op1_string);
2110
679k
    size_t op2_len = ZSTR_LEN(op2_string);
2111
679k
    size_t result_len = op1_len + op2_len;
2112
679k
    zend_string *result_str;
2113
679k
    uint32_t flags = ZSTR_GET_COPYABLE_CONCAT_PROPERTIES_BOTH(op1_string, op2_string);
2114
2115
679k
    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
679k
    if (result == op1) {
2126
      /* Destroy the old result first to drop the refcount, such that $x .= ...; may happen in-place. */
2127
662k
      if (free_op1_string) {
2128
        /* op1_string will be used as the result, so we should not free it */
2129
18.3k
        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
18.3k
        ZVAL_NULL(result);
2133
18.3k
        free_op1_string = false;
2134
18.3k
      }
2135
      /* special case, perform operations on result */
2136
662k
      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
662k
      if (op1_string == op2_string) {
2139
29.3k
        if (free_op2_string) {
2140
2.26k
          zend_string_release_ex(op2_string, false);
2141
2.26k
          free_op2_string = false;
2142
2.26k
        }
2143
29.3k
        op2_string = result_str;
2144
29.3k
      }
2145
662k
    } else {
2146
16.8k
      result_str = zend_string_alloc(result_len, 0);
2147
16.8k
      memcpy(ZSTR_VAL(result_str), ZSTR_VAL(op1_string), op1_len);
2148
16.8k
      if (result == orig_op1) {
2149
0
        i_zval_ptr_dtor(result);
2150
0
      }
2151
16.8k
    }
2152
679k
    GC_ADD_FLAGS(result_str, flags);
2153
2154
679k
    ZVAL_NEW_STR(result, result_str);
2155
679k
    memcpy(ZSTR_VAL(result_str) + op1_len, ZSTR_VAL(op2_string), op2_len);
2156
679k
    ZSTR_VAL(result_str)[result_len] = '\0';
2157
679k
  }
2158
2159
777k
  if (free_op1_string) zend_string_release_ex(op1_string, false);
2160
777k
  if (free_op2_string) zend_string_release_ex(op2_string, false);
2161
2162
777k
  return SUCCESS;
2163
777k
}
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
62.5k
{
2187
62.5k
  if (EXPECTED(Z_TYPE_P(op1) == IS_STRING) &&
2188
61.6k
      EXPECTED(Z_TYPE_P(op2) == IS_STRING)) {
2189
61.4k
    if (Z_STR_P(op1) == Z_STR_P(op2)) {
2190
7.08k
      return 0;
2191
54.3k
    } else {
2192
54.3k
      return zend_binary_strcmp(Z_STRVAL_P(op1), Z_STRLEN_P(op1), Z_STRVAL_P(op2), Z_STRLEN_P(op2));
2193
54.3k
    }
2194
61.4k
  } else {
2195
1.16k
    zend_string *tmp_str1, *tmp_str2;
2196
1.16k
    zend_string *str1 = zval_get_tmp_string(op1, &tmp_str1);
2197
1.16k
    zend_string *str2 = zval_get_tmp_string(op2, &tmp_str2);
2198
1.16k
    int ret = zend_binary_strcmp(ZSTR_VAL(str1), ZSTR_LEN(str1), ZSTR_VAL(str2), ZSTR_LEN(str2));
2199
2200
1.16k
    zend_tmp_string_release(tmp_str1);
2201
1.16k
    zend_tmp_string_release(tmp_str2);
2202
1.16k
    return ret;
2203
1.16k
  }
2204
62.5k
}
2205
/* }}} */
2206
2207
ZEND_API int ZEND_FASTCALL string_case_compare_function(zval *op1, zval *op2) /* {{{ */
2208
43
{
2209
43
  if (EXPECTED(Z_TYPE_P(op1) == IS_STRING) &&
2210
42
      EXPECTED(Z_TYPE_P(op2) == IS_STRING)) {
2211
27
    if (Z_STR_P(op1) == Z_STR_P(op2)) {
2212
0
      return 0;
2213
27
    } else {
2214
27
      return zend_binary_strcasecmp(Z_STRVAL_P(op1), Z_STRLEN_P(op1), Z_STRVAL_P(op2), Z_STRLEN_P(op2));
2215
27
    }
2216
27
  } else {
2217
16
    zend_string *tmp_str1, *tmp_str2;
2218
16
    zend_string *str1 = zval_get_tmp_string(op1, &tmp_str1);
2219
16
    zend_string *str2 = zval_get_tmp_string(op2, &tmp_str2);
2220
16
    int ret = zend_binary_strcasecmp(ZSTR_VAL(str1), ZSTR_LEN(str1), ZSTR_VAL(str2), ZSTR_LEN(str2));
2221
2222
16
    zend_tmp_string_release(tmp_str1);
2223
16
    zend_tmp_string_release(tmp_str2);
2224
16
    return ret;
2225
16
  }
2226
43
}
2227
/* }}} */
2228
2229
ZEND_API int ZEND_FASTCALL string_locale_compare_function(zval *op1, zval *op2) /* {{{ */
2230
548
{
2231
548
  zend_string *tmp_str1, *tmp_str2;
2232
548
  zend_string *str1 = zval_get_tmp_string(op1, &tmp_str1);
2233
548
  zend_string *str2 = zval_get_tmp_string(op2, &tmp_str2);
2234
548
  int ret = strcoll(ZSTR_VAL(str1), ZSTR_VAL(str2));
2235
2236
548
  zend_tmp_string_release(tmp_str1);
2237
548
  zend_tmp_string_release(tmp_str2);
2238
548
  return ret;
2239
548
}
2240
/* }}} */
2241
2242
ZEND_API int ZEND_FASTCALL numeric_compare_function(zval *op1, zval *op2) /* {{{ */
2243
1
{
2244
1
  double d1, d2;
2245
2246
1
  d1 = zval_get_double(op1);
2247
1
  d2 = zval_get_double(op2);
2248
2249
1
  return ZEND_THREEWAY_COMPARE(d1, d2);
2250
1
}
2251
/* }}} */
2252
2253
ZEND_API zend_result ZEND_FASTCALL compare_function(zval *result, zval *op1, zval *op2) /* {{{ */
2254
390
{
2255
390
  ZVAL_LONG(result, zend_compare(op1, op2));
2256
390
  return SUCCESS;
2257
390
}
2258
/* }}} */
2259
2260
static int compare_long_to_string(zend_long lval, zend_string *str) /* {{{ */
2261
4.65k
{
2262
4.65k
  zend_long str_lval;
2263
4.65k
  double str_dval;
2264
4.65k
  uint8_t type = is_numeric_string(ZSTR_VAL(str), ZSTR_LEN(str), &str_lval, &str_dval, 0);
2265
2266
4.65k
  if (type == IS_LONG) {
2267
1.93k
    return lval > str_lval ? 1 : lval < str_lval ? -1 : 0;
2268
1.93k
  }
2269
2270
2.71k
  if (type == IS_DOUBLE) {
2271
224
    return ZEND_THREEWAY_COMPARE((double) lval, str_dval);
2272
224
  }
2273
2274
2.49k
  zend_string *lval_as_str = zend_long_to_str(lval);
2275
2.49k
  int cmp_result = zend_binary_strcmp(
2276
2.49k
    ZSTR_VAL(lval_as_str), ZSTR_LEN(lval_as_str), ZSTR_VAL(str), ZSTR_LEN(str));
2277
2.49k
  zend_string_release(lval_as_str);
2278
2.49k
  return ZEND_NORMALIZE_BOOL(cmp_result);
2279
2.71k
}
2280
/* }}} */
2281
2282
static int compare_double_to_string(double dval, zend_string *str) /* {{{ */
2283
7.44k
{
2284
7.44k
  zend_long str_lval;
2285
7.44k
  double str_dval;
2286
7.44k
  uint8_t type = is_numeric_string(ZSTR_VAL(str), ZSTR_LEN(str), &str_lval, &str_dval, 0);
2287
2288
7.44k
  ZEND_ASSERT(!zend_isnan(dval));
2289
2290
7.44k
  if (type == IS_LONG) {
2291
1.92k
    return ZEND_THREEWAY_COMPARE(dval, (double) str_lval);
2292
1.92k
  }
2293
2294
5.51k
  if (type == IS_DOUBLE) {
2295
3.96k
    return ZEND_THREEWAY_COMPARE(dval, str_dval);
2296
3.96k
  }
2297
2298
1.55k
  zend_string *dval_as_str = zend_double_to_str(dval);
2299
1.55k
  int cmp_result = zend_binary_strcmp(
2300
1.55k
    ZSTR_VAL(dval_as_str), ZSTR_LEN(dval_as_str), ZSTR_VAL(str), ZSTR_LEN(str));
2301
1.55k
  zend_string_release(dval_as_str);
2302
1.55k
  return ZEND_NORMALIZE_BOOL(cmp_result);
2303
5.51k
}
2304
/* }}} */
2305
2306
ZEND_API int ZEND_FASTCALL zend_compare(zval *op1, zval *op2) /* {{{ */
2307
176k
{
2308
176k
  bool converted = false;
2309
176k
  zval op1_copy, op2_copy;
2310
2311
179k
  while (1) {
2312
179k
    switch (TYPE_PAIR(Z_TYPE_P(op1), Z_TYPE_P(op2))) {
2313
5.11k
      case TYPE_PAIR(IS_LONG, IS_LONG):
2314
5.11k
        return Z_LVAL_P(op1)>Z_LVAL_P(op2)?1:(Z_LVAL_P(op1)<Z_LVAL_P(op2)?-1:0);
2315
2316
1.48k
      case TYPE_PAIR(IS_DOUBLE, IS_LONG):
2317
1.48k
        return ZEND_THREEWAY_COMPARE(Z_DVAL_P(op1), (double)Z_LVAL_P(op2));
2318
2319
1.89k
      case TYPE_PAIR(IS_LONG, IS_DOUBLE):
2320
1.89k
        return ZEND_THREEWAY_COMPARE((double)Z_LVAL_P(op1), Z_DVAL_P(op2));
2321
2322
1.09k
      case TYPE_PAIR(IS_DOUBLE, IS_DOUBLE):
2323
1.09k
        return ZEND_THREEWAY_COMPARE(Z_DVAL_P(op1), Z_DVAL_P(op2));
2324
2325
3.46k
      case TYPE_PAIR(IS_ARRAY, IS_ARRAY):
2326
3.46k
        return zend_compare_arrays(op1, op2);
2327
2328
1.46k
      case TYPE_PAIR(IS_NULL, IS_NULL):
2329
2.55k
      case TYPE_PAIR(IS_NULL, IS_FALSE):
2330
5.03k
      case TYPE_PAIR(IS_FALSE, IS_NULL):
2331
5.51k
      case TYPE_PAIR(IS_FALSE, IS_FALSE):
2332
6.22k
      case TYPE_PAIR(IS_TRUE, IS_TRUE):
2333
6.22k
        return 0;
2334
2335
217
      case TYPE_PAIR(IS_NULL, IS_TRUE):
2336
217
        return -1;
2337
2338
202
      case TYPE_PAIR(IS_TRUE, IS_NULL):
2339
202
        return 1;
2340
2341
7.08k
      case TYPE_PAIR(IS_STRING, IS_STRING):
2342
7.08k
        if (Z_STR_P(op1) == Z_STR_P(op2)) {
2343
331
          return 0;
2344
331
        }
2345
6.75k
        return zendi_smart_strcmp(Z_STR_P(op1), Z_STR_P(op2));
2346
2347
7.35k
      case TYPE_PAIR(IS_NULL, IS_STRING):
2348
7.35k
        return Z_STRLEN_P(op2) == 0 ? 0 : -1;
2349
2350
8.83k
      case TYPE_PAIR(IS_STRING, IS_NULL):
2351
8.83k
        return Z_STRLEN_P(op1) == 0 ? 0 : 1;
2352
2353
3.12k
      case TYPE_PAIR(IS_LONG, IS_STRING):
2354
3.12k
        return compare_long_to_string(Z_LVAL_P(op1), Z_STR_P(op2));
2355
2356
1.53k
      case TYPE_PAIR(IS_STRING, IS_LONG):
2357
1.53k
        return -compare_long_to_string(Z_LVAL_P(op2), Z_STR_P(op1));
2358
2359
2.71k
      case TYPE_PAIR(IS_DOUBLE, IS_STRING):
2360
2.71k
        if (zend_isnan(Z_DVAL_P(op1))) {
2361
27
          return 1;
2362
27
        }
2363
2364
2.68k
        return compare_double_to_string(Z_DVAL_P(op1), Z_STR_P(op2));
2365
2366
4.89k
      case TYPE_PAIR(IS_STRING, IS_DOUBLE):
2367
4.89k
        if (zend_isnan(Z_DVAL_P(op2))) {
2368
137
          return 1;
2369
137
        }
2370
2371
4.75k
        return -compare_double_to_string(Z_DVAL_P(op2), Z_STR_P(op1));
2372
2373
21
      case TYPE_PAIR(IS_OBJECT, IS_NULL):
2374
21
        return 1;
2375
2376
170
      case TYPE_PAIR(IS_NULL, IS_OBJECT):
2377
170
        return -1;
2378
2379
124k
      default:
2380
124k
        if (Z_ISREF_P(op1)) {
2381
294
          op1 = Z_REFVAL_P(op1);
2382
294
          continue;
2383
123k
        } else if (Z_ISREF_P(op2)) {
2384
37
          op2 = Z_REFVAL_P(op2);
2385
37
          continue;
2386
37
        }
2387
2388
123k
        if (Z_TYPE_P(op1) == IS_OBJECT
2389
123k
         || Z_TYPE_P(op2) == IS_OBJECT) {
2390
474
          zval *object, *other;
2391
474
          if (Z_TYPE_P(op1) == IS_OBJECT) {
2392
462
            object = op1;
2393
462
            other = op2;
2394
462
          } else {
2395
12
            object = op2;
2396
12
            other = op1;
2397
12
          }
2398
474
          if (EXPECTED(Z_TYPE_P(other) == IS_OBJECT)) {
2399
178
            if (Z_OBJ_P(object) == Z_OBJ_P(other)) {
2400
13
              return 0;
2401
13
            }
2402
296
          } else if (Z_TYPE_P(other) == IS_TRUE || Z_TYPE_P(other) == IS_FALSE) {
2403
1
            zval casted;
2404
1
            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
1
            int ret = object == op1 ? zend_compare(&casted, other) : zend_compare(other, &casted);
2408
1
            ZEND_ASSERT(!Z_REFCOUNTED_P(&casted));
2409
1
            return ret;
2410
1
          }
2411
460
          return Z_OBJ_HANDLER_P(object, compare)(op1, op2);
2412
474
        }
2413
2414
123k
        if (!converted) {
2415
          /* Handle NAN */
2416
121k
          if (UNEXPECTED(
2417
121k
            (Z_TYPE_P(op1) == IS_DOUBLE && zend_isnan(Z_DVAL_P(op1)))
2418
121k
            || (Z_TYPE_P(op2) == IS_DOUBLE && zend_isnan(Z_DVAL_P(op2)))
2419
121k
          )) {
2420
            // TODO: NAN should always be uncomparable
2421
            /* NAN used be cast to TRUE so handle this manually for the time being */
2422
12
            if (Z_TYPE_P(op1) < IS_TRUE) {
2423
1
              return -1;
2424
11
            } else if (Z_TYPE_P(op1) == IS_TRUE || Z_TYPE_P(op2) == IS_TRUE) {
2425
11
              return 0;
2426
11
            } else if (Z_TYPE_P(op2) < IS_TRUE) {
2427
0
              return 1;
2428
0
            } else if (Z_TYPE_P(op1) != IS_DOUBLE) {
2429
0
              op1 = _zendi_convert_scalar_to_number_silent(op1, &op1_copy);
2430
0
              converted = true;
2431
0
            } else if (Z_TYPE_P(op2) != IS_DOUBLE) {
2432
0
              op2 = _zendi_convert_scalar_to_number_silent(op2, &op2_copy);
2433
0
              converted = true;
2434
0
            }
2435
121k
          } else if (Z_TYPE_P(op1) < IS_TRUE) {
2436
110k
            return zend_is_true(op2) ? -1 : 0;
2437
110k
          } else if (Z_TYPE_P(op1) == IS_TRUE) {
2438
851
            return zend_is_true(op2) ? 0 : 1;
2439
9.27k
          } else if (Z_TYPE_P(op2) < IS_TRUE) {
2440
5.34k
            return zend_is_true(op1) ? 1 : 0;
2441
5.34k
          } else if (Z_TYPE_P(op2) == IS_TRUE) {
2442
1.69k
            return zend_is_true(op1) ? 0 : -1;
2443
2.22k
          } else {
2444
2.22k
            op1 = _zendi_convert_scalar_to_number_silent(op1, &op1_copy);
2445
2.22k
            op2 = _zendi_convert_scalar_to_number_silent(op2, &op2_copy);
2446
2.22k
            if (EG(exception)) {
2447
0
              return 1; /* to stop comparison of arrays */
2448
0
            }
2449
2.22k
            converted = true;
2450
2.22k
          }
2451
121k
        } else if (Z_TYPE_P(op1)==IS_ARRAY) {
2452
1.70k
          return 1;
2453
1.70k
        } else if (Z_TYPE_P(op2)==IS_ARRAY) {
2454
528
          return -1;
2455
528
        } else {
2456
0
          ZEND_UNREACHABLE();
2457
0
          zend_throw_error(NULL, "Unsupported operand types");
2458
0
          return 1;
2459
0
        }
2460
179k
    }
2461
179k
  }
2462
176k
}
2463
/* }}} */
2464
2465
/* return int to be compatible with compare_func_t */
2466
static int hash_zval_identical_function(zval *z1, zval *z2) /* {{{ */
2467
737
{
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
737
  ZVAL_DEREF(z1);
2474
737
  ZVAL_DEREF(z2);
2475
737
  return fast_is_not_identical_function(z1, z2);
2476
737
}
2477
/* }}} */
2478
2479
ZEND_API bool ZEND_FASTCALL zend_is_identical(const zval *op1, const zval *op2) /* {{{ */
2480
16.4k
{
2481
16.4k
  if (Z_TYPE_P(op1) != Z_TYPE_P(op2)) {
2482
674
    return 0;
2483
674
  }
2484
15.8k
  switch (Z_TYPE_P(op1)) {
2485
22
    case IS_NULL:
2486
371
    case IS_FALSE:
2487
479
    case IS_TRUE:
2488
479
      return 1;
2489
2.35k
    case IS_LONG:
2490
2.35k
      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
343
    case IS_DOUBLE:
2494
343
      return (Z_DVAL_P(op1) == Z_DVAL_P(op2));
2495
10.4k
    case IS_STRING:
2496
10.4k
      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.93k
        zend_hash_compare(Z_ARRVAL_P(op1), Z_ARRVAL_P(op2), (compare_func_t) hash_zval_identical_function, 1) == 0);
2500
23
    case IS_OBJECT:
2501
23
      return (Z_OBJ_P(op1) == Z_OBJ_P(op2));
2502
0
    default:
2503
0
      return 0;
2504
15.8k
  }
2505
15.8k
}
2506
/* }}} */
2507
2508
ZEND_API zend_result ZEND_FASTCALL is_identical_function(zval *result, zval *op1, zval *op2) /* {{{ */
2509
2.96k
{
2510
2.96k
  ZVAL_BOOL(result, zend_is_identical(op1, op2));
2511
2.96k
  return SUCCESS;
2512
2.96k
}
2513
/* }}} */
2514
2515
ZEND_API zend_result ZEND_FASTCALL is_not_identical_function(zval *result, zval *op1, zval *op2) /* {{{ */
2516
1.00k
{
2517
1.00k
  ZVAL_BOOL(result, !zend_is_identical(op1, op2));
2518
1.00k
  return SUCCESS;
2519
1.00k
}
2520
/* }}} */
2521
2522
ZEND_API zend_result ZEND_FASTCALL is_equal_function(zval *result, zval *op1, zval *op2) /* {{{ */
2523
2.74k
{
2524
2.74k
  ZVAL_BOOL(result, zend_compare(op1, op2) == 0);
2525
2.74k
  return SUCCESS;
2526
2.74k
}
2527
/* }}} */
2528
2529
ZEND_API zend_result ZEND_FASTCALL is_not_equal_function(zval *result, zval *op1, zval *op2) /* {{{ */
2530
2.56k
{
2531
2.56k
  ZVAL_BOOL(result, (zend_compare(op1, op2) != 0));
2532
2.56k
  return SUCCESS;
2533
2.56k
}
2534
/* }}} */
2535
2536
ZEND_API zend_result ZEND_FASTCALL is_smaller_function(zval *result, zval *op1, zval *op2) /* {{{ */
2537
28.4k
{
2538
28.4k
  ZVAL_BOOL(result, (zend_compare(op1, op2) < 0));
2539
28.4k
  return SUCCESS;
2540
28.4k
}
2541
/* }}} */
2542
2543
ZEND_API zend_result ZEND_FASTCALL is_smaller_or_equal_function(zval *result, zval *op1, zval *op2) /* {{{ */
2544
1.72k
{
2545
1.72k
  ZVAL_BOOL(result, (zend_compare(op1, op2) <= 0));
2546
1.72k
  return SUCCESS;
2547
1.72k
}
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
1.43k
{
2552
1.43k
  uint32_t i;
2553
1.43k
  ZEND_ASSERT(interface_ce->ce_flags & ZEND_ACC_INTERFACE);
2554
2555
1.43k
  if (class_ce->num_interfaces) {
2556
1.41k
    ZEND_ASSERT(class_ce->ce_flags & ZEND_ACC_RESOLVED_INTERFACES);
2557
5.50k
    for (i = 0; i < class_ce->num_interfaces; i++) {
2558
4.54k
      if (class_ce->interfaces[i] == interface_ce) {
2559
456
        return 1;
2560
456
      }
2561
4.54k
    }
2562
1.41k
  }
2563
983
  return 0;
2564
1.43k
}
2565
/* }}} */
2566
2567
ZEND_API bool ZEND_FASTCALL instanceof_function_slow(const zend_class_entry *instance_ce, const zend_class_entry *ce) /* {{{ */
2568
3.30M
{
2569
3.30M
  ZEND_ASSERT(instance_ce != ce && "Should have been checked already");
2570
3.30M
  if (ce->ce_flags & ZEND_ACC_INTERFACE) {
2571
581k
    uint32_t i;
2572
2573
581k
    if (instance_ce->num_interfaces) {
2574
580k
      ZEND_ASSERT(instance_ce->ce_flags & ZEND_ACC_RESOLVED_INTERFACES);
2575
1.15M
      for (i = 0; i < instance_ce->num_interfaces; i++) {
2576
1.15M
        if (instance_ce->interfaces[i] == ce) {
2577
580k
          return 1;
2578
580k
        }
2579
1.15M
      }
2580
580k
    }
2581
437
    return 0;
2582
2.72M
  } else {
2583
5.44M
    while (1) {
2584
5.44M
      instance_ce = instance_ce->parent;
2585
5.44M
      if (instance_ce == ce) {
2586
967k
        return 1;
2587
967k
      }
2588
4.47M
      if (instance_ce == NULL) {
2589
1.75M
        return 0;
2590
1.75M
      }
2591
4.47M
    }
2592
2.72M
  }
2593
3.30M
}
2594
/* }}} */
2595
2596
3
#define LOWER_CASE 1
2597
1
#define UPPER_CASE 2
2598
494
#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
500
{
2615
500
  int carry=0;
2616
500
  size_t pos=Z_STRLEN_P(str)-1;
2617
500
  char *s;
2618
500
  zend_string *t;
2619
500
  int last=0; /* Shut up the compiler warning */
2620
500
  int ch;
2621
2622
500
  zend_string *zstr = Z_STR_P(str);
2623
500
  zend_string_addref(zstr);
2624
500
  zend_error(E_DEPRECATED, "Increment on non-numeric string is deprecated, use str_increment() instead");
2625
500
  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
500
  zval_ptr_dtor(str);
2631
500
  ZVAL_STR(str, zstr);
2632
2633
500
  if (UNEXPECTED(Z_STRLEN_P(str) == 0)) {
2634
0
    zval_ptr_dtor(str);
2635
0
    ZVAL_CHAR(str, '1');
2636
0
    return true;
2637
0
  }
2638
2639
500
  if (!Z_REFCOUNTED_P(str)) {
2640
4
    Z_STR_P(str) = zend_string_init(Z_STRVAL_P(str), Z_STRLEN_P(str), 0);
2641
4
    Z_TYPE_INFO_P(str) = IS_STRING_EX;
2642
496
  } else if (Z_REFCOUNT_P(str) > 1) {
2643
    /* Only release string after allocation succeeded. */
2644
211
    zend_string *orig_str = Z_STR_P(str);
2645
211
    Z_STR_P(str) = zend_string_init(Z_STRVAL_P(str), Z_STRLEN_P(str), 0);
2646
211
    GC_DELREF(orig_str);
2647
285
  } else {
2648
285
    zend_string_forget_hash_val(Z_STR_P(str));
2649
285
  }
2650
500
  s = Z_STRVAL_P(str);
2651
2652
500
  do {
2653
500
    ch = s[pos];
2654
500
    if (ch >= 'a' && ch <= 'z') {
2655
3
      if (ch == 'z') {
2656
0
        s[pos] = 'a';
2657
0
        carry=1;
2658
3
      } else {
2659
3
        s[pos]++;
2660
3
        carry=0;
2661
3
      }
2662
3
      last=LOWER_CASE;
2663
497
    } else if (ch >= 'A' && ch <= 'Z') {
2664
1
      if (ch == 'Z') {
2665
0
        s[pos] = 'A';
2666
0
        carry=1;
2667
1
      } else {
2668
1
        s[pos]++;
2669
1
        carry=0;
2670
1
      }
2671
1
      last=UPPER_CASE;
2672
496
    } else if (ch >= '0' && ch <= '9') {
2673
494
      if (ch == '9') {
2674
0
        s[pos] = '0';
2675
0
        carry=1;
2676
494
      } else {
2677
494
        s[pos]++;
2678
494
        carry=0;
2679
494
      }
2680
494
      last = NUMERIC;
2681
494
    } else {
2682
2
      carry=0;
2683
2
      break;
2684
2
    }
2685
498
    if (carry == 0) {
2686
498
      break;
2687
498
    }
2688
498
  } while (pos-- > 0);
2689
2690
500
  if (carry) {
2691
0
    t = zend_string_alloc(Z_STRLEN_P(str)+1, 0);
2692
0
    memcpy(ZSTR_VAL(t) + 1, Z_STRVAL_P(str), Z_STRLEN_P(str));
2693
0
    ZSTR_VAL(t)[Z_STRLEN_P(str) + 1] = '\0';
2694
0
    switch (last) {
2695
0
      case NUMERIC:
2696
0
        ZSTR_VAL(t)[0] = '1';
2697
0
        break;
2698
0
      case UPPER_CASE:
2699
0
        ZSTR_VAL(t)[0] = 'A';
2700
0
        break;
2701
0
      case LOWER_CASE:
2702
0
        ZSTR_VAL(t)[0] = 'a';
2703
0
        break;
2704
0
    }
2705
0
    zend_string_free(Z_STR_P(str));
2706
0
    ZVAL_NEW_STR(str, t);
2707
0
  }
2708
500
  return true;
2709
500
}
2710
/* }}} */
2711
2712
ZEND_API zend_result ZEND_FASTCALL increment_function(zval *op1) /* {{{ */
2713
2.73k
{
2714
2.73k
try_again:
2715
2.73k
  switch (Z_TYPE_P(op1)) {
2716
502
    case IS_LONG:
2717
502
      fast_long_increment_function(op1);
2718
502
      break;
2719
674
    case IS_DOUBLE:
2720
674
      Z_DVAL_P(op1) = Z_DVAL_P(op1) + 1;
2721
674
      break;
2722
880
    case IS_NULL:
2723
880
      ZVAL_LONG(op1, 1);
2724
880
      break;
2725
671
    case IS_STRING: {
2726
671
        zend_long lval;
2727
671
        double dval;
2728
2729
671
        switch (is_numeric_str_function(Z_STR_P(op1), &lval, &dval)) {
2730
160
          case IS_LONG:
2731
160
            zval_ptr_dtor_str(op1);
2732
160
            if (lval == ZEND_LONG_MAX) {
2733
              /* switch to double */
2734
1
              double d = (double)lval;
2735
1
              ZVAL_DOUBLE(op1, d+1);
2736
159
            } else {
2737
159
              ZVAL_LONG(op1, lval+1);
2738
159
            }
2739
160
            break;
2740
11
          case IS_DOUBLE:
2741
11
            zval_ptr_dtor_str(op1);
2742
11
            ZVAL_DOUBLE(op1, dval+1);
2743
11
            break;
2744
500
          default:
2745
            /* Perl style string increment */
2746
500
            increment_string(op1);
2747
500
            if (EG(exception)) {
2748
0
              return FAILURE;
2749
0
            }
2750
500
            break;
2751
671
        }
2752
671
      }
2753
669
      break;
2754
669
    case IS_FALSE:
2755
3
    case IS_TRUE: {
2756
      /* Error handler can undef/change type of op1, save it and reset it in case those cases */
2757
3
      zval copy;
2758
3
      ZVAL_COPY_VALUE(&copy, op1);
2759
3
      zend_error(E_WARNING, "Increment on type bool has no effect, this will change in the next major version of PHP");
2760
3
      zval_ptr_dtor(op1);
2761
3
      ZVAL_COPY_VALUE(op1, &copy);
2762
3
      if (EG(exception)) {
2763
0
        return FAILURE;
2764
0
      }
2765
3
      break;
2766
3
    }
2767
3
    case IS_REFERENCE:
2768
0
      op1 = Z_REFVAL_P(op1);
2769
0
      goto try_again;
2770
1
    case IS_OBJECT: {
2771
1
      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
1
      zval tmp;
2779
1
      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
1
      ZEND_FALLTHROUGH;
2786
1
    }
2787
1
    case IS_RESOURCE:
2788
1
    case IS_ARRAY:
2789
1
      zend_type_error("Cannot increment %s", zend_zval_value_name(op1));
2790
1
      return FAILURE;
2791
2.73k
    EMPTY_SWITCH_DEFAULT_CASE()
2792
2.73k
  }
2793
2.72k
  return SUCCESS;
2794
2.73k
}
2795
/* }}} */
2796
2797
ZEND_API zend_result ZEND_FASTCALL decrement_function(zval *op1) /* {{{ */
2798
4.89k
{
2799
4.89k
  zend_long lval;
2800
4.89k
  double dval;
2801
2802
4.89k
try_again:
2803
4.89k
  switch (Z_TYPE_P(op1)) {
2804
171
    case IS_LONG:
2805
171
      fast_long_decrement_function(op1);
2806
171
      break;
2807
2.63k
    case IS_DOUBLE:
2808
2.63k
      Z_DVAL_P(op1) = Z_DVAL_P(op1) - 1;
2809
2.63k
      break;
2810
719
    case IS_STRING:   /* Like perl we only support string increment */
2811
719
      if (Z_STRLEN_P(op1) == 0) { /* consider as 0 */
2812
0
        zend_error(E_DEPRECATED, "Decrement on empty string is deprecated as non-numeric");
2813
0
        if (EG(exception)) {
2814
0
          return FAILURE;
2815
0
        }
2816
        /* A userland error handler can change the type from string to something else */
2817
0
        zval_ptr_dtor(op1);
2818
0
        ZVAL_LONG(op1, -1);
2819
0
        break;
2820
0
      }
2821
719
      switch (is_numeric_str_function(Z_STR_P(op1), &lval, &dval)) {
2822
370
        case IS_LONG:
2823
370
          zval_ptr_dtor_str(op1);
2824
370
          if (lval == ZEND_LONG_MIN) {
2825
2
            double d = (double)lval;
2826
2
            ZVAL_DOUBLE(op1, d-1);
2827
368
          } else {
2828
368
            ZVAL_LONG(op1, lval-1);
2829
368
          }
2830
370
          break;
2831
12
        case IS_DOUBLE:
2832
12
          zval_ptr_dtor_str(op1);
2833
12
          ZVAL_DOUBLE(op1, dval - 1);
2834
12
          break;
2835
337
        default: {
2836
          /* Error handler can unset the variable */
2837
337
          zend_string *zstr = Z_STR_P(op1);
2838
337
          zend_string_addref(zstr);
2839
337
          zend_error(E_DEPRECATED, "Decrement on non-numeric string has no effect and is deprecated");
2840
337
          if (EG(exception)) {
2841
0
            zend_string_release(zstr);
2842
0
            return FAILURE;
2843
0
          }
2844
337
          zval_ptr_dtor(op1);
2845
337
          ZVAL_STR(op1, zstr);
2846
337
        }
2847
719
      }
2848
719
      break;
2849
1.15k
    case IS_NULL: {
2850
      /* Error handler can undef/change type of op1, save it and reset it in case those cases */
2851
1.15k
      zval copy;
2852
1.15k
      ZVAL_COPY_VALUE(&copy, op1);
2853
1.15k
      zend_error(E_WARNING, "Decrement on type null has no effect, this will change in the next major version of PHP");
2854
1.15k
      zval_ptr_dtor(op1);
2855
1.15k
      ZVAL_COPY_VALUE(op1, &copy);
2856
1.15k
      if (EG(exception)) {
2857
0
        return FAILURE;
2858
0
      }
2859
1.15k
      break;
2860
1.15k
    }
2861
1.15k
    case IS_FALSE:
2862
206
    case IS_TRUE: {
2863
      /* Error handler can undef/change type of op1, save it and reset it in case those cases */
2864
206
      zval copy;
2865
206
      ZVAL_COPY_VALUE(&copy, op1);
2866
206
      zend_error(E_WARNING, "Decrement on type bool has no effect, this will change in the next major version of PHP");
2867
206
      zval_ptr_dtor(op1);
2868
206
      ZVAL_COPY_VALUE(op1, &copy);
2869
206
      if (EG(exception)) {
2870
0
        return FAILURE;
2871
0
      }
2872
206
      break;
2873
206
    }
2874
206
    case IS_REFERENCE:
2875
0
      op1 = Z_REFVAL_P(op1);
2876
0
      goto try_again;
2877
1
    case IS_OBJECT: {
2878
1
      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
1
      zval tmp;
2886
1
      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
1
      ZEND_FALLTHROUGH;
2893
1
    }
2894
1
    case IS_RESOURCE:
2895
1
    case IS_ARRAY:
2896
1
      zend_type_error("Cannot decrement %s", zend_zval_value_name(op1));
2897
1
      return FAILURE;
2898
4.89k
    EMPTY_SWITCH_DEFAULT_CASE()
2899
4.89k
  }
2900
2901
4.89k
  return SUCCESS;
2902
4.89k
}
2903
/* }}} */
2904
2905
ZEND_API bool ZEND_FASTCALL zend_is_true(const zval *op) /* {{{ */
2906
202k
{
2907
202k
  return i_zend_is_true(op);
2908
202k
}
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
14
{
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
14
  if (MB_CUR_MAX > 1) {
2946
14
#ifdef HAVE_NL_LANGINFO
2947
14
    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
14
    CG(variable_width_locale) = 1;
2971
14
    CG(ascii_compatible_locale) = 0;
2972
2973
14
    if (charmap) {
2974
14
      size_t len = strlen(charmap);
2975
14
      static const char *ascii_compatible_charmaps[] = {
2976
14
        "utf-8",
2977
14
        "utf8",
2978
        // TODO: EUC-* are also ASCII compatible ???
2979
14
        NULL
2980
14
      };
2981
14
      const char **p;
2982
      /* Check if current locale is ASCII compatible */
2983
14
      for (p = ascii_compatible_charmaps; *p; p++) {
2984
14
        if (zend_binary_strcasecmp(charmap, len, *p, strlen(*p)) == 0) {
2985
14
          CG(ascii_compatible_locale) = 1;
2986
14
          break;
2987
14
        }
2988
14
      }
2989
14
    }
2990
2991
14
  } 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
14
}
3001
/* }}} */
3002
3003
ZEND_API void zend_reset_lc_ctype_locale(void)
3004
14
{
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
14
  if (!setlocale(LC_CTYPE, "C.UTF-8")) {
3008
0
    setlocale(LC_CTYPE, "C");
3009
0
  }
3010
14
}
3011
3012
17.2M
static zend_always_inline void zend_str_tolower_impl(char *dest, const char *str, size_t length) /* {{{ */ {
3013
17.2M
  unsigned char *p = (unsigned char*)str;
3014
17.2M
  unsigned char *q = (unsigned char*)dest;
3015
17.2M
  unsigned char *end = p + length;
3016
17.2M
#ifdef HAVE_BLOCKCONV
3017
17.2M
  if (length >= BLOCKCONV_STRIDE) {
3018
1.32M
    BLOCKCONV_INIT_RANGE('A', 'Z');
3019
1.32M
    BLOCKCONV_INIT_DELTA('a' - 'A');
3020
1.61M
    do {
3021
1.61M
      BLOCKCONV_LOAD(p);
3022
1.61M
      BLOCKCONV_STORE(q);
3023
1.61M
      p += BLOCKCONV_STRIDE;
3024
1.61M
      q += BLOCKCONV_STRIDE;
3025
1.61M
    } while (p + BLOCKCONV_STRIDE <= end);
3026
1.32M
  }
3027
17.2M
#endif
3028
140M
  while (p < end) {
3029
123M
    *q++ = zend_tolower_ascii(*p++);
3030
123M
  }
3031
17.2M
}
3032
/* }}} */
3033
3034
6
static zend_always_inline void zend_str_toupper_impl(char *dest, const char *str, size_t length) /* {{{ */ {
3035
6
  unsigned char *p = (unsigned char*)str;
3036
6
  unsigned char *q = (unsigned char*)dest;
3037
6
  unsigned char *end = p + length;
3038
6
#ifdef HAVE_BLOCKCONV
3039
6
  if (length >= BLOCKCONV_STRIDE) {
3040
0
    BLOCKCONV_INIT_RANGE('a', 'z');
3041
0
    BLOCKCONV_INIT_DELTA('A' - 'a');
3042
0
    do {
3043
0
      BLOCKCONV_LOAD(p);
3044
0
      BLOCKCONV_STORE(q);
3045
0
      p += BLOCKCONV_STRIDE;
3046
0
      q += BLOCKCONV_STRIDE;
3047
0
    } while (p + BLOCKCONV_STRIDE <= end);
3048
0
  }
3049
6
#endif
3050
24
  while (p < end) {
3051
18
    *q++ = zend_toupper_ascii(*p++);
3052
18
  }
3053
6
}
3054
/* }}} */
3055
3056
ZEND_API char* ZEND_FASTCALL zend_str_tolower_copy(char *dest, const char *source, size_t length) /* {{{ */
3057
3.62M
{
3058
3.62M
  zend_str_tolower_impl(dest, source, length);
3059
3.62M
  dest[length] = '\0';
3060
3.62M
  return dest;
3061
3.62M
}
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
856
{
3074
856
  return zend_str_tolower_copy((char *)emalloc(length+1), source, length);
3075
856
}
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
8.69M
{
3086
8.69M
  zend_str_tolower_impl(str, (const char*)str, length);
3087
8.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
1
{
3099
1
  const unsigned char *p = (const unsigned char*)source;
3100
1
  const unsigned char *end = p + length;
3101
3102
1
  while (p < end) {
3103
0
    if (*p != zend_tolower_ascii(*p)) {
3104
0
      char *res = (char*)emalloc(length + 1);
3105
0
      unsigned char *r;
3106
3107
0
      if (p != (const unsigned char*)source) {
3108
0
        memcpy(res, source, p - (const unsigned char*)source);
3109
0
      }
3110
0
      r = (unsigned char*)p + (res - source);
3111
0
      zend_str_tolower_impl((char *)r, (const char*)p, end - p);
3112
0
      res[length] = '\0';
3113
0
      return res;
3114
0
    }
3115
0
    p++;
3116
0
  }
3117
1
  return NULL;
3118
1
}
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
12.0M
{
3147
12.0M
  size_t length = ZSTR_LEN(str);
3148
12.0M
  unsigned char *p = (unsigned char *) ZSTR_VAL(str);
3149
12.0M
  unsigned char *end = p + length;
3150
3151
12.0M
#ifdef HAVE_BLOCKCONV
3152
12.0M
  BLOCKCONV_INIT_RANGE('A', 'Z');
3153
26.1M
  while (p + BLOCKCONV_STRIDE <= end) {
3154
18.9M
    BLOCKCONV_LOAD(p);
3155
18.9M
    if (BLOCKCONV_FOUND()) {
3156
4.90M
      zend_string *res = zend_string_alloc(length, persistent);
3157
4.90M
      memcpy(ZSTR_VAL(res), ZSTR_VAL(str), p - (unsigned char *) ZSTR_VAL(str));
3158
4.90M
      unsigned char *q = (unsigned char*) ZSTR_VAL(res) + (p - (unsigned char*) ZSTR_VAL(str));
3159
3160
      /* Lowercase the chunk we already compared. */
3161
4.90M
      BLOCKCONV_INIT_DELTA('a' - 'A');
3162
4.90M
      BLOCKCONV_STORE(q);
3163
3164
      /* Lowercase the rest of the string. */
3165
4.90M
      p += BLOCKCONV_STRIDE;
3166
4.90M
      q += BLOCKCONV_STRIDE;
3167
4.90M
      zend_str_tolower_impl((char *) q, (const char *) p, end - p);
3168
4.90M
      ZSTR_VAL(res)[length] = '\0';
3169
4.90M
      return res;
3170
4.90M
    }
3171
14.0M
    p += BLOCKCONV_STRIDE;
3172
14.0M
  }
3173
7.11M
#endif
3174
3175
34.1M
  while (p < end) {
3176
31.7M
    if (*p != zend_tolower_ascii(*p)) {
3177
4.75M
      zend_string *res = zend_string_alloc(length, persistent);
3178
4.75M
      memcpy(ZSTR_VAL(res), ZSTR_VAL(str), p - (unsigned char*) ZSTR_VAL(str));
3179
3180
4.75M
      unsigned char *q = (unsigned char*) ZSTR_VAL(res) + (p - (unsigned char*) ZSTR_VAL(str));
3181
45.0M
      while (p < end) {
3182
40.2M
        *q++ = zend_tolower_ascii(*p++);
3183
40.2M
      }
3184
4.75M
      ZSTR_VAL(res)[length] = '\0';
3185
4.75M
      return res;
3186
4.75M
    }
3187
27.0M
    p++;
3188
27.0M
  }
3189
3190
2.36M
  return zend_string_copy(str);
3191
7.11M
}
3192
/* }}} */
3193
3194
ZEND_API zend_string* ZEND_FASTCALL zend_string_toupper_ex(zend_string *str, bool persistent) /* {{{ */
3195
533
{
3196
533
  size_t length = ZSTR_LEN(str);
3197
533
  unsigned char *p = (unsigned char *) ZSTR_VAL(str);
3198
533
  unsigned char *end = p + length;
3199
3200
533
#ifdef HAVE_BLOCKCONV
3201
533
  BLOCKCONV_INIT_RANGE('a', 'z');
3202
536
  while (p + BLOCKCONV_STRIDE <= end) {
3203
9
    BLOCKCONV_LOAD(p);
3204
9
    if (BLOCKCONV_FOUND()) {
3205
6
      zend_string *res = zend_string_alloc(length, persistent);
3206
6
      memcpy(ZSTR_VAL(res), ZSTR_VAL(str), p - (unsigned char *) ZSTR_VAL(str));
3207
6
      unsigned char *q = (unsigned char *) ZSTR_VAL(res) + (p - (unsigned char *) ZSTR_VAL(str));
3208
3209
      /* Uppercase the chunk we already compared. */
3210
6
      BLOCKCONV_INIT_DELTA('A' - 'a');
3211
6
      BLOCKCONV_STORE(q);
3212
3213
      /* Uppercase the rest of the string. */
3214
6
      p += BLOCKCONV_STRIDE;
3215
6
      q += BLOCKCONV_STRIDE;
3216
6
      zend_str_toupper_impl((char *) q, (const char *) p, end - p);
3217
6
      ZSTR_VAL(res)[length] = '\0';
3218
6
      return res;
3219
6
    }
3220
3
    p += BLOCKCONV_STRIDE;
3221
3
  }
3222
527
#endif
3223
3224
554
  while (p < end) {
3225
478
    if (*p != zend_toupper_ascii(*p)) {
3226
451
      zend_string *res = zend_string_alloc(length, persistent);
3227
451
      memcpy(ZSTR_VAL(res), ZSTR_VAL(str), p - (unsigned char*) ZSTR_VAL(str));
3228
3229
451
      unsigned char *q = (unsigned char *) ZSTR_VAL(res) + (p - (unsigned char *) ZSTR_VAL(str));
3230
4.04k
      while (p < end) {
3231
3.59k
        *q++ = zend_toupper_ascii(*p++);
3232
3.59k
      }
3233
451
      ZSTR_VAL(res)[length] = '\0';
3234
451
      return res;
3235
451
    }
3236
27
    p++;
3237
27
  }
3238
3239
76
  return zend_string_copy(str);
3240
527
}
3241
/* }}} */
3242
3243
ZEND_API int ZEND_FASTCALL zend_binary_strcmp(const char *s1, size_t len1, const char *s2, size_t len2) /* {{{ */
3244
63.5k
{
3245
63.5k
  int retval;
3246
3247
63.5k
  if (s1 == s2) {
3248
22
    return 0;
3249
22
  }
3250
63.5k
  retval = memcmp(s1, s2, MIN(len1, len2));
3251
63.5k
  if (!retval) {
3252
9.21k
    return ZEND_THREEWAY_COMPARE(len1, len2);
3253
54.3k
  } else {
3254
54.3k
    return retval;
3255
54.3k
  }
3256
63.5k
}
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
0
{
3261
0
  int retval;
3262
3263
0
  if (s1 == s2) {
3264
0
    return 0;
3265
0
  }
3266
0
  retval = memcmp(s1, s2, MIN(length, MIN(len1, len2)));
3267
0
  if (!retval) {
3268
0
    return ZEND_THREEWAY_COMPARE(MIN(length, len1), MIN(length, len2));
3269
0
  } else {
3270
0
    return retval;
3271
0
  }
3272
0
}
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.90M
{
3277
1.90M
  size_t len;
3278
1.90M
  int c1, c2;
3279
3280
1.90M
  if (s1 == s2) {
3281
47.7k
    return 0;
3282
47.7k
  }
3283
3284
1.85M
  len = MIN(len1, len2);
3285
4.50M
  while (len--) {
3286
4.31M
    c1 = zend_tolower_ascii(*(unsigned char *)s1++);
3287
4.31M
    c2 = zend_tolower_ascii(*(unsigned char *)s2++);
3288
4.31M
    if (c1 != c2) {
3289
1.66M
      return c1 - c2;
3290
1.66M
    }
3291
4.31M
  }
3292
3293
189k
  return ZEND_THREEWAY_COMPARE(len1, len2);
3294
1.85M
}
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
0
{
3299
0
  size_t len;
3300
0
  int c1, c2;
3301
3302
0
  if (s1 == s2) {
3303
0
    return 0;
3304
0
  }
3305
0
  len = MIN(length, MIN(len1, len2));
3306
0
  while (len--) {
3307
0
    c1 = zend_tolower_ascii(*(unsigned char *)s1++);
3308
0
    c2 = zend_tolower_ascii(*(unsigned char *)s2++);
3309
0
    if (c1 != c2) {
3310
0
      return c1 - c2;
3311
0
    }
3312
0
  }
3313
3314
0
  return ZEND_THREEWAY_COMPARE(MIN(length, len1), MIN(length, len2));
3315
0
}
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
813
{
3375
813
  uint8_t ret1, ret2;
3376
813
  int oflow1, oflow2;
3377
813
  zend_long lval1 = 0, lval2 = 0;
3378
813
  double dval1 = 0.0, dval2 = 0.0;
3379
3380
813
  if ((ret1 = is_numeric_string_ex(s1->val, s1->len, &lval1, &dval1, false, &oflow1, NULL)) &&
3381
521
    (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
488
    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
488
    if ((ret1 == IS_DOUBLE) || (ret2 == IS_DOUBLE)) {
3394
206
      if (ret1 != IS_DOUBLE) {
3395
115
        if (oflow2) {
3396
          /* 2nd operand is integer > LONG_MAX (oflow2==1) or < LONG_MIN (-1) */
3397
100
          return 0;
3398
100
        }
3399
15
        dval1 = (double) lval1;
3400
91
      } else if (ret2 != IS_DOUBLE) {
3401
24
        if (oflow1) {
3402
21
          return 0;
3403
21
        }
3404
3
        dval2 = (double) lval2;
3405
67
      } 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
30
        goto string_cmp;
3409
30
      }
3410
55
      return dval1 == dval2;
3411
282
    } else { /* they both have to be long's */
3412
282
      return lval1 == lval2;
3413
282
    }
3414
488
  } else {
3415
355
string_cmp:
3416
355
    return zend_string_equal_content(s1, s2);
3417
325
  }
3418
813
}
3419
/* }}} */
3420
3421
ZEND_API int ZEND_FASTCALL zendi_smart_strcmp(zend_string *s1, zend_string *s2) /* {{{ */
3422
6.75k
{
3423
6.75k
  uint8_t ret1, ret2;
3424
6.75k
  int oflow1, oflow2;
3425
6.75k
  zend_long lval1 = 0, lval2 = 0;
3426
6.75k
  double dval1 = 0.0, dval2 = 0.0;
3427
3428
6.75k
  if ((ret1 = is_numeric_string_ex(s1->val, s1->len, &lval1, &dval1, false, &oflow1, NULL)) &&
3429
4.02k
    (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.78k
    if (oflow1 != 0 && oflow1 == oflow2 && dval1 - dval2 == 0.) {
3436
18
#endif
3437
      /* both values are integers overflowed to the same side, and the
3438
       * double comparison may have resulted in crucial accuracy lost */
3439
18
      goto string_cmp;
3440
18
    }
3441
2.76k
    if ((ret1 == IS_DOUBLE) || (ret2 == IS_DOUBLE)) {
3442
2.27k
      if (ret1 != IS_DOUBLE) {
3443
265
        if (oflow2) {
3444
          /* 2nd operand is integer > LONG_MAX (oflow2==1) or < LONG_MIN (-1) */
3445
165
          return -1 * oflow2;
3446
165
        }
3447
100
        dval1 = (double) lval1;
3448
2.00k
      } else if (ret2 != IS_DOUBLE) {
3449
1.49k
        if (oflow1) {
3450
67
          return oflow1;
3451
67
        }
3452
1.43k
        dval2 = (double) lval2;
3453
1.43k
      } 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
56
        goto string_cmp;
3457
56
      }
3458
1.98k
      dval1 = dval1 - dval2;
3459
1.98k
      return ZEND_NORMALIZE_BOOL(dval1);
3460
2.27k
    } else { /* they both have to be long's */
3461
492
      return lval1 > lval2 ? 1 : (lval1 < lval2 ? -1 : 0);
3462
492
    }
3463
3.96k
  } else {
3464
3.96k
    int strcmp_ret;
3465
4.04k
string_cmp:
3466
4.04k
    strcmp_ret = zend_binary_strcmp(s1->val, s1->len, s2->val, s2->len);
3467
4.04k
    return ZEND_NORMALIZE_BOOL(strcmp_ret);
3468
3.96k
  }
3469
6.75k
}
3470
/* }}} */
3471
3472
static int hash_zval_compare_function(zval *z1, zval *z2) /* {{{ */
3473
397
{
3474
397
  return zend_compare(z1, z2);
3475
397
}
3476
/* }}} */
3477
3478
ZEND_API int ZEND_FASTCALL zend_compare_symbol_tables(HashTable *ht1, HashTable *ht2) /* {{{ */
3479
3.46k
{
3480
3.46k
  if (ht1 == ht2) {
3481
304
    return 0;
3482
304
  }
3483
3484
3.16k
  GC_TRY_ADDREF(ht1);
3485
3.16k
  GC_TRY_ADDREF(ht2);
3486
3487
3.16k
  int ret = zend_hash_compare(ht1, ht2, (compare_func_t) hash_zval_compare_function, 0);
3488
3489
3.16k
  GC_TRY_DTOR_NO_REF(ht1);
3490
3.16k
  GC_TRY_DTOR_NO_REF(ht2);
3491
3492
3.16k
  return ret;
3493
3.46k
}
3494
/* }}} */
3495
3496
ZEND_API int ZEND_FASTCALL zend_compare_arrays(zval *a1, zval *a2) /* {{{ */
3497
3.46k
{
3498
3.46k
  return zend_compare_symbol_tables(Z_ARRVAL_P(a1), Z_ARRVAL_P(a2));
3499
3.46k
}
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
976k
{
3518
976k
  if ((zend_ulong)num <= 9) {
3519
898k
    return ZSTR_CHAR((zend_uchar)'0' + (zend_uchar)num);
3520
898k
  } else {
3521
78.4k
    char buf[MAX_LENGTH_OF_LONG + 1];
3522
78.4k
    char *res = zend_print_long_to_buf(buf + sizeof(buf) - 1, num);
3523
78.4k
    zend_string *str =  zend_string_init(res, buf + sizeof(buf) - 1 - res, 0);
3524
78.4k
    GC_ADD_FLAGS(str, IS_STR_VALID_UTF8);
3525
78.4k
    return str;
3526
78.4k
  }
3527
976k
}
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
168k
{
3602
168k
  char buf[ZEND_DOUBLE_MAX_LENGTH];
3603
  /* Model snprintf precision behavior. */
3604
168k
  int precision = (int) EG(precision);
3605
168k
  zend_gcvt(num, precision ? precision : 1, '.', 'E', buf);
3606
168k
  zend_string *str =  zend_string_init(buf, strlen(buf), 0);
3607
168k
  if (UNEXPECTED(zend_isnan(num))) {
3608
349
    zend_nan_coerced_to_type_warning(IS_STRING);
3609
349
  }
3610
168k
  GC_ADD_FLAGS(str, IS_STR_VALID_UTF8);
3611
168k
  return str;
3612
168k
}
3613
3614
ZEND_API uint8_t ZEND_FASTCALL is_numeric_str_function(const zend_string *str, zend_long *lval, double *dval) /* {{{ */
3615
16.8k
{
3616
16.8k
  return is_numeric_string(ZSTR_VAL(str), ZSTR_LEN(str), lval, dval, false);
3617
16.8k
}
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
179k
{
3623
179k
  const char *ptr;
3624
179k
  int digits = 0, dp_or_e = 0;
3625
179k
  double local_dval = 0.0;
3626
179k
  uint8_t type;
3627
179k
  zend_ulong tmp_lval = 0;
3628
179k
  int neg = 0;
3629
3630
179k
  if (!length) {
3631
23.7k
    return 0;
3632
23.7k
  }
3633
3634
155k
  if (oflow_info != NULL) {
3635
10.7k
    *oflow_info = 0;
3636
10.7k
  }
3637
155k
  if (trailing_data != NULL) {
3638
26.0k
    *trailing_data = false;
3639
26.0k
  }
3640
3641
  /* Skip any whitespace
3642
   * This is much faster than the isspace() function */
3643
189k
  while (*str == ' ' || *str == '\t' || *str == '\n' || *str == '\r' || *str == '\v' || *str == '\f') {
3644
34.0k
    str++;
3645
34.0k
    length--;
3646
34.0k
  }
3647
155k
  ptr = str;
3648
3649
155k
  if (*ptr == '-') {
3650
36.6k
    neg = 1;
3651
36.6k
    ptr++;
3652
119k
  } else if (*ptr == '+') {
3653
2.92k
    ptr++;
3654
2.92k
  }
3655
3656
155k
  if (ZEND_IS_DIGIT(*ptr)) {
3657
    /* Skip any leading 0s */
3658
235k
    while (*ptr == '0') {
3659
132k
      ptr++;
3660
132k
    }
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
760k
    for (type = IS_LONG; !(digits >= MAX_LENGTH_OF_LONG && (dval || allow_errors)); digits++, ptr++) {
3666
766k
check_digits:
3667
766k
      if (ZEND_IS_DIGIT(*ptr)) {
3668
657k
        tmp_lval = tmp_lval * 10 + (*ptr) - '0';
3669
657k
        continue;
3670
657k
      } else if (*ptr == '.' && dp_or_e < 1) {
3671
15.4k
        goto process_double;
3672
93.5k
      } else if ((*ptr == 'e' || *ptr == 'E') && dp_or_e < 2) {
3673
15.4k
        const char *e = ptr + 1;
3674
3675
15.4k
        if (*e == '-' || *e == '+') {
3676
2.38k
          ptr = e++;
3677
2.38k
        }
3678
15.4k
        if (ZEND_IS_DIGIT(*e)) {
3679
8.21k
          goto process_double;
3680
8.21k
        }
3681
15.4k
      }
3682
3683
85.3k
      break;
3684
766k
    }
3685
3686
91.3k
    if (digits >= MAX_LENGTH_OF_LONG) {
3687
9.91k
      if (oflow_info != NULL) {
3688
816
        *oflow_info = *str == '-' ? -1 : 1;
3689
816
      }
3690
9.91k
      dp_or_e = -1;
3691
9.91k
      goto process_double;
3692
9.91k
    }
3693
91.3k
  } else if (*ptr == '.' && ZEND_IS_DIGIT(ptr[1])) {
3694
46.7k
process_double:
3695
46.7k
    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
46.7k
    if (dval) {
3700
30.4k
      local_dval = zend_strtod(str, &ptr);
3701
30.4k
    } else if (!allow_errors && dp_or_e != -1) {
3702
12.1k
      dp_or_e = (*ptr++ == '.') ? 1 : 2;
3703
12.1k
      goto check_digits;
3704
12.1k
    }
3705
46.7k
  } else {
3706
39.8k
    return 0;
3707
39.8k
  }
3708
3709
116k
  if (ptr != str + length) {
3710
34.8k
    const char *endptr = ptr;
3711
48.9k
    while (*endptr == ' ' || *endptr == '\t' || *endptr == '\n' || *endptr == '\r' || *endptr == '\v' || *endptr == '\f') {
3712
14.1k
      endptr++;
3713
14.1k
      length--;
3714
14.1k
    }
3715
34.8k
    if (ptr != str + length) {
3716
33.6k
      if (!allow_errors) {
3717
11.4k
        return 0;
3718
11.4k
      }
3719
22.2k
      if (trailing_data != NULL) {
3720
10.3k
        *trailing_data = true;
3721
10.3k
      }
3722
22.2k
    }
3723
34.8k
  }
3724
3725
104k
  if (type == IS_LONG) {
3726
62.8k
    if (digits == MAX_LENGTH_OF_LONG - 1) {
3727
5.93k
      int cmp = strcmp(&ptr[-digits], long_min_digits);
3728
3729
5.93k
      if (!(cmp < 0 || (cmp == 0 && *str == '-'))) {
3730
3.31k
        if (dval) {
3731
2.62k
          *dval = zend_strtod(str, NULL);
3732
2.62k
        }
3733
3.31k
        if (oflow_info != NULL) {
3734
185
          *oflow_info = *str == '-' ? -1 : 1;
3735
185
        }
3736
3737
3.31k
        return IS_DOUBLE;
3738
3.31k
      }
3739
5.93k
    }
3740
3741
59.5k
    if (lval) {
3742
39.7k
      if (neg) {
3743
17.2k
        tmp_lval = -tmp_lval;
3744
17.2k
      }
3745
39.7k
      *lval = (zend_long) tmp_lval;
3746
39.7k
    }
3747
3748
59.5k
    return IS_LONG;
3749
62.8k
  } else {
3750
41.7k
    if (dval) {
3751
29.0k
      *dval = local_dval;
3752
29.0k
    }
3753
3754
41.7k
    return IS_DOUBLE;
3755
41.7k
  }
3756
104k
}
3757
/* }}} */
3758
3759
/*
3760
 * String matching - Sunday algorithm
3761
 * http://www.iti.fh-flensburg.de/lang/algorithmen/pattern/sundayen.htm
3762
 */
3763
77
static zend_always_inline void zend_memnstr_ex_pre(unsigned int td[], const char *needle, size_t needle_len, int reverse) /* {{{ */ {
3764
77
  int i;
3765
3766
19.7k
  for (i = 0; i < 256; i++) {
3767
19.7k
    td[i] = needle_len + 1;
3768
19.7k
  }
3769
3770
77
  if (reverse) {
3771
0
    for (i = needle_len - 1; i >= 0; i--) {
3772
0
      td[(unsigned char)needle[i]] = i + 1;
3773
0
    }
3774
77
  } else {
3775
77
    size_t i;
3776
3777
7.29k
    for (i = 0; i < needle_len; i++) {
3778
7.21k
      td[(unsigned char)needle[i]] = (int)needle_len - i;
3779
7.21k
    }
3780
77
  }
3781
77
}
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
77
{
3786
77
  unsigned int td[256];
3787
77
  size_t i;
3788
77
  const char *p;
3789
3790
77
  if (needle_len == 0 || (end - haystack) < needle_len) {
3791
0
    return NULL;
3792
0
  }
3793
3794
77
  zend_memnstr_ex_pre(td, needle, needle_len, 0);
3795
3796
77
  p = haystack;
3797
77
  end -= needle_len;
3798
3799
2.59k
  while (p <= end) {
3800
2.65k
    for (i = 0; i < needle_len; i++) {
3801
2.65k
      if (needle[i] != p[i]) {
3802
2.51k
        break;
3803
2.51k
      }
3804
2.65k
    }
3805
2.51k
    if (i == needle_len) {
3806
0
      return p;
3807
0
    }
3808
2.51k
    if (UNEXPECTED(p == end)) {
3809
3
      return NULL;
3810
3
    }
3811
2.51k
    p += td[(unsigned char)(p[needle_len])];
3812
2.51k
  }
3813
3814
74
  return NULL;
3815
77
}
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
40.7k
{
3872
40.7k
  double  two_pow_64 = pow(2., 64.),
3873
40.7k
      dmod;
3874
3875
40.7k
  dmod = fmod(d, two_pow_64);
3876
40.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
4.91k
    dmod += two_pow_64;
3880
4.91k
  }
3881
40.7k
  return (zend_long)(zend_ulong)dmod;
3882
40.7k
}
3883
/* }}} */
3884
#endif