Coverage Report

Created: 2025-11-16 06:23

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
843k
#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
10.3M
  const __m128i blconv_offset = _mm_set1_epi8((signed char)(SCHAR_MIN - start)); \
93
10.3M
  const __m128i blconv_threshold = _mm_set1_epi8(SCHAR_MIN + (end - start) + 1);
94
95
54.8M
#define BLOCKCONV_STRIDE sizeof(__m128i)
96
97
#define BLOCKCONV_INIT_DELTA(delta) \
98
4.57M
  const __m128i blconv_delta = _mm_set1_epi8(delta);
99
100
#define BLOCKCONV_LOAD(input) \
101
15.3M
  __m128i blconv_operand = _mm_loadu_si128((__m128i*)(input)); \
102
15.3M
  __m128i blconv_mask = _mm_cmplt_epi8(_mm_add_epi8(blconv_operand, blconv_offset), blconv_threshold);
103
104
14.5M
#define BLOCKCONV_FOUND() _mm_movemask_epi8(blconv_mask)
105
106
#define BLOCKCONV_STORE(dest) \
107
4.67M
  __m128i blconv_add = _mm_and_si128(blconv_mask, blconv_delta); \
108
4.67M
  __m128i blconv_result = _mm_add_epi8(blconv_operand, blconv_add); \
109
4.67M
  _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
302
  ZVAL_UNDEF(dst);                                    \
234
302
  if (Z_OBJ_HT_P(op)->cast_object(Z_OBJ_P(op), dst, ctype) == FAILURE) {         \
235
302
    zend_error(E_WARNING,                               \
236
302
      "Object of class %s could not be converted to %s", ZSTR_VAL(Z_OBJCE_P(op)->name),\
237
302
    zend_get_type_by_const(ctype));                           \
238
302
  }                                            \
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
7.58k
{
294
7.58k
  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.79k
    case IS_STRING:
303
1.79k
      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
764
        ZVAL_LONG(holder, 0);
305
764
      }
306
1.79k
      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
1.95k
    case IS_LONG:
318
1.97k
    case IS_DOUBLE:
319
5.78k
    default:
320
5.78k
      return op;
321
7.58k
  }
322
7.58k
}
323
/* }}} */
324
325
static zend_never_inline zend_result ZEND_FASTCALL _zendi_try_convert_scalar_to_number(zval *op, zval *holder) /* {{{ */
326
93.2k
{
327
93.2k
  switch (Z_TYPE_P(op)) {
328
48.6k
    case IS_NULL:
329
58.9k
    case IS_FALSE:
330
58.9k
      ZVAL_LONG(holder, 0);
331
58.9k
      return SUCCESS;
332
5.61k
    case IS_TRUE:
333
5.61k
      ZVAL_LONG(holder, 1);
334
5.61k
      return SUCCESS;
335
28.4k
    case IS_STRING:
336
28.4k
    {
337
28.4k
      bool trailing_data = false;
338
      /* For BC reasons we allow errors so that we can warn on leading numeric string */
339
28.4k
      if (0 == (Z_TYPE_INFO_P(holder) = is_numeric_string_ex(Z_STRVAL_P(op), Z_STRLEN_P(op),
340
28.4k
          &Z_LVAL_P(holder), &Z_DVAL_P(holder),  /* allow errors */ true, NULL, &trailing_data))) {
341
        /* Will lead to invalid OP type error */
342
532
        return FAILURE;
343
532
      }
344
27.9k
      if (UNEXPECTED(trailing_data)) {
345
14.3k
        zend_error(E_WARNING, "A non-numeric value encountered");
346
14.3k
        if (UNEXPECTED(EG(exception))) {
347
0
          return FAILURE;
348
0
        }
349
14.3k
      }
350
27.9k
      return SUCCESS;
351
27.9k
    }
352
105
    case IS_OBJECT:
353
105
      if (Z_OBJ_HT_P(op)->cast_object(Z_OBJ_P(op), holder, _IS_NUMBER) == FAILURE
354
105
          || EG(exception)) {
355
105
        return FAILURE;
356
105
      }
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
194
    case IS_ARRAY:
361
194
      return FAILURE;
362
93.2k
    EMPTY_SWITCH_DEFAULT_CASE()
363
93.2k
  }
364
93.2k
}
365
/* }}} */
366
367
static zend_always_inline zend_result zendi_try_convert_scalar_to_number(zval *op, zval *holder) /* {{{ */
368
160k
{
369
160k
  if (Z_TYPE_P(op) == IS_LONG || Z_TYPE_P(op) == IS_DOUBLE) {
370
67.1k
    ZVAL_COPY_VALUE(holder, op);
371
67.1k
    return SUCCESS;
372
93.2k
  } else {
373
93.2k
    return _zendi_try_convert_scalar_to_number(op, holder);
374
93.2k
  }
375
160k
}
376
/* }}} */
377
378
static zend_never_inline zend_long ZEND_FASTCALL zendi_try_get_long(const zval *op, bool *failed) /* {{{ */
379
87.1k
{
380
87.1k
  *failed = false;
381
87.1k
try_again:
382
87.1k
  switch (Z_TYPE_P(op)) {
383
7.95k
    case IS_NULL:
384
14.3k
    case IS_FALSE:
385
14.3k
      return 0;
386
2.46k
    case IS_TRUE:
387
2.46k
      return 1;
388
62.7k
    case IS_DOUBLE: {
389
62.7k
      double dval = Z_DVAL_P(op);
390
62.7k
      zend_long lval = zend_dval_to_lval_safe(dval);
391
62.7k
      if (UNEXPECTED(EG(exception))) {
392
0
        *failed = true;
393
0
      }
394
62.7k
      return lval;
395
7.95k
    }
396
7.49k
    case IS_STRING:
397
7.49k
      {
398
7.49k
        uint8_t type;
399
7.49k
        zend_long lval;
400
7.49k
        double dval;
401
7.49k
        bool trailing_data = false;
402
7.49k
        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.49k
        type = is_numeric_string_ex(Z_STRVAL_P(op), Z_STRLEN_P(op), &lval, &dval,
406
7.49k
          /* allow errors */ true, NULL, &trailing_data);
407
7.49k
        if (type == 0) {
408
603
          *failed = true;
409
603
          return 0;
410
603
        }
411
6.89k
        if (UNEXPECTED(trailing_data)) {
412
3.01k
          if (type != IS_LONG) {
413
803
            op_str = zend_string_copy(Z_STR_P(op));
414
803
          }
415
3.01k
          zend_error(E_WARNING, "A non-numeric value encountered");
416
3.01k
          if (UNEXPECTED(EG(exception))) {
417
0
            *failed = true;
418
0
            zend_tmp_string_release(op_str);
419
0
            return 0;
420
0
          }
421
3.01k
        }
422
6.89k
        if (EXPECTED(type == IS_LONG)) {
423
4.85k
          return lval;
424
4.85k
        } 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
2.03k
          if (op_str == NULL) {
431
            /* zend_dval_to_lval_cap() can emit a warning so always do the copy here */
432
1.23k
            op_str = zend_string_copy(Z_STR_P(op));
433
1.23k
          }
434
2.03k
          lval = zend_dval_to_lval_cap(dval, op_str);
435
2.03k
          if (!zend_is_long_compatible(dval, lval)) {
436
1.56k
            zend_incompatible_string_to_long_error(op_str);
437
1.56k
            if (UNEXPECTED(EG(exception))) {
438
0
              *failed = true;
439
0
            }
440
1.56k
          }
441
2.03k
          zend_string_release(op_str);
442
2.03k
          return lval;
443
2.03k
        }
444
6.89k
      }
445
83
    case IS_OBJECT:
446
83
      {
447
83
        zval dst;
448
83
        if (Z_OBJ_HT_P(op)->cast_object(Z_OBJ_P(op), &dst, IS_LONG) == FAILURE
449
83
            || EG(exception)) {
450
83
          *failed = true;
451
83
          return 0;
452
83
        }
453
0
        ZEND_ASSERT(Z_TYPE(dst) == IS_LONG);
454
0
        return Z_LVAL(dst);
455
0
      }
456
0
    case IS_RESOURCE:
457
28
    case IS_ARRAY:
458
28
      *failed = true;
459
28
      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
87.1k
    EMPTY_SWITCH_DEFAULT_CASE()
469
87.1k
  }
470
87.1k
}
471
/* }}} */
472
473
ZEND_API zend_long ZEND_FASTCALL zval_try_get_long(const zval *op, bool *failed)
474
0
{
475
0
  if (EXPECTED(Z_TYPE_P(op) == IS_LONG)) {
476
0
    *failed = false;
477
0
    return Z_LVAL_P(op);
478
0
  }
479
0
  return zendi_try_get_long(op, failed);
480
0
}
481
482
#define ZEND_TRY_BINARY_OP1_OBJECT_OPERATION(opcode) \
483
254k
  if (UNEXPECTED(Z_TYPE_P(op1) == IS_OBJECT) \
484
254k
    && 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
325k
  if (UNEXPECTED(Z_TYPE_P(op2) == IS_OBJECT) \
492
325k
    && UNEXPECTED(Z_OBJ_HANDLER_P(op2, do_operation)) \
493
325k
    && 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
211k
  ZEND_TRY_BINARY_OP1_OBJECT_OPERATION(opcode) \
499
211k
  else \
500
211k
  ZEND_TRY_BINARY_OP2_OBJECT_OPERATION(opcode)
501
502
#define ZEND_TRY_UNARY_OBJECT_OPERATION(opcode) \
503
32.1k
  if (UNEXPECTED(Z_TYPE_P(op1) == IS_OBJECT) \
504
32.1k
    && UNEXPECTED(Z_OBJ_HANDLER_P(op1, do_operation)) \
505
32.1k
    && 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
28.8k
  do {                               \
511
28.8k
    if (UNEXPECTED(Z_TYPE_P(op1) != IS_LONG)) {           \
512
19.5k
      bool failed;                      \
513
19.5k
      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
19.5k
      ZEND_TRY_BINARY_OP1_OBJECT_OPERATION(opcode);       \
521
19.5k
      op1_lval = zendi_try_get_long(op1, &failed);        \
522
19.5k
      if (UNEXPECTED(failed)) {                 \
523
176
        zend_binop_error(sigil, op1, op2);            \
524
176
        if (result != op1) {                 \
525
113
          ZVAL_UNDEF(result);                 \
526
113
        }                            \
527
176
        return FAILURE;                     \
528
176
      }                              \
529
19.5k
    } else {                           \
530
9.30k
      op1_lval = Z_LVAL_P(op1);                  \
531
9.30k
    }                                \
532
28.8k
  } while (0);                            \
533
28.8k
  do {                               \
534
28.6k
    if (UNEXPECTED(Z_TYPE_P(op2) != IS_LONG)) {           \
535
6.76k
      bool failed;                      \
536
6.76k
      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
6.76k
      ZEND_TRY_BINARY_OP2_OBJECT_OPERATION(opcode);       \
544
6.76k
      op2_lval = zendi_try_get_long(op2, &failed);        \
545
6.76k
      if (UNEXPECTED(failed)) {                 \
546
337
        zend_binop_error(sigil, op1, op2);            \
547
337
        if (result != op1) {                 \
548
326
          ZVAL_UNDEF(result);                 \
549
326
        }                            \
550
337
        return FAILURE;                     \
551
337
      }                              \
552
21.8k
    } else {                           \
553
21.8k
      op2_lval = Z_LVAL_P(op2);                  \
554
21.8k
    }                                \
555
28.6k
  } while (0);
556
557
ZEND_API void ZEND_FASTCALL convert_to_long(zval *op) /* {{{ */
558
27
{
559
27
  zend_long tmp;
560
561
27
try_again:
562
27
  switch (Z_TYPE_P(op)) {
563
0
    case IS_NULL:
564
0
    case IS_FALSE:
565
0
      ZVAL_LONG(op, 0);
566
0
      break;
567
0
    case IS_TRUE:
568
0
      ZVAL_LONG(op, 1);
569
0
      break;
570
0
    case IS_RESOURCE:
571
0
      tmp = Z_RES_HANDLE_P(op);
572
0
      zval_ptr_dtor(op);
573
0
      ZVAL_LONG(op, tmp);
574
0
      break;
575
22
    case IS_LONG:
576
22
      break;
577
5
    case IS_DOUBLE: {
578
      /* NAN might emit a warning */
579
5
      zend_long new_value = zend_dval_to_lval(Z_DVAL_P(op));
580
5
      zval_ptr_dtor(op);
581
5
      ZVAL_LONG(op, new_value);
582
5
      break;
583
0
    }
584
0
    case IS_STRING:
585
0
      {
586
0
        zend_string *str = Z_STR_P(op);
587
0
        ZVAL_LONG(op, zval_get_long(op));
588
0
        zend_string_release_ex(str, 0);
589
0
      }
590
0
      break;
591
0
    case IS_ARRAY:
592
0
      tmp = (zend_hash_num_elements(Z_ARRVAL_P(op))?1:0);
593
0
      zval_ptr_dtor(op);
594
0
      ZVAL_LONG(op, tmp);
595
0
      break;
596
0
    case IS_OBJECT:
597
0
      {
598
0
        zval dst;
599
600
0
        convert_object_to_type(op, &dst, IS_LONG);
601
0
        zval_ptr_dtor(op);
602
603
0
        if (Z_TYPE(dst) == IS_LONG) {
604
0
          ZVAL_LONG(op, Z_LVAL(dst));
605
0
        } else {
606
0
          ZVAL_LONG(op, 1);
607
0
        }
608
0
        return;
609
0
      }
610
0
    case IS_REFERENCE:
611
0
      zend_unwrap_reference(op);
612
0
      goto try_again;
613
27
    EMPTY_SWITCH_DEFAULT_CASE()
614
27
  }
615
27
}
616
/* }}} */
617
618
ZEND_API void ZEND_FASTCALL convert_to_double(zval *op) /* {{{ */
619
325
{
620
325
  double tmp;
621
622
325
try_again:
623
325
  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
325
    case IS_LONG:
638
325
      ZVAL_DOUBLE(op, (double) Z_LVAL_P(op));
639
325
      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
325
    EMPTY_SWITCH_DEFAULT_CASE()
673
325
  }
674
325
}
675
/* }}} */
676
677
ZEND_API void ZEND_FASTCALL convert_to_null(zval *op) /* {{{ */
678
32
{
679
32
  if (UNEXPECTED(Z_TYPE_P(op) == IS_DOUBLE && zend_isnan(Z_DVAL_P(op)))) {
680
5
    zend_nan_coerced_to_type_warning(IS_NULL);
681
5
  }
682
32
  zval_ptr_dtor(op);
683
32
  ZVAL_NULL(op);
684
32
}
685
/* }}} */
686
687
ZEND_API void ZEND_FASTCALL convert_to_boolean(zval *op) /* {{{ */
688
5
{
689
5
  bool tmp;
690
691
5
try_again:
692
5
  switch (Z_TYPE_P(op)) {
693
0
    case IS_FALSE:
694
0
    case IS_TRUE:
695
0
      break;
696
0
    case IS_NULL:
697
0
      ZVAL_FALSE(op);
698
0
      break;
699
0
    case IS_RESOURCE: {
700
0
        zend_long l = (Z_RES_HANDLE_P(op) ? 1 : 0);
701
702
0
        zval_ptr_dtor(op);
703
0
        ZVAL_BOOL(op, l);
704
0
      }
705
0
      break;
706
0
    case IS_LONG:
707
0
      ZVAL_BOOL(op, Z_LVAL_P(op) ? 1 : 0);
708
0
      break;
709
5
    case IS_DOUBLE: {
710
      /* We compute the new value before emitting the warning as the zval may change */
711
5
      bool new_value = Z_DVAL_P(op) ? true : false;
712
5
      if (UNEXPECTED(zend_isnan(Z_DVAL_P(op)))) {
713
5
        zend_nan_coerced_to_type_warning(_IS_BOOL);
714
5
        zval_ptr_dtor(op);
715
5
      }
716
5
      ZVAL_BOOL(op, new_value);
717
5
      break;
718
0
    }
719
0
    case IS_STRING:
720
0
      {
721
0
        zend_string *str = Z_STR_P(op);
722
723
0
        if (ZSTR_LEN(str) == 0
724
0
          || (ZSTR_LEN(str) == 1 && ZSTR_VAL(str)[0] == '0')) {
725
0
          ZVAL_FALSE(op);
726
0
        } else {
727
0
          ZVAL_TRUE(op);
728
0
        }
729
0
        zend_string_release_ex(str, 0);
730
0
      }
731
0
      break;
732
0
    case IS_ARRAY:
733
0
      tmp = (zend_hash_num_elements(Z_ARRVAL_P(op))?1:0);
734
0
      zval_ptr_dtor(op);
735
0
      ZVAL_BOOL(op, tmp);
736
0
      break;
737
0
    case IS_OBJECT:
738
0
      {
739
0
        zval dst;
740
741
0
        convert_object_to_type(op, &dst, _IS_BOOL);
742
0
        zval_ptr_dtor(op);
743
744
0
        if (Z_TYPE_INFO(dst) == IS_FALSE || Z_TYPE_INFO(dst) == IS_TRUE) {
745
0
          Z_TYPE_INFO_P(op) = Z_TYPE_INFO(dst);
746
0
        } else {
747
0
          ZVAL_TRUE(op);
748
0
        }
749
0
        break;
750
0
      }
751
0
    case IS_REFERENCE:
752
0
      zend_unwrap_reference(op);
753
0
      goto try_again;
754
5
    EMPTY_SWITCH_DEFAULT_CASE()
755
5
  }
756
5
}
757
/* }}} */
758
759
ZEND_API void ZEND_FASTCALL _convert_to_string(zval *op) /* {{{ */
760
825k
{
761
825k
try_again:
762
825k
  switch (Z_TYPE_P(op)) {
763
2
    case IS_UNDEF:
764
5.82k
    case IS_NULL:
765
7.90k
    case IS_FALSE: {
766
7.90k
      ZVAL_EMPTY_STRING(op);
767
7.90k
      break;
768
5.82k
    }
769
3.13k
    case IS_TRUE:
770
3.13k
      ZVAL_CHAR(op, '1');
771
3.13k
      break;
772
0
    case IS_STRING:
773
0
      break;
774
0
    case IS_RESOURCE: {
775
0
      zend_string *str = zend_strpprintf(0, "Resource id #" ZEND_LONG_FMT, (zend_long)Z_RES_HANDLE_P(op));
776
0
      zval_ptr_dtor(op);
777
0
      ZVAL_NEW_STR(op, str);
778
0
      break;
779
5.82k
    }
780
812k
    case IS_LONG:
781
812k
      ZVAL_STR(op, zend_long_to_str(Z_LVAL_P(op)));
782
812k
      break;
783
2.32k
    case IS_DOUBLE: {
784
      /* Casting NAN will cause a warning */
785
2.32k
      zend_string *new_value = zend_double_to_str(Z_DVAL_P(op));
786
2.32k
      zval_ptr_dtor(op);
787
2.32k
      ZVAL_NEW_STR(op, new_value);
788
2.32k
      break;
789
5.82k
    }
790
247
    case IS_ARRAY:
791
247
      zend_error(E_WARNING, "Array to string conversion");
792
247
      zval_ptr_dtor(op);
793
247
      ZVAL_INTERNED_STR(op, ZSTR_KNOWN(ZEND_STR_ARRAY_CAPITALIZED));
794
247
      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
825k
    EMPTY_SWITCH_DEFAULT_CASE()
813
825k
  }
814
825k
}
815
/* }}} */
816
817
ZEND_API bool ZEND_FASTCALL _try_convert_to_string(zval *op) /* {{{ */
818
13
{
819
13
  zend_string *str;
820
821
13
  ZEND_ASSERT(Z_TYPE_P(op) != IS_STRING);
822
13
  str = zval_try_get_string_func(op);
823
13
  if (UNEXPECTED(!str)) {
824
13
    return 0;
825
13
  }
826
0
  zval_ptr_dtor(op);
827
0
  ZVAL_STR(op, str);
828
0
  return 1;
829
13
}
830
/* }}} */
831
832
static void convert_scalar_to_array(zval *op) /* {{{ */
833
141
{
834
141
  if (UNEXPECTED(Z_TYPE_P(op) == IS_DOUBLE && zend_isnan(Z_DVAL_P(op)))) {
835
5
    zend_nan_coerced_to_type_warning(IS_ARRAY);
836
5
  }
837
141
  HashTable *ht = zend_new_array(1);
838
141
  zend_hash_index_add_new(ht, 0, op);
839
141
  ZVAL_ARR(op, ht);
840
141
}
841
/* }}} */
842
843
ZEND_API void ZEND_FASTCALL convert_to_array(zval *op) /* {{{ */
844
291
{
845
291
try_again:
846
291
  switch (Z_TYPE_P(op)) {
847
68
    case IS_ARRAY:
848
68
      break;
849
/* OBJECTS_OPTIMIZE */
850
60
    case IS_OBJECT:
851
60
      if (Z_OBJCE_P(op) == zend_ce_closure) {
852
5
        convert_scalar_to_array(op);
853
55
      } else if (ZEND_STD_BUILD_OBJECT_PROPERTIES_ARRAY_COMPATIBLE(op)) {
854
        /* Optimized version without rebuilding properties HashTable */
855
31
        HashTable *ht = zend_std_build_object_properties_array(Z_OBJ_P(op));
856
31
        OBJ_RELEASE(Z_OBJ_P(op));
857
31
        ZVAL_ARR(op, ht);
858
31
      } else {
859
24
        HashTable *obj_ht = zend_get_properties_for(op, ZEND_PROP_PURPOSE_ARRAY_CAST);
860
24
        if (obj_ht) {
861
24
          HashTable *new_obj_ht = zend_proptable_to_symtable(obj_ht,
862
24
            (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
24
          zval_ptr_dtor(op);
866
24
          ZVAL_ARR(op, new_obj_ht);
867
24
          zend_release_properties(obj_ht);
868
24
        } else {
869
0
          zval_ptr_dtor(op);
870
          /*ZVAL_EMPTY_ARRAY(op);*/
871
0
          array_init(op);
872
0
        }
873
24
      }
874
60
      break;
875
27
    case IS_NULL:
876
      /*ZVAL_EMPTY_ARRAY(op);*/
877
27
      array_init(op);
878
27
      break;
879
0
    case IS_REFERENCE:
880
0
      zend_unwrap_reference(op);
881
0
      goto try_again;
882
136
    default:
883
136
      convert_scalar_to_array(op);
884
136
      break;
885
291
  }
886
291
}
887
/* }}} */
888
889
ZEND_API void ZEND_FASTCALL convert_to_object(zval *op) /* {{{ */
890
5
{
891
5
try_again:
892
5
  switch (Z_TYPE_P(op)) {
893
0
    case IS_ARRAY:
894
0
      {
895
0
        HashTable *ht = zend_symtable_to_proptable(Z_ARR_P(op));
896
0
        zend_object *obj;
897
898
0
        if (GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE) {
899
          /* TODO: try not to duplicate immutable arrays as well ??? */
900
0
          ht = zend_array_dup(ht);
901
0
        } else if (ht != Z_ARR_P(op)) {
902
0
          zval_ptr_dtor(op);
903
0
        } else {
904
0
          GC_DELREF(ht);
905
0
        }
906
0
        obj = zend_objects_new(zend_standard_class_def);
907
0
        obj->properties = ht;
908
0
        ZVAL_OBJ(op, obj);
909
0
        break;
910
0
      }
911
0
    case IS_OBJECT:
912
0
      break;
913
0
    case IS_NULL:
914
0
      object_init(op);
915
0
      break;
916
0
    case IS_REFERENCE:
917
0
      zend_unwrap_reference(op);
918
0
      goto try_again;
919
5
    case IS_DOUBLE:
920
5
      if (UNEXPECTED(zend_isnan(Z_DVAL_P(op)))) {
921
5
        zend_nan_coerced_to_type_warning(IS_OBJECT);
922
5
      }
923
5
      ZEND_FALLTHROUGH;
924
5
    default: {
925
5
      zval tmp;
926
5
      ZVAL_COPY_VALUE(&tmp, op);
927
5
      object_init(op);
928
5
      zend_hash_add_new(Z_OBJPROP_P(op), ZSTR_KNOWN(ZEND_STR_SCALAR), &tmp);
929
5
      break;
930
5
    }
931
5
  }
932
5
}
933
/* }}} */
934
935
ZEND_API void ZEND_COLD zend_incompatible_double_to_long_error(double d)
936
43.3k
{
937
43.3k
  zend_error_unchecked(E_DEPRECATED, "Implicit conversion from float %.*H to int loses precision", -1, d);
938
43.3k
}
939
ZEND_API void ZEND_COLD zend_incompatible_string_to_long_error(const zend_string *s)
940
1.59k
{
941
1.59k
  zend_error(E_DEPRECATED, "Implicit conversion from float-string \"%s\" to int loses precision", ZSTR_VAL(s));
942
1.59k
}
943
ZEND_API void ZEND_COLD zend_oob_double_to_long_error(double d)
944
28.7k
{
945
28.7k
  zend_error_unchecked(E_WARNING, "The float %.*H is not representable as an int, cast occurred", -1, d);
946
28.7k
}
947
ZEND_API void ZEND_COLD zend_oob_string_to_long_error(const zend_string *s)
948
4.09k
{
949
4.09k
  zend_error_unchecked(E_WARNING, "The float-string \"%s\" is not representable as an int, cast occurred", ZSTR_VAL(s));
950
4.09k
}
951
952
ZEND_API void ZEND_COLD zend_nan_coerced_to_type_warning(uint8_t type)
953
460
{
954
460
  zend_error(E_WARNING, "unexpected NAN value was coerced to %s", zend_get_type_by_const(type));
955
460
}
956
957
ZEND_API zend_long ZEND_FASTCALL zval_get_long_func(const zval *op, bool is_strict) /* {{{ */
958
32.6k
{
959
32.7k
try_again:
960
32.7k
  switch (Z_TYPE_P(op)) {
961
250
    case IS_UNDEF:
962
752
    case IS_NULL:
963
2.11k
    case IS_FALSE:
964
2.11k
      return 0;
965
873
    case IS_TRUE:
966
873
      return 1;
967
0
    case IS_RESOURCE:
968
0
      return Z_RES_HANDLE_P(op);
969
14
    case IS_LONG:
970
14
      return Z_LVAL_P(op);
971
11.7k
    case IS_DOUBLE: {
972
11.7k
      double dval = Z_DVAL_P(op);
973
11.7k
      zend_long lval = zend_dval_to_lval(dval);
974
11.7k
      if (UNEXPECTED(is_strict)) {
975
127
        if (!zend_is_long_compatible(dval, lval)) {
976
127
          zend_incompatible_double_to_long_error(dval);
977
127
        }
978
127
      }
979
11.7k
      return lval;
980
752
    }
981
17.8k
    case IS_STRING:
982
17.8k
      {
983
17.8k
        uint8_t type;
984
17.8k
        zend_long lval;
985
17.8k
        double dval;
986
17.8k
        if (0 == (type = is_numeric_string(Z_STRVAL_P(op), Z_STRLEN_P(op), &lval, &dval, true))) {
987
2.06k
          return 0;
988
15.8k
        } else if (EXPECTED(type == IS_LONG)) {
989
9.36k
          return lval;
990
9.36k
        } else {
991
          /* Previously we used strtol here, not is_numeric_string,
992
           * and strtol gives you LONG_MAX/_MIN on overflow.
993
           * We use saturating conversion to emulate strtol()'s
994
           * behaviour.
995
           */
996
           /* Most usages are expected to not be (int) casts */
997
6.45k
          lval = zend_dval_to_lval_cap(dval, Z_STR_P(op));
998
6.45k
          if (UNEXPECTED(is_strict)) {
999
0
            if (!zend_is_long_compatible(dval, lval)) {
1000
0
              zend_incompatible_string_to_long_error(Z_STR_P(op));
1001
0
            }
1002
0
          }
1003
6.45k
          return lval;
1004
6.45k
        }
1005
17.8k
      }
1006
11
    case IS_ARRAY:
1007
11
      return zend_hash_num_elements(Z_ARRVAL_P(op)) ? 1 : 0;
1008
28
    case IS_OBJECT:
1009
28
      {
1010
28
        zval dst;
1011
28
        convert_object_to_type(op, &dst, IS_LONG);
1012
28
        if (Z_TYPE(dst) == IS_LONG) {
1013
0
          return Z_LVAL(dst);
1014
28
        } else {
1015
28
          return 1;
1016
28
        }
1017
28
      }
1018
14
    case IS_REFERENCE:
1019
14
      op = Z_REFVAL_P(op);
1020
14
      goto try_again;
1021
32.7k
    EMPTY_SWITCH_DEFAULT_CASE()
1022
32.7k
  }
1023
0
  return 0;
1024
32.7k
}
1025
/* }}} */
1026
1027
ZEND_API double ZEND_FASTCALL zval_get_double_func(const zval *op) /* {{{ */
1028
34.0k
{
1029
40.8k
try_again:
1030
40.8k
  switch (Z_TYPE_P(op)) {
1031
563
    case IS_NULL:
1032
1.15k
    case IS_FALSE:
1033
1.15k
      return 0.0;
1034
386
    case IS_TRUE:
1035
386
      return 1.0;
1036
0
    case IS_RESOURCE:
1037
0
      return (double) Z_RES_HANDLE_P(op);
1038
19.1k
    case IS_LONG:
1039
19.1k
      return (double) Z_LVAL_P(op);
1040
2.80k
    case IS_DOUBLE:
1041
2.80k
      return Z_DVAL_P(op);
1042
2.02k
    case IS_STRING:
1043
2.02k
      return zend_strtod(Z_STRVAL_P(op), NULL);
1044
8.22k
    case IS_ARRAY:
1045
8.22k
      return zend_hash_num_elements(Z_ARRVAL_P(op)) ? 1.0 : 0.0;
1046
274
    case IS_OBJECT:
1047
274
      {
1048
274
        zval dst;
1049
274
        convert_object_to_type(op, &dst, IS_DOUBLE);
1050
1051
274
        if (Z_TYPE(dst) == IS_DOUBLE) {
1052
0
          return Z_DVAL(dst);
1053
274
        } else {
1054
274
          return 1.0;
1055
274
        }
1056
274
      }
1057
6.87k
    case IS_REFERENCE:
1058
6.87k
      op = Z_REFVAL_P(op);
1059
6.87k
      goto try_again;
1060
40.8k
    EMPTY_SWITCH_DEFAULT_CASE()
1061
40.8k
  }
1062
0
  return 0.0;
1063
40.8k
}
1064
/* }}} */
1065
1066
static zend_always_inline zend_string* __zval_get_string_func(zval *op, bool try) /* {{{ */
1067
851k
{
1068
857k
try_again:
1069
857k
  switch (Z_TYPE_P(op)) {
1070
378k
    case IS_UNDEF:
1071
571k
    case IS_NULL:
1072
594k
    case IS_FALSE:
1073
594k
      return ZSTR_EMPTY_ALLOC();
1074
14.6k
    case IS_TRUE:
1075
14.6k
      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
161k
    case IS_LONG:
1079
161k
      return zend_long_to_str(Z_LVAL_P(op));
1080
70.0k
    case IS_DOUBLE:
1081
70.0k
      return zend_double_to_str(Z_DVAL_P(op));
1082
2.77k
    case IS_ARRAY:
1083
2.77k
      zend_error(E_WARNING, "Array to string conversion");
1084
2.77k
      return (try && UNEXPECTED(EG(exception))) ?
1085
2.77k
        NULL : ZSTR_KNOWN(ZEND_STR_ARRAY_CAPITALIZED);
1086
6.93k
    case IS_OBJECT: {
1087
6.93k
      zval tmp;
1088
6.93k
      if (Z_OBJ_HT_P(op)->cast_object(Z_OBJ_P(op), &tmp, IS_STRING) == SUCCESS) {
1089
3.84k
        return Z_STR(tmp);
1090
3.84k
      }
1091
3.08k
      if (!EG(exception)) {
1092
881
        zend_throw_error(NULL, "Object of class %s could not be converted to string", ZSTR_VAL(Z_OBJCE_P(op)->name));
1093
881
      }
1094
3.08k
      return try ? NULL : ZSTR_EMPTY_ALLOC();
1095
6.93k
    }
1096
5.69k
    case IS_REFERENCE:
1097
5.69k
      op = Z_REFVAL_P(op);
1098
5.69k
      goto try_again;
1099
784
    case IS_STRING:
1100
784
      return zend_string_copy(Z_STR_P(op));
1101
857k
    EMPTY_SWITCH_DEFAULT_CASE()
1102
857k
  }
1103
0
  return NULL;
1104
857k
}
1105
/* }}} */
1106
1107
ZEND_API zend_string* ZEND_FASTCALL zval_get_string_func(zval *op) /* {{{ */
1108
623k
{
1109
623k
  return __zval_get_string_func(op, false);
1110
623k
}
1111
/* }}} */
1112
1113
ZEND_API zend_string* ZEND_FASTCALL zval_try_get_string_func(zval *op) /* {{{ */
1114
227k
{
1115
227k
  return __zval_get_string_func(op, true);
1116
227k
}
1117
/* }}} */
1118
1119
1.54k
static ZEND_COLD zend_never_inline void ZEND_FASTCALL zend_binop_error(const char *operator, zval *op1, zval *op2) /* {{{ */ {
1120
1.54k
  if (EG(exception)) {
1121
0
    return;
1122
0
  }
1123
1124
1.54k
  zend_type_error("Unsupported operand types: %s %s %s",
1125
1.54k
    zend_zval_type_name(op1), operator, zend_zval_type_name(op2));
1126
1.54k
}
1127
/* }}} */
1128
1129
static zend_never_inline void ZEND_FASTCALL add_function_array(zval *result, zval *op1, zval *op2) /* {{{ */
1130
5.78k
{
1131
5.78k
  if (result == op1 && Z_ARR_P(op1) == Z_ARR_P(op2)) {
1132
    /* $a += $a */
1133
419
    return;
1134
419
  }
1135
5.36k
  if (result != op1) {
1136
5.12k
    ZVAL_ARR(result, zend_array_dup(Z_ARR_P(op1)));
1137
5.12k
  } else {
1138
241
    SEPARATE_ARRAY(result);
1139
241
  }
1140
5.36k
  zend_hash_merge(Z_ARRVAL_P(result), Z_ARRVAL_P(op2), zval_add_ref, 0);
1141
5.36k
}
1142
/* }}} */
1143
1144
static zend_always_inline zend_result add_function_fast(zval *result, zval *op1, zval *op2) /* {{{ */
1145
112k
{
1146
112k
  uint8_t type_pair = TYPE_PAIR(Z_TYPE_P(op1), Z_TYPE_P(op2));
1147
1148
112k
  if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_LONG))) {
1149
34.2k
    fast_long_add_function(result, op1, op2);
1150
34.2k
    return SUCCESS;
1151
78.6k
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_DOUBLE, IS_DOUBLE))) {
1152
3.74k
    ZVAL_DOUBLE(result, Z_DVAL_P(op1) + Z_DVAL_P(op2));
1153
3.74k
    return SUCCESS;
1154
74.8k
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_DOUBLE))) {
1155
2.65k
    ZVAL_DOUBLE(result, ((double)Z_LVAL_P(op1)) + Z_DVAL_P(op2));
1156
2.65k
    return SUCCESS;
1157
72.2k
  } 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
68.4k
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_ARRAY, IS_ARRAY))) {
1161
5.78k
    add_function_array(result, op1, op2);
1162
5.78k
    return SUCCESS;
1163
62.6k
  } else {
1164
62.6k
    return FAILURE;
1165
62.6k
  }
1166
112k
} /* }}} */
1167
1168
static zend_never_inline zend_result ZEND_FASTCALL add_function_slow(zval *result, zval *op1, zval *op2) /* {{{ */
1169
33.8k
{
1170
33.8k
  ZVAL_DEREF(op1);
1171
33.8k
  ZVAL_DEREF(op2);
1172
33.8k
  if (add_function_fast(result, op1, op2) == SUCCESS) {
1173
4.97k
    return SUCCESS;
1174
4.97k
  }
1175
1176
33.8k
  ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_ADD);
1177
1178
28.8k
  zval op1_copy, op2_copy;
1179
28.8k
  if (UNEXPECTED(zendi_try_convert_scalar_to_number(op1, &op1_copy) == FAILURE)
1180
28.6k
      || UNEXPECTED(zendi_try_convert_scalar_to_number(op2, &op2_copy) == FAILURE)) {
1181
200
    zend_binop_error("+", op1, op2);
1182
200
    if (result != op1) {
1183
156
      ZVAL_UNDEF(result);
1184
156
    }
1185
200
    return FAILURE;
1186
200
  }
1187
1188
28.6k
  if (result == op1) {
1189
5.91k
    zval_ptr_dtor(result);
1190
5.91k
  }
1191
1192
28.6k
  if (add_function_fast(result, &op1_copy, &op2_copy) == SUCCESS) {
1193
28.6k
    return SUCCESS;
1194
28.6k
  }
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
50.4k
{
1202
50.4k
  if (add_function_fast(result, op1, op2) == SUCCESS) {
1203
16.6k
    return SUCCESS;
1204
33.8k
  } else {
1205
33.8k
    return add_function_slow(result, op1, op2);
1206
33.8k
  }
1207
50.4k
}
1208
/* }}} */
1209
1210
static zend_always_inline zend_result sub_function_fast(zval *result, zval *op1, zval *op2) /* {{{ */
1211
39.7k
{
1212
39.7k
  uint8_t type_pair = TYPE_PAIR(Z_TYPE_P(op1), Z_TYPE_P(op2));
1213
1214
39.7k
  if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_LONG))) {
1215
12.9k
    fast_long_sub_function(result, op1, op2);
1216
12.9k
    return SUCCESS;
1217
26.8k
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_DOUBLE, IS_DOUBLE))) {
1218
1.45k
    ZVAL_DOUBLE(result, Z_DVAL_P(op1) - Z_DVAL_P(op2));
1219
1.45k
    return SUCCESS;
1220
25.3k
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_DOUBLE))) {
1221
3.20k
    ZVAL_DOUBLE(result, ((double)Z_LVAL_P(op1)) - Z_DVAL_P(op2));
1222
3.20k
    return SUCCESS;
1223
22.1k
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_DOUBLE, IS_LONG))) {
1224
2.05k
    ZVAL_DOUBLE(result, Z_DVAL_P(op1) - ((double)Z_LVAL_P(op2)));
1225
2.05k
    return SUCCESS;
1226
20.1k
  } else {
1227
20.1k
    return FAILURE;
1228
20.1k
  }
1229
39.7k
}
1230
/* }}} */
1231
1232
static zend_never_inline zend_result ZEND_FASTCALL sub_function_slow(zval *result, zval *op1, zval *op2) /* {{{ */
1233
10.6k
{
1234
10.6k
  ZVAL_DEREF(op1);
1235
10.6k
  ZVAL_DEREF(op2);
1236
10.6k
  if (sub_function_fast(result, op1, op2) == SUCCESS) {
1237
1.27k
    return SUCCESS;
1238
1.27k
  }
1239
1240
10.6k
  ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_SUB);
1241
1242
9.42k
  zval op1_copy, op2_copy;
1243
9.42k
  if (UNEXPECTED(zendi_try_convert_scalar_to_number(op1, &op1_copy) == FAILURE)
1244
9.31k
      || UNEXPECTED(zendi_try_convert_scalar_to_number(op2, &op2_copy) == FAILURE)) {
1245
216
    zend_binop_error("-", op1, op2);
1246
216
    if (result != op1) {
1247
171
      ZVAL_UNDEF(result);
1248
171
    }
1249
216
    return FAILURE;
1250
216
  }
1251
1252
9.20k
  if (result == op1) {
1253
941
    zval_ptr_dtor(result);
1254
941
  }
1255
1256
9.20k
  if (sub_function_fast(result, &op1_copy, &op2_copy) == SUCCESS) {
1257
9.20k
    return SUCCESS;
1258
9.20k
  }
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
19.8k
{
1267
19.8k
  if (sub_function_fast(result, op1, op2) == SUCCESS) {
1268
9.18k
    return SUCCESS;
1269
10.6k
  } else {
1270
10.6k
    return sub_function_slow(result, op1, op2);
1271
10.6k
  }
1272
19.8k
}
1273
/* }}} */
1274
1275
static zend_always_inline zend_result mul_function_fast(zval *result, zval *op1, zval *op2) /* {{{ */
1276
161k
{
1277
161k
  uint8_t type_pair = TYPE_PAIR(Z_TYPE_P(op1), Z_TYPE_P(op2));
1278
1279
161k
  if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_LONG))) {
1280
70.4k
    zend_long overflow;
1281
70.4k
    ZEND_SIGNED_MULTIPLY_LONG(
1282
70.4k
      Z_LVAL_P(op1), Z_LVAL_P(op2),
1283
70.4k
      Z_LVAL_P(result), Z_DVAL_P(result), overflow);
1284
70.4k
    Z_TYPE_INFO_P(result) = overflow ? IS_DOUBLE : IS_LONG;
1285
70.4k
    return SUCCESS;
1286
91.4k
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_DOUBLE, IS_DOUBLE))) {
1287
1.64k
    ZVAL_DOUBLE(result, Z_DVAL_P(op1) * Z_DVAL_P(op2));
1288
1.64k
    return SUCCESS;
1289
89.8k
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_DOUBLE))) {
1290
1.04k
    ZVAL_DOUBLE(result, ((double)Z_LVAL_P(op1)) * Z_DVAL_P(op2));
1291
1.04k
    return SUCCESS;
1292
88.8k
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_DOUBLE, IS_LONG))) {
1293
8.45k
    ZVAL_DOUBLE(result, Z_DVAL_P(op1) * ((double)Z_LVAL_P(op2)));
1294
8.45k
    return SUCCESS;
1295
80.3k
  } else {
1296
80.3k
    return FAILURE;
1297
80.3k
  }
1298
161k
}
1299
/* }}} */
1300
1301
static zend_never_inline zend_result ZEND_FASTCALL mul_function_slow(zval *result, zval *op1, zval *op2) /* {{{ */
1302
40.6k
{
1303
40.6k
  ZVAL_DEREF(op1);
1304
40.6k
  ZVAL_DEREF(op2);
1305
40.6k
  if (mul_function_fast(result, op1, op2) == SUCCESS) {
1306
891
    return SUCCESS;
1307
891
  }
1308
1309
40.6k
  ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_MUL);
1310
1311
39.7k
  zval op1_copy, op2_copy;
1312
39.7k
  if (UNEXPECTED(zendi_try_convert_scalar_to_number(op1, &op1_copy) == FAILURE)
1313
39.5k
      || UNEXPECTED(zendi_try_convert_scalar_to_number(op2, &op2_copy) == FAILURE)) {
1314
255
    zend_binop_error("*", op1, op2);
1315
255
    if (result != op1) {
1316
203
      ZVAL_UNDEF(result);
1317
203
    }
1318
255
    return FAILURE;
1319
255
  }
1320
1321
39.4k
  if (result == op1) {
1322
3.00k
    zval_ptr_dtor(result);
1323
3.00k
  }
1324
1325
39.4k
  if (mul_function_fast(result, &op1_copy, &op2_copy) == SUCCESS) {
1326
39.4k
    return SUCCESS;
1327
39.4k
  }
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
81.8k
{
1336
81.8k
  if (mul_function_fast(result, op1, op2) == SUCCESS) {
1337
41.1k
    return SUCCESS;
1338
41.1k
  } else {
1339
40.6k
    return mul_function_slow(result, op1, op2);
1340
40.6k
  }
1341
81.8k
}
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
3.96k
{
1351
3.96k
  if (UNEXPECTED(base == 0.0 && exponent < 0.0)) {
1352
0
    zend_power_base_0_exponent_lt_0_error();
1353
0
  }
1354
1355
3.96k
  return pow(base, exponent);
1356
3.96k
}
1357
1358
static zend_result ZEND_FASTCALL pow_function_base(zval *result, zval *op1, zval *op2) /* {{{ */
1359
6.07k
{
1360
6.07k
  uint8_t type_pair = TYPE_PAIR(Z_TYPE_P(op1), Z_TYPE_P(op2));
1361
1362
6.07k
  if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_LONG))) {
1363
4.14k
    if (Z_LVAL_P(op2) >= 0) {
1364
3.80k
      zend_long l1 = 1, l2 = Z_LVAL_P(op1), i = Z_LVAL_P(op2);
1365
1366
3.80k
      if (i == 0) {
1367
369
        ZVAL_LONG(result, 1L);
1368
369
        return SUCCESS;
1369
3.43k
      } else if (l2 == 0) {
1370
335
        ZVAL_LONG(result, 0);
1371
335
        return SUCCESS;
1372
335
      }
1373
1374
19.3k
      while (i >= 1) {
1375
18.2k
        zend_long overflow;
1376
18.2k
        double dval = 0.0;
1377
1378
18.2k
        if (i % 2) {
1379
6.80k
          --i;
1380
6.80k
          ZEND_SIGNED_MULTIPLY_LONG(l1, l2, l1, dval, overflow);
1381
6.80k
          if (overflow) {
1382
1.22k
            ZVAL_DOUBLE(result, dval * safe_pow(l2, i));
1383
1.22k
            return SUCCESS;
1384
1.22k
          }
1385
11.4k
        } else {
1386
11.4k
          i /= 2;
1387
11.4k
          ZEND_SIGNED_MULTIPLY_LONG(l2, l2, l2, dval, overflow);
1388
11.4k
          if (overflow) {
1389
790
            ZVAL_DOUBLE(result, (double)l1 * safe_pow(dval, i));
1390
790
            return SUCCESS;
1391
790
          }
1392
11.4k
        }
1393
18.2k
      }
1394
      /* i == 0 */
1395
1.08k
      ZVAL_LONG(result, l1);
1396
1.08k
    } else {
1397
344
      ZVAL_DOUBLE(result, safe_pow((double)Z_LVAL_P(op1), (double)Z_LVAL_P(op2)));
1398
344
    }
1399
1.42k
    return SUCCESS;
1400
4.14k
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_DOUBLE, IS_DOUBLE))) {
1401
1.07k
    ZVAL_DOUBLE(result, safe_pow(Z_DVAL_P(op1), Z_DVAL_P(op2)));
1402
1.07k
    return SUCCESS;
1403
1.07k
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_DOUBLE))) {
1404
264
    ZVAL_DOUBLE(result, safe_pow((double)Z_LVAL_P(op1), Z_DVAL_P(op2)));
1405
264
    return SUCCESS;
1406
592
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_DOUBLE, IS_LONG))) {
1407
269
    ZVAL_DOUBLE(result, safe_pow(Z_DVAL_P(op1), (double)Z_LVAL_P(op2)));
1408
269
    return SUCCESS;
1409
323
  } else {
1410
323
    return FAILURE;
1411
323
  }
1412
6.07k
}
1413
/* }}} */
1414
1415
ZEND_API zend_result ZEND_FASTCALL pow_function(zval *result, zval *op1, zval *op2) /* {{{ */
1416
5.79k
{
1417
5.79k
  ZVAL_DEREF(op1);
1418
5.79k
  ZVAL_DEREF(op2);
1419
5.79k
  if (pow_function_base(result, op1, op2) == SUCCESS) {
1420
5.47k
    return SUCCESS;
1421
5.47k
  }
1422
1423
5.79k
  ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_POW);
1424
1425
323
  zval op1_copy, op2_copy;
1426
323
  if (UNEXPECTED(zendi_try_convert_scalar_to_number(op1, &op1_copy) == FAILURE)
1427
278
      || UNEXPECTED(zendi_try_convert_scalar_to_number(op2, &op2_copy) == FAILURE)) {
1428
45
    zend_binop_error("**", op1, op2);
1429
45
    if (result != op1) {
1430
12
      ZVAL_UNDEF(result);
1431
12
    }
1432
45
    return FAILURE;
1433
45
  }
1434
1435
278
  if (result == op1) {
1436
21
    zval_ptr_dtor(result);
1437
21
  }
1438
1439
278
  if (pow_function_base(result, &op1_copy, &op2_copy) == SUCCESS) {
1440
278
    return SUCCESS;
1441
278
  }
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
11.4k
{
1456
11.4k
  uint8_t type_pair = TYPE_PAIR(Z_TYPE_P(op1), Z_TYPE_P(op2));
1457
1458
11.4k
  if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_LONG))) {
1459
6.33k
    if (Z_LVAL_P(op2) == 0) {
1460
81
      return DIV_BY_ZERO;
1461
6.25k
    } else if (Z_LVAL_P(op2) == -1 && Z_LVAL_P(op1) == ZEND_LONG_MIN) {
1462
      /* Prevent overflow error/crash */
1463
28
      ZVAL_DOUBLE(result, (double) ZEND_LONG_MIN / -1);
1464
28
      return DIV_SUCCESS;
1465
28
    }
1466
6.23k
    if (Z_LVAL_P(op1) % Z_LVAL_P(op2) == 0) { /* integer */
1467
1.33k
      ZVAL_LONG(result, Z_LVAL_P(op1) / Z_LVAL_P(op2));
1468
4.89k
    } else {
1469
4.89k
      ZVAL_DOUBLE(result, ((double) Z_LVAL_P(op1)) / Z_LVAL_P(op2));
1470
4.89k
    }
1471
6.23k
    return DIV_SUCCESS;
1472
6.33k
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_DOUBLE, IS_DOUBLE))) {
1473
714
    if (Z_DVAL_P(op2) == 0) {
1474
7
      return DIV_BY_ZERO;
1475
7
    }
1476
707
    ZVAL_DOUBLE(result, Z_DVAL_P(op1) / Z_DVAL_P(op2));
1477
707
    return DIV_SUCCESS;
1478
4.36k
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_DOUBLE, IS_LONG))) {
1479
1.46k
    if (Z_LVAL_P(op2) == 0) {
1480
13
      return DIV_BY_ZERO;
1481
13
    }
1482
1.45k
    ZVAL_DOUBLE(result, Z_DVAL_P(op1) / (double)Z_LVAL_P(op2));
1483
1.45k
    return DIV_SUCCESS;
1484
2.89k
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_DOUBLE))) {
1485
669
    if (Z_DVAL_P(op2) == 0) {
1486
10
      return DIV_BY_ZERO;
1487
10
    }
1488
659
    ZVAL_DOUBLE(result, (double)Z_LVAL_P(op1) / Z_DVAL_P(op2));
1489
659
    return DIV_SUCCESS;
1490
2.22k
  } else {
1491
2.22k
    return DIV_TYPES_NOT_HANDLED;
1492
2.22k
  }
1493
11.4k
}
1494
/* }}} */
1495
1496
ZEND_API zend_result ZEND_FASTCALL div_function(zval *result, zval *op1, zval *op2) /* {{{ */
1497
9.30k
{
1498
9.30k
  ZVAL_DEREF(op1);
1499
9.30k
  ZVAL_DEREF(op2);
1500
1501
9.30k
  zend_div_status retval = div_function_base(result, op1, op2);
1502
9.30k
  if (EXPECTED(retval == DIV_SUCCESS)) {
1503
7.03k
    return SUCCESS;
1504
7.03k
  }
1505
1506
2.26k
  if (UNEXPECTED(retval == DIV_BY_ZERO)) {
1507
44
    goto div_by_zero;
1508
44
  }
1509
1510
2.26k
  ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_DIV);
1511
1512
2.22k
  zval result_copy, op1_copy, op2_copy;
1513
2.22k
  if (UNEXPECTED(zendi_try_convert_scalar_to_number(op1, &op1_copy) == FAILURE)
1514
2.12k
      || UNEXPECTED(zendi_try_convert_scalar_to_number(op2, &op2_copy) == FAILURE)) {
1515
115
    zend_binop_error("/", op1, op2);
1516
115
    if (result != op1) {
1517
86
      ZVAL_UNDEF(result);
1518
86
    }
1519
115
    return FAILURE;
1520
115
  }
1521
1522
2.10k
  retval = div_function_base(&result_copy, &op1_copy, &op2_copy);
1523
2.10k
  if (retval == DIV_SUCCESS) {
1524
2.04k
    if (result == op1) {
1525
289
      zval_ptr_dtor(result);
1526
289
    }
1527
2.04k
    ZVAL_COPY_VALUE(result, &result_copy);
1528
2.04k
    return SUCCESS;
1529
2.04k
  }
1530
1531
111
div_by_zero:
1532
111
  ZEND_ASSERT(retval == DIV_BY_ZERO && "DIV_TYPES_NOT_HANDLED should not occur here");
1533
111
  if (result != op1) {
1534
89
    ZVAL_UNDEF(result);
1535
89
  }
1536
111
  zend_throw_error(zend_ce_division_by_zero_error, "Division by zero");
1537
111
  return FAILURE;
1538
111
}
1539
/* }}} */
1540
1541
ZEND_API zend_result ZEND_FASTCALL mod_function(zval *result, zval *op1, zval *op2) /* {{{ */
1542
20.6k
{
1543
20.6k
  zend_long op1_lval, op2_lval;
1544
1545
20.6k
  convert_op1_op2_long(op1, op1_lval, op2, op2_lval, result, ZEND_MOD, "%");
1546
1547
20.5k
  if (op2_lval == 0) {
1548
    /* modulus by zero */
1549
151
    if (EG(current_execute_data) && !CG(in_compilation)) {
1550
151
      zend_throw_exception_ex(zend_ce_division_by_zero_error, 0, "Modulo by zero");
1551
151
    } else {
1552
0
      zend_error_noreturn(E_ERROR, "Modulo by zero");
1553
0
    }
1554
151
    if (op1 != result) {
1555
97
      ZVAL_UNDEF(result);
1556
97
    }
1557
151
    return FAILURE;
1558
151
  }
1559
1560
20.3k
  if (op1 == result) {
1561
199
    zval_ptr_dtor(result);
1562
199
  }
1563
1564
20.3k
  if (op2_lval == -1) {
1565
    /* Prevent overflow error/crash if op1==LONG_MIN */
1566
246
    ZVAL_LONG(result, 0);
1567
246
    return SUCCESS;
1568
246
  }
1569
1570
20.1k
  ZVAL_LONG(result, op1_lval % op2_lval);
1571
20.1k
  return SUCCESS;
1572
20.3k
}
1573
/* }}} */
1574
1575
ZEND_API zend_result ZEND_FASTCALL boolean_xor_function(zval *result, zval *op1, zval *op2) /* {{{ */
1576
7.99k
{
1577
7.99k
  int op1_val, op2_val;
1578
1579
7.99k
  do {
1580
7.99k
    if (Z_TYPE_P(op1) == IS_FALSE) {
1581
676
      op1_val = 0;
1582
7.31k
    } else if (EXPECTED(Z_TYPE_P(op1) == IS_TRUE)) {
1583
560
      op1_val = 1;
1584
6.75k
    } else {
1585
6.75k
      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
6.75k
      ZEND_TRY_BINARY_OP1_OBJECT_OPERATION(ZEND_BOOL_XOR);
1596
6.75k
      op1_val = zend_is_true(op1);
1597
6.75k
    }
1598
7.99k
  } while (0);
1599
7.99k
  do {
1600
7.99k
    if (Z_TYPE_P(op2) == IS_FALSE) {
1601
425
      op2_val = 0;
1602
7.56k
    } else if (EXPECTED(Z_TYPE_P(op2) == IS_TRUE)) {
1603
231
      op2_val = 1;
1604
7.33k
    } else {
1605
7.33k
      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.33k
      ZEND_TRY_BINARY_OP2_OBJECT_OPERATION(ZEND_BOOL_XOR);
1616
7.33k
      op2_val = zend_is_true(op2);
1617
7.33k
    }
1618
7.99k
  } while (0);
1619
1620
7.99k
  ZVAL_BOOL(result, op1_val ^ op2_val);
1621
7.99k
  return SUCCESS;
1622
7.99k
}
1623
/* }}} */
1624
1625
ZEND_API zend_result ZEND_FASTCALL boolean_not_function(zval *result, zval *op1) /* {{{ */
1626
50.6k
{
1627
50.6k
  if (Z_TYPE_P(op1) < IS_TRUE) {
1628
17.0k
    ZVAL_TRUE(result);
1629
33.5k
  } else if (EXPECTED(Z_TYPE_P(op1) == IS_TRUE)) {
1630
1.54k
    ZVAL_FALSE(result);
1631
32.0k
  } else {
1632
32.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
32.0k
    ZEND_TRY_UNARY_OBJECT_OPERATION(ZEND_BOOL_NOT);
1643
1644
32.0k
    ZVAL_BOOL(result, !zend_is_true(op1));
1645
32.0k
  }
1646
50.6k
  return SUCCESS;
1647
50.6k
}
1648
/* }}} */
1649
1650
ZEND_API zend_result ZEND_FASTCALL bitwise_not_function(zval *result, zval *op1) /* {{{ */
1651
9.34k
{
1652
9.35k
try_again:
1653
9.35k
  switch (Z_TYPE_P(op1)) {
1654
1.87k
    case IS_LONG:
1655
1.87k
      ZVAL_LONG(result, ~Z_LVAL_P(op1));
1656
1.87k
      return SUCCESS;
1657
1.14k
    case IS_DOUBLE: {
1658
1.14k
      zend_long lval = zend_dval_to_lval_safe(Z_DVAL_P(op1));
1659
1.14k
      if (EG(exception)) {
1660
0
        if (result != op1) {
1661
0
          ZVAL_UNDEF(result);
1662
0
        }
1663
0
        return FAILURE;
1664
0
      }
1665
1.14k
      ZVAL_LONG(result, ~lval);
1666
1.14k
      return SUCCESS;
1667
1.14k
    }
1668
6.23k
    case IS_STRING: {
1669
6.23k
      size_t i;
1670
1671
6.23k
      if (Z_STRLEN_P(op1) == 1) {
1672
136
        zend_uchar not = (zend_uchar) ~*Z_STRVAL_P(op1);
1673
136
        ZVAL_CHAR(result, not);
1674
6.09k
      } else {
1675
6.09k
        ZVAL_NEW_STR(result, zend_string_alloc(Z_STRLEN_P(op1), 0));
1676
138M
        for (i = 0; i < Z_STRLEN_P(op1); i++) {
1677
138M
          Z_STRVAL_P(result)[i] = ~Z_STRVAL_P(op1)[i];
1678
138M
        }
1679
6.09k
        Z_STRVAL_P(result)[i] = 0;
1680
6.09k
      }
1681
6.23k
      return SUCCESS;
1682
1.14k
    }
1683
6
    case IS_REFERENCE:
1684
6
      op1 = Z_REFVAL_P(op1);
1685
6
      goto try_again;
1686
93
    default:
1687
93
      ZEND_TRY_UNARY_OBJECT_OPERATION(ZEND_BW_NOT);
1688
1689
93
      if (result != op1) {
1690
93
        ZVAL_UNDEF(result);
1691
93
      }
1692
93
      zend_type_error("Cannot perform bitwise not on %s", zend_zval_value_name(op1));
1693
93
      return FAILURE;
1694
9.35k
  }
1695
9.35k
}
1696
/* }}} */
1697
1698
ZEND_API zend_result ZEND_FASTCALL bitwise_or_function(zval *result, zval *op1, zval *op2) /* {{{ */
1699
19.0k
{
1700
19.0k
  zend_long op1_lval, op2_lval;
1701
1702
19.0k
  if (EXPECTED(Z_TYPE_P(op1) == IS_LONG) && EXPECTED(Z_TYPE_P(op2) == IS_LONG)) {
1703
943
    ZVAL_LONG(result, Z_LVAL_P(op1) | Z_LVAL_P(op2));
1704
943
    return SUCCESS;
1705
943
  }
1706
1707
18.0k
  ZVAL_DEREF(op1);
1708
18.0k
  ZVAL_DEREF(op2);
1709
1710
18.0k
  if (Z_TYPE_P(op1) == IS_STRING && EXPECTED(Z_TYPE_P(op2) == IS_STRING)) {
1711
14.9k
    zval *longer, *shorter;
1712
14.9k
    zend_string *str;
1713
14.9k
    size_t i;
1714
1715
14.9k
    if (EXPECTED(Z_STRLEN_P(op1) >= Z_STRLEN_P(op2))) {
1716
9.44k
      if (EXPECTED(Z_STRLEN_P(op1) == Z_STRLEN_P(op2)) && Z_STRLEN_P(op1) == 1) {
1717
143
        zend_uchar or = (zend_uchar) (*Z_STRVAL_P(op1) | *Z_STRVAL_P(op2));
1718
143
        if (result==op1) {
1719
5
          zval_ptr_dtor_str(result);
1720
5
        }
1721
143
        ZVAL_CHAR(result, or);
1722
143
        return SUCCESS;
1723
143
      }
1724
9.30k
      longer = op1;
1725
9.30k
      shorter = op2;
1726
9.30k
    } else {
1727
5.52k
      longer = op2;
1728
5.52k
      shorter = op1;
1729
5.52k
    }
1730
1731
14.8k
    str = zend_string_alloc(Z_STRLEN_P(longer), 0);
1732
4.20M
    for (i = 0; i < Z_STRLEN_P(shorter); i++) {
1733
4.18M
      ZSTR_VAL(str)[i] = Z_STRVAL_P(longer)[i] | Z_STRVAL_P(shorter)[i];
1734
4.18M
    }
1735
14.8k
    memcpy(ZSTR_VAL(str) + i, Z_STRVAL_P(longer) + i, Z_STRLEN_P(longer) - i + 1);
1736
14.8k
    if (result==op1) {
1737
13
      zval_ptr_dtor_str(result);
1738
13
    }
1739
14.8k
    ZVAL_NEW_STR(result, str);
1740
14.8k
    return SUCCESS;
1741
14.9k
  }
1742
1743
3.10k
  if (UNEXPECTED(Z_TYPE_P(op1) != IS_LONG)) {
1744
1.78k
    bool failed;
1745
1.78k
    ZEND_TRY_BINARY_OP1_OBJECT_OPERATION(ZEND_BW_OR);
1746
1.78k
    op1_lval = zendi_try_get_long(op1, &failed);
1747
1.78k
    if (UNEXPECTED(failed)) {
1748
36
      zend_binop_error("|", op1, op2);
1749
36
      if (result != op1) {
1750
21
        ZVAL_UNDEF(result);
1751
21
      }
1752
36
      return FAILURE;
1753
36
    }
1754
1.78k
  } else {
1755
1.32k
    op1_lval = Z_LVAL_P(op1);
1756
1.32k
  }
1757
3.06k
  if (UNEXPECTED(Z_TYPE_P(op2) != IS_LONG)) {
1758
1.64k
    bool failed;
1759
1.64k
    ZEND_TRY_BINARY_OP2_OBJECT_OPERATION(ZEND_BW_OR);
1760
1.64k
    op2_lval = zendi_try_get_long(op2, &failed);
1761
1.64k
    if (UNEXPECTED(failed)) {
1762
27
      zend_binop_error("|", op1, op2);
1763
27
      if (result != op1) {
1764
19
        ZVAL_UNDEF(result);
1765
19
      }
1766
27
      return FAILURE;
1767
27
    }
1768
1.64k
  } else {
1769
1.42k
    op2_lval = Z_LVAL_P(op2);
1770
1.42k
  }
1771
1772
3.04k
  if (op1 == result) {
1773
59
    zval_ptr_dtor(result);
1774
59
  }
1775
3.04k
  ZVAL_LONG(result, op1_lval | op2_lval);
1776
3.04k
  return SUCCESS;
1777
3.06k
}
1778
/* }}} */
1779
1780
ZEND_API zend_result ZEND_FASTCALL bitwise_and_function(zval *result, zval *op1, zval *op2) /* {{{ */
1781
309k
{
1782
309k
  zend_long op1_lval, op2_lval;
1783
1784
309k
  if (EXPECTED(Z_TYPE_P(op1) == IS_LONG) && EXPECTED(Z_TYPE_P(op2) == IS_LONG)) {
1785
12.7k
    ZVAL_LONG(result, Z_LVAL_P(op1) & Z_LVAL_P(op2));
1786
12.7k
    return SUCCESS;
1787
12.7k
  }
1788
1789
297k
  ZVAL_DEREF(op1);
1790
297k
  ZVAL_DEREF(op2);
1791
1792
297k
  if (Z_TYPE_P(op1) == IS_STRING && Z_TYPE_P(op2) == IS_STRING) {
1793
276k
    zval *longer, *shorter;
1794
276k
    zend_string *str;
1795
276k
    size_t i;
1796
1797
276k
    if (EXPECTED(Z_STRLEN_P(op1) >= Z_STRLEN_P(op2))) {
1798
74.0k
      if (EXPECTED(Z_STRLEN_P(op1) == Z_STRLEN_P(op2)) && Z_STRLEN_P(op1) == 1) {
1799
71
        zend_uchar and = (zend_uchar) (*Z_STRVAL_P(op1) & *Z_STRVAL_P(op2));
1800
71
        if (result==op1) {
1801
46
          zval_ptr_dtor_str(result);
1802
46
        }
1803
71
        ZVAL_CHAR(result, and);
1804
71
        return SUCCESS;
1805
71
      }
1806
73.9k
      longer = op1;
1807
73.9k
      shorter = op2;
1808
202k
    } else {
1809
202k
      longer = op2;
1810
202k
      shorter = op1;
1811
202k
    }
1812
1813
276k
    str = zend_string_alloc(Z_STRLEN_P(shorter), 0);
1814
2.88G
    for (i = 0; i < Z_STRLEN_P(shorter); i++) {
1815
2.88G
      ZSTR_VAL(str)[i] = Z_STRVAL_P(shorter)[i] & Z_STRVAL_P(longer)[i];
1816
2.88G
    }
1817
276k
    ZSTR_VAL(str)[i] = 0;
1818
276k
    if (result==op1) {
1819
184k
      zval_ptr_dtor_str(result);
1820
184k
    }
1821
276k
    ZVAL_NEW_STR(result, str);
1822
276k
    return SUCCESS;
1823
276k
  }
1824
1825
20.7k
  if (UNEXPECTED(Z_TYPE_P(op1) != IS_LONG)) {
1826
13.1k
    bool failed;
1827
13.1k
    ZEND_TRY_BINARY_OP1_OBJECT_OPERATION(ZEND_BW_AND);
1828
13.1k
    op1_lval = zendi_try_get_long(op1, &failed);
1829
13.1k
    if (UNEXPECTED(failed)) {
1830
38
      zend_binop_error("&", op1, op2);
1831
38
      if (result != op1) {
1832
17
        ZVAL_UNDEF(result);
1833
17
      }
1834
38
      return FAILURE;
1835
38
    }
1836
13.1k
  } else {
1837
7.60k
    op1_lval = Z_LVAL_P(op1);
1838
7.60k
  }
1839
20.7k
  if (UNEXPECTED(Z_TYPE_P(op2) != IS_LONG)) {
1840
12.8k
    bool failed;
1841
12.8k
    ZEND_TRY_BINARY_OP2_OBJECT_OPERATION(ZEND_BW_AND);
1842
12.8k
    op2_lval = zendi_try_get_long(op2, &failed);
1843
12.8k
    if (UNEXPECTED(failed)) {
1844
40
      zend_binop_error("&", op1, op2);
1845
40
      if (result != op1) {
1846
19
        ZVAL_UNDEF(result);
1847
19
      }
1848
40
      return FAILURE;
1849
40
    }
1850
12.8k
  } else {
1851
7.82k
    op2_lval = Z_LVAL_P(op2);
1852
7.82k
  }
1853
1854
20.6k
  if (op1 == result) {
1855
9.57k
    zval_ptr_dtor(result);
1856
9.57k
  }
1857
20.6k
  ZVAL_LONG(result, op1_lval & op2_lval);
1858
20.6k
  return SUCCESS;
1859
20.7k
}
1860
/* }}} */
1861
1862
ZEND_API zend_result ZEND_FASTCALL bitwise_xor_function(zval *result, zval *op1, zval *op2) /* {{{ */
1863
88.0k
{
1864
88.0k
  zend_long op1_lval, op2_lval;
1865
1866
88.0k
  if (EXPECTED(Z_TYPE_P(op1) == IS_LONG) && EXPECTED(Z_TYPE_P(op2) == IS_LONG)) {
1867
1.16k
    ZVAL_LONG(result, Z_LVAL_P(op1) ^ Z_LVAL_P(op2));
1868
1.16k
    return SUCCESS;
1869
1.16k
  }
1870
1871
86.9k
  ZVAL_DEREF(op1);
1872
86.9k
  ZVAL_DEREF(op2);
1873
1874
86.9k
  if (Z_TYPE_P(op1) == IS_STRING && Z_TYPE_P(op2) == IS_STRING) {
1875
56.0k
    zval *longer, *shorter;
1876
56.0k
    zend_string *str;
1877
56.0k
    size_t i;
1878
1879
56.0k
    if (EXPECTED(Z_STRLEN_P(op1) >= Z_STRLEN_P(op2))) {
1880
46.0k
      if (EXPECTED(Z_STRLEN_P(op1) == Z_STRLEN_P(op2)) && Z_STRLEN_P(op1) == 1) {
1881
95
        zend_uchar xor = (zend_uchar) (*Z_STRVAL_P(op1) ^ *Z_STRVAL_P(op2));
1882
95
        if (result==op1) {
1883
8
          zval_ptr_dtor_str(result);
1884
8
        }
1885
95
        ZVAL_CHAR(result, xor);
1886
95
        return SUCCESS;
1887
95
      }
1888
45.9k
      longer = op1;
1889
45.9k
      shorter = op2;
1890
45.9k
    } else {
1891
10.0k
      longer = op2;
1892
10.0k
      shorter = op1;
1893
10.0k
    }
1894
1895
55.9k
    str = zend_string_alloc(Z_STRLEN_P(shorter), 0);
1896
226M
    for (i = 0; i < Z_STRLEN_P(shorter); i++) {
1897
226M
      ZSTR_VAL(str)[i] = Z_STRVAL_P(shorter)[i] ^ Z_STRVAL_P(longer)[i];
1898
226M
    }
1899
55.9k
    ZSTR_VAL(str)[i] = 0;
1900
55.9k
    if (result==op1) {
1901
12
      zval_ptr_dtor_str(result);
1902
12
    }
1903
55.9k
    ZVAL_NEW_STR(result, str);
1904
55.9k
    return SUCCESS;
1905
56.0k
  }
1906
1907
30.8k
  if (UNEXPECTED(Z_TYPE_P(op1) != IS_LONG)) {
1908
1.64k
    bool failed;
1909
1.64k
    ZEND_TRY_BINARY_OP1_OBJECT_OPERATION(ZEND_BW_XOR);
1910
1.64k
    op1_lval = zendi_try_get_long(op1, &failed);
1911
1.64k
    if (UNEXPECTED(failed)) {
1912
33
      zend_binop_error("^", op1, op2);
1913
33
      if (result != op1) {
1914
18
        ZVAL_UNDEF(result);
1915
18
      }
1916
33
      return FAILURE;
1917
33
    }
1918
29.2k
  } else {
1919
29.2k
    op1_lval = Z_LVAL_P(op1);
1920
29.2k
  }
1921
30.8k
  if (UNEXPECTED(Z_TYPE_P(op2) != IS_LONG)) {
1922
29.7k
    bool failed;
1923
29.7k
    ZEND_TRY_BINARY_OP2_OBJECT_OPERATION(ZEND_BW_XOR);
1924
29.7k
    op2_lval = zendi_try_get_long(op2, &failed);
1925
29.7k
    if (UNEXPECTED(failed)) {
1926
27
      zend_binop_error("^", op1, op2);
1927
27
      if (result != op1) {
1928
22
        ZVAL_UNDEF(result);
1929
22
      }
1930
27
      return FAILURE;
1931
27
    }
1932
29.7k
  } else {
1933
1.11k
    op2_lval = Z_LVAL_P(op2);
1934
1.11k
  }
1935
1936
30.8k
  if (op1 == result) {
1937
38
    zval_ptr_dtor(result);
1938
38
  }
1939
30.8k
  ZVAL_LONG(result, op1_lval ^ op2_lval);
1940
30.8k
  return SUCCESS;
1941
30.8k
}
1942
/* }}} */
1943
1944
ZEND_API zend_result ZEND_FASTCALL shift_left_function(zval *result, zval *op1, zval *op2) /* {{{ */
1945
3.51k
{
1946
3.51k
  zend_long op1_lval, op2_lval;
1947
1948
3.51k
  convert_op1_op2_long(op1, op1_lval, op2, op2_lval, result, ZEND_SL, "<<");
1949
1950
  /* prevent wrapping quirkiness on some processors where << 64 + x == << x */
1951
3.18k
  if (UNEXPECTED((zend_ulong)op2_lval >= SIZEOF_ZEND_LONG * 8)) {
1952
525
    if (EXPECTED(op2_lval > 0)) {
1953
488
      if (op1 == result) {
1954
7
        zval_ptr_dtor(result);
1955
7
      }
1956
488
      ZVAL_LONG(result, 0);
1957
488
      return SUCCESS;
1958
488
    } else {
1959
37
      if (EG(current_execute_data) && !CG(in_compilation)) {
1960
37
        zend_throw_exception_ex(zend_ce_arithmetic_error, 0, "Bit shift by negative number");
1961
37
      } else {
1962
0
        zend_error_noreturn(E_ERROR, "Bit shift by negative number");
1963
0
      }
1964
37
      if (op1 != result) {
1965
22
        ZVAL_UNDEF(result);
1966
22
      }
1967
37
      return FAILURE;
1968
37
    }
1969
525
  }
1970
1971
2.65k
  if (op1 == result) {
1972
52
    zval_ptr_dtor(result);
1973
52
  }
1974
1975
  /* Perform shift on unsigned numbers to get well-defined wrap behavior. */
1976
2.65k
  ZVAL_LONG(result, (zend_long) ((zend_ulong) op1_lval << op2_lval));
1977
2.65k
  return SUCCESS;
1978
3.18k
}
1979
/* }}} */
1980
1981
ZEND_API zend_result ZEND_FASTCALL shift_right_function(zval *result, zval *op1, zval *op2) /* {{{ */
1982
4.64k
{
1983
4.64k
  zend_long op1_lval, op2_lval;
1984
1985
4.64k
  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
4.59k
  if (UNEXPECTED((zend_ulong)op2_lval >= SIZEOF_ZEND_LONG * 8)) {
1989
1.07k
    if (EXPECTED(op2_lval > 0)) {
1990
957
      if (op1 == result) {
1991
5
        zval_ptr_dtor(result);
1992
5
      }
1993
957
      ZVAL_LONG(result, (op1_lval < 0) ? -1 : 0);
1994
957
      return SUCCESS;
1995
957
    } else {
1996
118
      if (EG(current_execute_data) && !CG(in_compilation)) {
1997
118
        zend_throw_exception_ex(zend_ce_arithmetic_error, 0, "Bit shift by negative number");
1998
118
      } else {
1999
0
        zend_error_noreturn(E_ERROR, "Bit shift by negative number");
2000
0
      }
2001
118
      if (op1 != result) {
2002
105
        ZVAL_UNDEF(result);
2003
105
      }
2004
118
      return FAILURE;
2005
118
    }
2006
1.07k
  }
2007
2008
3.52k
  if (op1 == result) {
2009
2.31k
    zval_ptr_dtor(result);
2010
2.31k
  }
2011
2012
3.52k
  ZVAL_LONG(result, op1_lval >> op2_lval);
2013
3.52k
  return SUCCESS;
2014
4.59k
}
2015
/* }}} */
2016
2017
ZEND_API zend_result ZEND_FASTCALL concat_function(zval *result, zval *op1, zval *op2) /* {{{ */
2018
1.22M
{
2019
1.22M
  zval *orig_op1 = op1;
2020
1.22M
  zend_string *op1_string, *op2_string;
2021
1.22M
  bool free_op1_string = false;
2022
1.22M
  bool free_op2_string = false;
2023
2024
1.22M
  do {
2025
1.22M
    if (EXPECTED(Z_TYPE_P(op1) == IS_STRING)) {
2026
1.09M
      op1_string = Z_STR_P(op1);
2027
1.09M
    } else {
2028
131k
      if (Z_ISREF_P(op1)) {
2029
1.48k
        op1 = Z_REFVAL_P(op1);
2030
1.48k
        if (Z_TYPE_P(op1) == IS_STRING) {
2031
391
          op1_string = Z_STR_P(op1);
2032
391
          break;
2033
391
        }
2034
1.48k
      }
2035
131k
      ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_CONCAT);
2036
130k
      op1_string = zval_try_get_string_func(op1);
2037
130k
      if (UNEXPECTED(!op1_string)) {
2038
43
        if (orig_op1 != result) {
2039
25
          ZVAL_UNDEF(result);
2040
25
        }
2041
43
        return FAILURE;
2042
43
      }
2043
130k
      free_op1_string = true;
2044
130k
      if (result == op1) {
2045
107k
        if (UNEXPECTED(op1 == op2)) {
2046
78
          op2_string = op1_string;
2047
78
          goto has_op2_string;
2048
78
        }
2049
107k
      }
2050
130k
    }
2051
1.22M
  } while (0);
2052
1.22M
  do {
2053
1.22M
    if (EXPECTED(Z_TYPE_P(op2) == IS_STRING)) {
2054
1.17M
      op2_string = Z_STR_P(op2);
2055
1.17M
    } else {
2056
55.7k
      if (Z_ISREF_P(op2)) {
2057
59
        op2 = Z_REFVAL_P(op2);
2058
59
        if (Z_TYPE_P(op2) == IS_STRING) {
2059
35
          op2_string = Z_STR_P(op2);
2060
35
          break;
2061
35
        }
2062
59
      }
2063
      /* hold an additional reference because a userland function could free this */
2064
55.7k
      if (!free_op1_string) {
2065
34.1k
        op1_string = zend_string_copy(op1_string);
2066
34.1k
        free_op1_string = true;
2067
34.1k
      }
2068
55.7k
      ZEND_TRY_BINARY_OP2_OBJECT_OPERATION(ZEND_CONCAT);
2069
55.7k
      op2_string = zval_try_get_string_func(op2);
2070
55.7k
      if (UNEXPECTED(!op2_string)) {
2071
50
        zend_string_release_ex(op1_string, false);
2072
50
        if (orig_op1 != result) {
2073
29
          ZVAL_UNDEF(result);
2074
29
        }
2075
50
        return FAILURE;
2076
50
      }
2077
55.6k
      free_op2_string = true;
2078
55.6k
    }
2079
1.22M
  } while (0);
2080
2081
1.22M
has_op2_string:;
2082
1.22M
  if (UNEXPECTED(ZSTR_LEN(op1_string) == 0)) {
2083
130k
    if (EXPECTED(result != op2 || Z_TYPE_P(result) != IS_STRING)) {
2084
130k
      if (result == orig_op1) {
2085
119k
        i_zval_ptr_dtor(result);
2086
119k
      }
2087
130k
      if (free_op2_string) {
2088
        /* transfer ownership of op2_string */
2089
22.5k
        ZVAL_STR(result, op2_string);
2090
22.5k
        free_op2_string = false;
2091
107k
      } else {
2092
107k
        ZVAL_STR_COPY(result, op2_string);
2093
107k
      }
2094
130k
    }
2095
1.09M
  } else if (UNEXPECTED(ZSTR_LEN(op2_string) == 0)) {
2096
18.8k
    if (EXPECTED(result != op1 || Z_TYPE_P(result) != IS_STRING)) {
2097
5.16k
      if (result == orig_op1) {
2098
797
        i_zval_ptr_dtor(result);
2099
797
      }
2100
5.16k
      if (free_op1_string) {
2101
        /* transfer ownership of op1_string */
2102
4.94k
        ZVAL_STR(result, op1_string);
2103
4.94k
        free_op1_string = false;
2104
4.94k
      } else {
2105
218
        ZVAL_STR_COPY(result, op1_string);
2106
218
      }
2107
5.16k
    }
2108
1.07M
  } else {
2109
1.07M
    size_t op1_len = ZSTR_LEN(op1_string);
2110
1.07M
    size_t op2_len = ZSTR_LEN(op2_string);
2111
1.07M
    size_t result_len = op1_len + op2_len;
2112
1.07M
    zend_string *result_str;
2113
1.07M
    uint32_t flags = ZSTR_GET_COPYABLE_CONCAT_PROPERTIES_BOTH(op1_string, op2_string);
2114
2115
1.07M
    if (UNEXPECTED(op1_len > ZSTR_MAX_LEN - op2_len)) {
2116
0
      if (free_op1_string) zend_string_release_ex(op1_string, false);
2117
0
      if (free_op2_string) zend_string_release_ex(op2_string, false);
2118
0
      zend_throw_error(NULL, "String size overflow");
2119
0
      if (orig_op1 != result) {
2120
0
        ZVAL_UNDEF(result);
2121
0
      }
2122
0
      return FAILURE;
2123
0
    }
2124
2125
1.07M
    if (result == op1) {
2126
      /* Destroy the old result first to drop the refcount, such that $x .= ...; may happen in-place. */
2127
1.05M
      if (free_op1_string) {
2128
        /* op1_string will be used as the result, so we should not free it */
2129
16.7k
        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
16.7k
        ZVAL_NULL(result);
2133
16.7k
        free_op1_string = false;
2134
16.7k
      }
2135
      /* special case, perform operations on result */
2136
1.05M
      result_str = zend_string_extend(op1_string, result_len, 0);
2137
      /* account for the case where result_str == op1_string == op2_string and the realloc is done */
2138
1.05M
      if (op1_string == op2_string) {
2139
44.9k
        if (free_op2_string) {
2140
1.55k
          zend_string_release_ex(op2_string, false);
2141
1.55k
          free_op2_string = false;
2142
1.55k
        }
2143
44.9k
        op2_string = result_str;
2144
44.9k
      }
2145
1.05M
    } else {
2146
20.0k
      result_str = zend_string_alloc(result_len, 0);
2147
20.0k
      memcpy(ZSTR_VAL(result_str), ZSTR_VAL(op1_string), op1_len);
2148
20.0k
      if (result == orig_op1) {
2149
0
        i_zval_ptr_dtor(result);
2150
0
      }
2151
20.0k
    }
2152
1.07M
    GC_ADD_FLAGS(result_str, flags);
2153
2154
1.07M
    ZVAL_NEW_STR(result, result_str);
2155
1.07M
    memcpy(ZSTR_VAL(result_str) + op1_len, ZSTR_VAL(op2_string), op2_len);
2156
1.07M
    ZSTR_VAL(result_str)[result_len] = '\0';
2157
1.07M
  }
2158
2159
1.22M
  if (free_op1_string) zend_string_release_ex(op1_string, false);
2160
1.22M
  if (free_op2_string) zend_string_release_ex(op2_string, false);
2161
2162
1.22M
  return SUCCESS;
2163
1.22M
}
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
75.1k
{
2187
75.1k
  if (EXPECTED(Z_TYPE_P(op1) == IS_STRING) &&
2188
29.7k
      EXPECTED(Z_TYPE_P(op2) == IS_STRING)) {
2189
28.0k
    if (Z_STR_P(op1) == Z_STR_P(op2)) {
2190
3.70k
      return 0;
2191
24.3k
    } else {
2192
24.3k
      return zend_binary_strcmp(Z_STRVAL_P(op1), Z_STRLEN_P(op1), Z_STRVAL_P(op2), Z_STRLEN_P(op2));
2193
24.3k
    }
2194
47.0k
  } else {
2195
47.0k
    zend_string *tmp_str1, *tmp_str2;
2196
47.0k
    zend_string *str1 = zval_get_tmp_string(op1, &tmp_str1);
2197
47.0k
    zend_string *str2 = zval_get_tmp_string(op2, &tmp_str2);
2198
47.0k
    int ret = zend_binary_strcmp(ZSTR_VAL(str1), ZSTR_LEN(str1), ZSTR_VAL(str2), ZSTR_LEN(str2));
2199
2200
47.0k
    zend_tmp_string_release(tmp_str1);
2201
47.0k
    zend_tmp_string_release(tmp_str2);
2202
47.0k
    return ret;
2203
47.0k
  }
2204
75.1k
}
2205
/* }}} */
2206
2207
ZEND_API int ZEND_FASTCALL string_case_compare_function(zval *op1, zval *op2) /* {{{ */
2208
27
{
2209
27
  if (EXPECTED(Z_TYPE_P(op1) == IS_STRING) &&
2210
26
      EXPECTED(Z_TYPE_P(op2) == IS_STRING)) {
2211
18
    if (Z_STR_P(op1) == Z_STR_P(op2)) {
2212
0
      return 0;
2213
18
    } else {
2214
18
      return zend_binary_strcasecmp(Z_STRVAL_P(op1), Z_STRLEN_P(op1), Z_STRVAL_P(op2), Z_STRLEN_P(op2));
2215
18
    }
2216
18
  } else {
2217
9
    zend_string *tmp_str1, *tmp_str2;
2218
9
    zend_string *str1 = zval_get_tmp_string(op1, &tmp_str1);
2219
9
    zend_string *str2 = zval_get_tmp_string(op2, &tmp_str2);
2220
9
    int ret = zend_binary_strcasecmp(ZSTR_VAL(str1), ZSTR_LEN(str1), ZSTR_VAL(str2), ZSTR_LEN(str2));
2221
2222
9
    zend_tmp_string_release(tmp_str1);
2223
9
    zend_tmp_string_release(tmp_str2);
2224
9
    return ret;
2225
9
  }
2226
27
}
2227
/* }}} */
2228
2229
ZEND_API int ZEND_FASTCALL string_locale_compare_function(zval *op1, zval *op2) /* {{{ */
2230
1.03k
{
2231
1.03k
  zend_string *tmp_str1, *tmp_str2;
2232
1.03k
  zend_string *str1 = zval_get_tmp_string(op1, &tmp_str1);
2233
1.03k
  zend_string *str2 = zval_get_tmp_string(op2, &tmp_str2);
2234
1.03k
  int ret = strcoll(ZSTR_VAL(str1), ZSTR_VAL(str2));
2235
2236
1.03k
  zend_tmp_string_release(tmp_str1);
2237
1.03k
  zend_tmp_string_release(tmp_str2);
2238
1.03k
  return ret;
2239
1.03k
}
2240
/* }}} */
2241
2242
ZEND_API int ZEND_FASTCALL numeric_compare_function(zval *op1, zval *op2) /* {{{ */
2243
4.64k
{
2244
4.64k
  double d1, d2;
2245
2246
4.64k
  d1 = zval_get_double(op1);
2247
4.64k
  d2 = zval_get_double(op2);
2248
2249
4.64k
  return ZEND_THREEWAY_COMPARE(d1, d2);
2250
4.64k
}
2251
/* }}} */
2252
2253
ZEND_API zend_result ZEND_FASTCALL compare_function(zval *result, zval *op1, zval *op2) /* {{{ */
2254
213
{
2255
213
  ZVAL_LONG(result, zend_compare(op1, op2));
2256
213
  return SUCCESS;
2257
213
}
2258
/* }}} */
2259
2260
static int compare_long_to_string(zend_long lval, zend_string *str) /* {{{ */
2261
8.22k
{
2262
8.22k
  zend_long str_lval;
2263
8.22k
  double str_dval;
2264
8.22k
  uint8_t type = is_numeric_string(ZSTR_VAL(str), ZSTR_LEN(str), &str_lval, &str_dval, 0);
2265
2266
8.22k
  if (type == IS_LONG) {
2267
2.74k
    return lval > str_lval ? 1 : lval < str_lval ? -1 : 0;
2268
2.74k
  }
2269
2270
5.48k
  if (type == IS_DOUBLE) {
2271
281
    return ZEND_THREEWAY_COMPARE((double) lval, str_dval);
2272
281
  }
2273
2274
5.19k
  zend_string *lval_as_str = zend_long_to_str(lval);
2275
5.19k
  int cmp_result = zend_binary_strcmp(
2276
5.19k
    ZSTR_VAL(lval_as_str), ZSTR_LEN(lval_as_str), ZSTR_VAL(str), ZSTR_LEN(str));
2277
5.19k
  zend_string_release(lval_as_str);
2278
5.19k
  return ZEND_NORMALIZE_BOOL(cmp_result);
2279
5.48k
}
2280
/* }}} */
2281
2282
static int compare_double_to_string(double dval, zend_string *str) /* {{{ */
2283
4.96k
{
2284
4.96k
  zend_long str_lval;
2285
4.96k
  double str_dval;
2286
4.96k
  uint8_t type = is_numeric_string(ZSTR_VAL(str), ZSTR_LEN(str), &str_lval, &str_dval, 0);
2287
2288
4.96k
  ZEND_ASSERT(!zend_isnan(dval));
2289
2290
4.96k
  if (type == IS_LONG) {
2291
535
    return ZEND_THREEWAY_COMPARE(dval, (double) str_lval);
2292
535
  }
2293
2294
4.42k
  if (type == IS_DOUBLE) {
2295
2.87k
    return ZEND_THREEWAY_COMPARE(dval, str_dval);
2296
2.87k
  }
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
4.42k
}
2304
/* }}} */
2305
2306
ZEND_API int ZEND_FASTCALL zend_compare(zval *op1, zval *op2) /* {{{ */
2307
334k
{
2308
334k
  bool converted = false;
2309
334k
  zval op1_copy, op2_copy;
2310
2311
342k
  while (1) {
2312
342k
    switch (TYPE_PAIR(Z_TYPE_P(op1), Z_TYPE_P(op2))) {
2313
49.0k
      case TYPE_PAIR(IS_LONG, IS_LONG):
2314
49.0k
        return Z_LVAL_P(op1)>Z_LVAL_P(op2)?1:(Z_LVAL_P(op1)<Z_LVAL_P(op2)?-1:0);
2315
2316
2.44k
      case TYPE_PAIR(IS_DOUBLE, IS_LONG):
2317
2.44k
        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.13k
      case TYPE_PAIR(IS_DOUBLE, IS_DOUBLE):
2323
1.13k
        return ZEND_THREEWAY_COMPARE(Z_DVAL_P(op1), Z_DVAL_P(op2));
2324
2325
4.40k
      case TYPE_PAIR(IS_ARRAY, IS_ARRAY):
2326
4.40k
        return zend_compare_arrays(op1, op2);
2327
2328
2.20k
      case TYPE_PAIR(IS_NULL, IS_NULL):
2329
3.39k
      case TYPE_PAIR(IS_NULL, IS_FALSE):
2330
19.4k
      case TYPE_PAIR(IS_FALSE, IS_NULL):
2331
20.0k
      case TYPE_PAIR(IS_FALSE, IS_FALSE):
2332
20.8k
      case TYPE_PAIR(IS_TRUE, IS_TRUE):
2333
20.8k
        return 0;
2334
2335
1.06k
      case TYPE_PAIR(IS_NULL, IS_TRUE):
2336
1.06k
        return -1;
2337
2338
250
      case TYPE_PAIR(IS_TRUE, IS_NULL):
2339
250
        return 1;
2340
2341
7.58k
      case TYPE_PAIR(IS_STRING, IS_STRING):
2342
7.58k
        if (Z_STR_P(op1) == Z_STR_P(op2)) {
2343
1.11k
          return 0;
2344
1.11k
        }
2345
6.47k
        return zendi_smart_strcmp(Z_STR_P(op1), Z_STR_P(op2));
2346
2347
7.54k
      case TYPE_PAIR(IS_NULL, IS_STRING):
2348
7.54k
        return Z_STRLEN_P(op2) == 0 ? 0 : -1;
2349
2350
13.6k
      case TYPE_PAIR(IS_STRING, IS_NULL):
2351
13.6k
        return Z_STRLEN_P(op1) == 0 ? 0 : 1;
2352
2353
4.70k
      case TYPE_PAIR(IS_LONG, IS_STRING):
2354
4.70k
        return compare_long_to_string(Z_LVAL_P(op1), Z_STR_P(op2));
2355
2356
3.52k
      case TYPE_PAIR(IS_STRING, IS_LONG):
2357
3.52k
        return -compare_long_to_string(Z_LVAL_P(op2), Z_STR_P(op1));
2358
2359
3.59k
      case TYPE_PAIR(IS_DOUBLE, IS_STRING):
2360
3.59k
        if (zend_isnan(Z_DVAL_P(op1))) {
2361
108
          return 1;
2362
108
        }
2363
2364
3.48k
        return compare_double_to_string(Z_DVAL_P(op1), Z_STR_P(op2));
2365
2366
1.55k
      case TYPE_PAIR(IS_STRING, IS_DOUBLE):
2367
1.55k
        if (zend_isnan(Z_DVAL_P(op2))) {
2368
83
          return 1;
2369
83
        }
2370
2371
1.47k
        return -compare_double_to_string(Z_DVAL_P(op2), Z_STR_P(op1));
2372
2373
118
      case TYPE_PAIR(IS_OBJECT, IS_NULL):
2374
118
        return 1;
2375
2376
449
      case TYPE_PAIR(IS_NULL, IS_OBJECT):
2377
449
        return -1;
2378
2379
218k
      default:
2380
218k
        if (Z_ISREF_P(op1)) {
2381
2.33k
          op1 = Z_REFVAL_P(op1);
2382
2.33k
          continue;
2383
216k
        } else if (Z_ISREF_P(op2)) {
2384
1.18k
          op2 = Z_REFVAL_P(op2);
2385
1.18k
          continue;
2386
1.18k
        }
2387
2388
214k
        if (Z_TYPE_P(op1) == IS_OBJECT
2389
213k
         || Z_TYPE_P(op2) == IS_OBJECT) {
2390
1.93k
          zval *object, *other;
2391
1.93k
          if (Z_TYPE_P(op1) == IS_OBJECT) {
2392
1.72k
            object = op1;
2393
1.72k
            other = op2;
2394
1.72k
          } else {
2395
211
            object = op2;
2396
211
            other = op1;
2397
211
          }
2398
1.93k
          if (EXPECTED(Z_TYPE_P(other) == IS_OBJECT)) {
2399
1.03k
            if (Z_OBJ_P(object) == Z_OBJ_P(other)) {
2400
138
              return 0;
2401
138
            }
2402
1.03k
          } else if (Z_TYPE_P(other) == IS_TRUE || Z_TYPE_P(other) == IS_FALSE) {
2403
399
            zval casted;
2404
399
            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
399
            int ret = object == op1 ? zend_compare(&casted, other) : zend_compare(other, &casted);
2408
399
            ZEND_ASSERT(!Z_REFCOUNTED_P(&casted));
2409
399
            return ret;
2410
399
          }
2411
1.39k
          return Z_OBJ_HANDLER_P(object, compare)(op1, op2);
2412
1.93k
        }
2413
2414
212k
        if (!converted) {
2415
          /* Handle NAN */
2416
209k
          if (UNEXPECTED(
2417
209k
            (Z_TYPE_P(op1) == IS_DOUBLE && zend_isnan(Z_DVAL_P(op1)))
2418
209k
            || (Z_TYPE_P(op2) == IS_DOUBLE && zend_isnan(Z_DVAL_P(op2)))
2419
209k
          )) {
2420
            // TODO: NAN should always be uncomparable
2421
            /* NAN used be cast to TRUE so handle this manually for the time being */
2422
151
            if (Z_TYPE_P(op1) < IS_TRUE) {
2423
20
              return -1;
2424
131
            } else if (Z_TYPE_P(op1) == IS_TRUE || Z_TYPE_P(op2) == IS_TRUE) {
2425
41
              return 0;
2426
90
            } else if (Z_TYPE_P(op2) < IS_TRUE) {
2427
55
              return 1;
2428
55
            } else if (Z_TYPE_P(op1) != IS_DOUBLE) {
2429
10
              op1 = _zendi_convert_scalar_to_number_silent(op1, &op1_copy);
2430
10
              converted = true;
2431
25
            } else if (Z_TYPE_P(op2) != IS_DOUBLE) {
2432
25
              op2 = _zendi_convert_scalar_to_number_silent(op2, &op2_copy);
2433
25
              converted = true;
2434
25
            }
2435
208k
          } else if (Z_TYPE_P(op1) < IS_TRUE) {
2436
192k
            return zend_is_true(op2) ? -1 : 0;
2437
192k
          } else if (Z_TYPE_P(op1) == IS_TRUE) {
2438
779
            return zend_is_true(op2) ? 0 : 1;
2439
15.6k
          } else if (Z_TYPE_P(op2) < IS_TRUE) {
2440
9.55k
            return zend_is_true(op1) ? 1 : 0;
2441
9.55k
          } else if (Z_TYPE_P(op2) == IS_TRUE) {
2442
2.30k
            return zend_is_true(op1) ? 0 : -1;
2443
3.77k
          } else {
2444
3.77k
            op1 = _zendi_convert_scalar_to_number_silent(op1, &op1_copy);
2445
3.77k
            op2 = _zendi_convert_scalar_to_number_silent(op2, &op2_copy);
2446
3.77k
            if (EG(exception)) {
2447
0
              return 1; /* to stop comparison of arrays */
2448
0
            }
2449
3.77k
            converted = true;
2450
3.77k
          }
2451
209k
        } else if (Z_TYPE_P(op1)==IS_ARRAY) {
2452
2.60k
          return 1;
2453
2.60k
        } else if (Z_TYPE_P(op2)==IS_ARRAY) {
2454
1.20k
          return -1;
2455
1.20k
        } else {
2456
0
          ZEND_UNREACHABLE();
2457
0
          zend_throw_error(NULL, "Unsupported operand types");
2458
0
          return 1;
2459
0
        }
2460
342k
    }
2461
342k
  }
2462
334k
}
2463
/* }}} */
2464
2465
/* return int to be compatible with compare_func_t */
2466
static int hash_zval_identical_function(zval *z1, zval *z2) /* {{{ */
2467
893
{
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
893
  ZVAL_DEREF(z1);
2474
893
  ZVAL_DEREF(z2);
2475
893
  return fast_is_not_identical_function(z1, z2);
2476
893
}
2477
/* }}} */
2478
2479
ZEND_API bool ZEND_FASTCALL zend_is_identical(const zval *op1, const zval *op2) /* {{{ */
2480
44.0k
{
2481
44.0k
  if (Z_TYPE_P(op1) != Z_TYPE_P(op2)) {
2482
1.03k
    return 0;
2483
1.03k
  }
2484
43.0k
  switch (Z_TYPE_P(op1)) {
2485
144
    case IS_NULL:
2486
283
    case IS_FALSE:
2487
422
    case IS_TRUE:
2488
422
      return 1;
2489
8.64k
    case IS_LONG:
2490
8.64k
      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
625
    case IS_DOUBLE:
2494
625
      return (Z_DVAL_P(op1) == Z_DVAL_P(op2));
2495
30.9k
    case IS_STRING:
2496
30.9k
      return zend_string_equals(Z_STR_P(op1), Z_STR_P(op2));
2497
1.93k
    case IS_ARRAY:
2498
1.93k
      return (Z_ARRVAL_P(op1) == Z_ARRVAL_P(op2) ||
2499
1.70k
        zend_hash_compare(Z_ARRVAL_P(op1), Z_ARRVAL_P(op2), (compare_func_t) hash_zval_identical_function, 1) == 0);
2500
477
    case IS_OBJECT:
2501
477
      return (Z_OBJ_P(op1) == Z_OBJ_P(op2));
2502
0
    default:
2503
0
      return 0;
2504
43.0k
  }
2505
43.0k
}
2506
/* }}} */
2507
2508
ZEND_API zend_result ZEND_FASTCALL is_identical_function(zval *result, zval *op1, zval *op2) /* {{{ */
2509
3.15k
{
2510
3.15k
  ZVAL_BOOL(result, zend_is_identical(op1, op2));
2511
3.15k
  return SUCCESS;
2512
3.15k
}
2513
/* }}} */
2514
2515
ZEND_API zend_result ZEND_FASTCALL is_not_identical_function(zval *result, zval *op1, zval *op2) /* {{{ */
2516
440
{
2517
440
  ZVAL_BOOL(result, !zend_is_identical(op1, op2));
2518
440
  return SUCCESS;
2519
440
}
2520
/* }}} */
2521
2522
ZEND_API zend_result ZEND_FASTCALL is_equal_function(zval *result, zval *op1, zval *op2) /* {{{ */
2523
2.56k
{
2524
2.56k
  ZVAL_BOOL(result, zend_compare(op1, op2) == 0);
2525
2.56k
  return SUCCESS;
2526
2.56k
}
2527
/* }}} */
2528
2529
ZEND_API zend_result ZEND_FASTCALL is_not_equal_function(zval *result, zval *op1, zval *op2) /* {{{ */
2530
1.97k
{
2531
1.97k
  ZVAL_BOOL(result, (zend_compare(op1, op2) != 0));
2532
1.97k
  return SUCCESS;
2533
1.97k
}
2534
/* }}} */
2535
2536
ZEND_API zend_result ZEND_FASTCALL is_smaller_function(zval *result, zval *op1, zval *op2) /* {{{ */
2537
23.3k
{
2538
23.3k
  ZVAL_BOOL(result, (zend_compare(op1, op2) < 0));
2539
23.3k
  return SUCCESS;
2540
23.3k
}
2541
/* }}} */
2542
2543
ZEND_API zend_result ZEND_FASTCALL is_smaller_or_equal_function(zval *result, zval *op1, zval *op2) /* {{{ */
2544
1.94k
{
2545
1.94k
  ZVAL_BOOL(result, (zend_compare(op1, op2) <= 0));
2546
1.94k
  return SUCCESS;
2547
1.94k
}
2548
/* }}} */
2549
2550
ZEND_API bool ZEND_FASTCALL zend_class_implements_interface(const zend_class_entry *class_ce, const zend_class_entry *interface_ce) /* {{{ */
2551
4.03k
{
2552
4.03k
  uint32_t i;
2553
4.03k
  ZEND_ASSERT(interface_ce->ce_flags & ZEND_ACC_INTERFACE);
2554
2555
4.03k
  if (class_ce->num_interfaces) {
2556
3.98k
    ZEND_ASSERT(class_ce->ce_flags & ZEND_ACC_RESOLVED_INTERFACES);
2557
11.1k
    for (i = 0; i < class_ce->num_interfaces; i++) {
2558
8.57k
      if (class_ce->interfaces[i] == interface_ce) {
2559
1.36k
        return 1;
2560
1.36k
      }
2561
8.57k
    }
2562
3.98k
  }
2563
2.67k
  return 0;
2564
4.03k
}
2565
/* }}} */
2566
2567
ZEND_API bool ZEND_FASTCALL instanceof_function_slow(const zend_class_entry *instance_ce, const zend_class_entry *ce) /* {{{ */
2568
15.6M
{
2569
15.6M
  ZEND_ASSERT(instance_ce != ce && "Should have been checked already");
2570
15.6M
  if (ce->ce_flags & ZEND_ACC_INTERFACE) {
2571
1.03M
    uint32_t i;
2572
2573
1.03M
    if (instance_ce->num_interfaces) {
2574
1.03M
      ZEND_ASSERT(instance_ce->ce_flags & ZEND_ACC_RESOLVED_INTERFACES);
2575
2.05M
      for (i = 0; i < instance_ce->num_interfaces; i++) {
2576
2.05M
        if (instance_ce->interfaces[i] == ce) {
2577
1.03M
          return 1;
2578
1.03M
        }
2579
2.05M
      }
2580
1.03M
    }
2581
1.86k
    return 0;
2582
14.5M
  } else {
2583
40.1M
    while (1) {
2584
40.1M
      instance_ce = instance_ce->parent;
2585
40.1M
      if (instance_ce == ce) {
2586
1.50M
        return 1;
2587
1.50M
      }
2588
38.6M
      if (instance_ce == NULL) {
2589
13.0M
        return 0;
2590
13.0M
      }
2591
38.6M
    }
2592
14.5M
  }
2593
15.6M
}
2594
/* }}} */
2595
2596
374
#define LOWER_CASE 1
2597
139
#define UPPER_CASE 2
2598
1.01k
#define NUMERIC 3
2599
2600
ZEND_API bool zend_string_only_has_ascii_alphanumeric(const zend_string *str)
2601
0
{
2602
0
  const char *p = ZSTR_VAL(str);
2603
0
  const char *e = ZSTR_VAL(str) + ZSTR_LEN(str);
2604
0
  while (p < e) {
2605
0
    char c = *p++;
2606
0
    if (UNEXPECTED( c < '0' || c > 'z' || (c < 'a' && c > 'Z') || (c < 'A' && c > '9') ) ) {
2607
0
      return false;
2608
0
    }
2609
0
  }
2610
0
  return true;
2611
0
}
2612
2613
static bool ZEND_FASTCALL increment_string(zval *str) /* {{{ */
2614
1.41k
{
2615
1.41k
  int carry=0;
2616
1.41k
  size_t pos=Z_STRLEN_P(str)-1;
2617
1.41k
  char *s;
2618
1.41k
  zend_string *t;
2619
1.41k
  int last=0; /* Shut up the compiler warning */
2620
1.41k
  int ch;
2621
2622
1.41k
  zend_string *zstr = Z_STR_P(str);
2623
1.41k
  zend_string_addref(zstr);
2624
1.41k
  zend_error(E_DEPRECATED, "Increment on non-numeric string is deprecated, use str_increment() instead");
2625
1.41k
  if (EG(exception)) {
2626
0
    zend_string_release(zstr);
2627
0
    return false;
2628
0
  }
2629
  /* A userland error handler can change the type from string to something else */
2630
1.41k
  zval_ptr_dtor(str);
2631
1.41k
  ZVAL_STR(str, zstr);
2632
2633
1.41k
  if (UNEXPECTED(Z_STRLEN_P(str) == 0)) {
2634
19
    zval_ptr_dtor(str);
2635
19
    ZVAL_CHAR(str, '1');
2636
19
    return true;
2637
19
  }
2638
2639
1.39k
  if (!Z_REFCOUNTED_P(str)) {
2640
418
    Z_STR_P(str) = zend_string_init(Z_STRVAL_P(str), Z_STRLEN_P(str), 0);
2641
418
    Z_TYPE_INFO_P(str) = IS_STRING_EX;
2642
979
  } else if (Z_REFCOUNT_P(str) > 1) {
2643
    /* Only release string after allocation succeeded. */
2644
336
    zend_string *orig_str = Z_STR_P(str);
2645
336
    Z_STR_P(str) = zend_string_init(Z_STRVAL_P(str), Z_STRLEN_P(str), 0);
2646
336
    GC_DELREF(orig_str);
2647
643
  } else {
2648
643
    zend_string_forget_hash_val(Z_STR_P(str));
2649
643
  }
2650
1.39k
  s = Z_STRVAL_P(str);
2651
2652
1.59k
  do {
2653
1.59k
    ch = s[pos];
2654
1.59k
    if (ch >= 'a' && ch <= 'z') {
2655
359
      if (ch == 'z') {
2656
57
        s[pos] = 'a';
2657
57
        carry=1;
2658
302
      } else {
2659
302
        s[pos]++;
2660
302
        carry=0;
2661
302
      }
2662
359
      last=LOWER_CASE;
2663
1.23k
    } else if (ch >= 'A' && ch <= 'Z') {
2664
124
      if (ch == 'Z') {
2665
72
        s[pos] = 'A';
2666
72
        carry=1;
2667
72
      } else {
2668
52
        s[pos]++;
2669
52
        carry=0;
2670
52
      }
2671
124
      last=UPPER_CASE;
2672
1.11k
    } else if (ch >= '0' && ch <= '9') {
2673
997
      if (ch == '9') {
2674
120
        s[pos] = '0';
2675
120
        carry=1;
2676
877
      } else {
2677
877
        s[pos]++;
2678
877
        carry=0;
2679
877
      }
2680
997
      last = NUMERIC;
2681
997
    } else {
2682
114
      carry=0;
2683
114
      break;
2684
114
    }
2685
1.48k
    if (carry == 0) {
2686
1.23k
      break;
2687
1.23k
    }
2688
1.48k
  } while (pos-- > 0);
2689
2690
1.39k
  if (carry) {
2691
52
    t = zend_string_alloc(Z_STRLEN_P(str)+1, 0);
2692
52
    memcpy(ZSTR_VAL(t) + 1, Z_STRVAL_P(str), Z_STRLEN_P(str));
2693
52
    ZSTR_VAL(t)[Z_STRLEN_P(str) + 1] = '\0';
2694
52
    switch (last) {
2695
22
      case NUMERIC:
2696
22
        ZSTR_VAL(t)[0] = '1';
2697
22
        break;
2698
15
      case UPPER_CASE:
2699
15
        ZSTR_VAL(t)[0] = 'A';
2700
15
        break;
2701
15
      case LOWER_CASE:
2702
15
        ZSTR_VAL(t)[0] = 'a';
2703
15
        break;
2704
52
    }
2705
52
    zend_string_free(Z_STR_P(str));
2706
52
    ZVAL_NEW_STR(str, t);
2707
52
  }
2708
1.39k
  return true;
2709
1.39k
}
2710
/* }}} */
2711
2712
ZEND_API zend_result ZEND_FASTCALL increment_function(zval *op1) /* {{{ */
2713
9.28k
{
2714
9.28k
try_again:
2715
9.28k
  switch (Z_TYPE_P(op1)) {
2716
3.50k
    case IS_LONG:
2717
3.50k
      fast_long_increment_function(op1);
2718
3.50k
      break;
2719
1.78k
    case IS_DOUBLE:
2720
1.78k
      Z_DVAL_P(op1) = Z_DVAL_P(op1) + 1;
2721
1.78k
      break;
2722
1.87k
    case IS_NULL:
2723
1.87k
      ZVAL_LONG(op1, 1);
2724
1.87k
      break;
2725
1.83k
    case IS_STRING: {
2726
1.83k
        zend_long lval;
2727
1.83k
        double dval;
2728
2729
1.83k
        switch (is_numeric_str_function(Z_STR_P(op1), &lval, &dval)) {
2730
359
          case IS_LONG:
2731
359
            zval_ptr_dtor_str(op1);
2732
359
            if (lval == ZEND_LONG_MAX) {
2733
              /* switch to double */
2734
6
              double d = (double)lval;
2735
6
              ZVAL_DOUBLE(op1, d+1);
2736
353
            } else {
2737
353
              ZVAL_LONG(op1, lval+1);
2738
353
            }
2739
359
            break;
2740
63
          case IS_DOUBLE:
2741
63
            zval_ptr_dtor_str(op1);
2742
63
            ZVAL_DOUBLE(op1, dval+1);
2743
63
            break;
2744
1.41k
          default:
2745
            /* Perl style string increment */
2746
1.41k
            increment_string(op1);
2747
1.41k
            if (EG(exception)) {
2748
0
              return FAILURE;
2749
0
            }
2750
1.41k
            break;
2751
1.83k
        }
2752
1.83k
      }
2753
1.83k
      break;
2754
1.83k
    case IS_FALSE:
2755
234
    case IS_TRUE: {
2756
      /* Error handler can undef/change type of op1, save it and reset it in case those cases */
2757
234
      zval copy;
2758
234
      ZVAL_COPY_VALUE(&copy, op1);
2759
234
      zend_error(E_WARNING, "Increment on type bool has no effect, this will change in the next major version of PHP");
2760
234
      zval_ptr_dtor(op1);
2761
234
      ZVAL_COPY_VALUE(op1, &copy);
2762
234
      if (EG(exception)) {
2763
0
        return FAILURE;
2764
0
      }
2765
234
      break;
2766
234
    }
2767
234
    case IS_REFERENCE:
2768
0
      op1 = Z_REFVAL_P(op1);
2769
0
      goto try_again;
2770
51
    case IS_OBJECT: {
2771
51
      if (Z_OBJ_HANDLER_P(op1, do_operation)) {
2772
0
        zval op2;
2773
0
        ZVAL_LONG(&op2, 1);
2774
0
        if (Z_OBJ_HANDLER_P(op1, do_operation)(ZEND_ADD, op1, op1, &op2) == SUCCESS) {
2775
0
          return SUCCESS;
2776
0
        }
2777
0
      }
2778
51
      zval tmp;
2779
51
      if (Z_OBJ_HT_P(op1)->cast_object(Z_OBJ_P(op1), &tmp, _IS_NUMBER) == SUCCESS) {
2780
0
        ZEND_ASSERT(Z_TYPE(tmp) == IS_LONG || Z_TYPE(tmp) == IS_DOUBLE);
2781
0
        zval_ptr_dtor(op1);
2782
0
        ZVAL_COPY_VALUE(op1, &tmp);
2783
0
        goto try_again;
2784
0
      }
2785
51
      ZEND_FALLTHROUGH;
2786
51
    }
2787
51
    case IS_RESOURCE:
2788
51
    case IS_ARRAY:
2789
51
      zend_type_error("Cannot increment %s", zend_zval_value_name(op1));
2790
51
      return FAILURE;
2791
9.28k
    EMPTY_SWITCH_DEFAULT_CASE()
2792
9.28k
  }
2793
9.23k
  return SUCCESS;
2794
9.28k
}
2795
/* }}} */
2796
2797
ZEND_API zend_result ZEND_FASTCALL decrement_function(zval *op1) /* {{{ */
2798
13.5k
{
2799
13.5k
  zend_long lval;
2800
13.5k
  double dval;
2801
2802
13.5k
try_again:
2803
13.5k
  switch (Z_TYPE_P(op1)) {
2804
2.25k
    case IS_LONG:
2805
2.25k
      fast_long_decrement_function(op1);
2806
2.25k
      break;
2807
5.30k
    case IS_DOUBLE:
2808
5.30k
      Z_DVAL_P(op1) = Z_DVAL_P(op1) - 1;
2809
5.30k
      break;
2810
1.93k
    case IS_STRING:   /* Like perl we only support string increment */
2811
1.93k
      if (Z_STRLEN_P(op1) == 0) { /* consider as 0 */
2812
13
        zend_error(E_DEPRECATED, "Decrement on empty string is deprecated as non-numeric");
2813
13
        if (EG(exception)) {
2814
0
          return FAILURE;
2815
0
        }
2816
        /* A userland error handler can change the type from string to something else */
2817
13
        zval_ptr_dtor(op1);
2818
13
        ZVAL_LONG(op1, -1);
2819
13
        break;
2820
13
      }
2821
1.92k
      switch (is_numeric_str_function(Z_STR_P(op1), &lval, &dval)) {
2822
935
        case IS_LONG:
2823
935
          zval_ptr_dtor_str(op1);
2824
935
          if (lval == ZEND_LONG_MIN) {
2825
9
            double d = (double)lval;
2826
9
            ZVAL_DOUBLE(op1, d-1);
2827
926
          } else {
2828
926
            ZVAL_LONG(op1, lval-1);
2829
926
          }
2830
935
          break;
2831
133
        case IS_DOUBLE:
2832
133
          zval_ptr_dtor_str(op1);
2833
133
          ZVAL_DOUBLE(op1, dval - 1);
2834
133
          break;
2835
856
        default: {
2836
          /* Error handler can unset the variable */
2837
856
          zend_string *zstr = Z_STR_P(op1);
2838
856
          zend_string_addref(zstr);
2839
856
          zend_error(E_DEPRECATED, "Decrement on non-numeric string has no effect and is deprecated");
2840
856
          if (EG(exception)) {
2841
0
            zend_string_release(zstr);
2842
0
            return FAILURE;
2843
0
          }
2844
856
          zval_ptr_dtor(op1);
2845
856
          ZVAL_STR(op1, zstr);
2846
856
        }
2847
1.92k
      }
2848
1.92k
      break;
2849
3.24k
    case IS_NULL: {
2850
      /* Error handler can undef/change type of op1, save it and reset it in case those cases */
2851
3.24k
      zval copy;
2852
3.24k
      ZVAL_COPY_VALUE(&copy, op1);
2853
3.24k
      zend_error(E_WARNING, "Decrement on type null has no effect, this will change in the next major version of PHP");
2854
3.24k
      zval_ptr_dtor(op1);
2855
3.24k
      ZVAL_COPY_VALUE(op1, &copy);
2856
3.24k
      if (EG(exception)) {
2857
0
        return FAILURE;
2858
0
      }
2859
3.24k
      break;
2860
3.24k
    }
2861
3.24k
    case IS_FALSE:
2862
781
    case IS_TRUE: {
2863
      /* Error handler can undef/change type of op1, save it and reset it in case those cases */
2864
781
      zval copy;
2865
781
      ZVAL_COPY_VALUE(&copy, op1);
2866
781
      zend_error(E_WARNING, "Decrement on type bool has no effect, this will change in the next major version of PHP");
2867
781
      zval_ptr_dtor(op1);
2868
781
      ZVAL_COPY_VALUE(op1, &copy);
2869
781
      if (EG(exception)) {
2870
0
        return FAILURE;
2871
0
      }
2872
781
      break;
2873
781
    }
2874
781
    case IS_REFERENCE:
2875
0
      op1 = Z_REFVAL_P(op1);
2876
0
      goto try_again;
2877
50
    case IS_OBJECT: {
2878
50
      if (Z_OBJ_HANDLER_P(op1, do_operation)) {
2879
0
        zval op2;
2880
0
        ZVAL_LONG(&op2, 1);
2881
0
        if (Z_OBJ_HANDLER_P(op1, do_operation)(ZEND_SUB, op1, op1, &op2) == SUCCESS) {
2882
0
          return SUCCESS;
2883
0
        }
2884
0
      }
2885
50
      zval tmp;
2886
50
      if (Z_OBJ_HT_P(op1)->cast_object(Z_OBJ_P(op1), &tmp, _IS_NUMBER) == SUCCESS) {
2887
0
        ZEND_ASSERT(Z_TYPE(tmp) == IS_LONG || Z_TYPE(tmp) == IS_DOUBLE);
2888
0
        zval_ptr_dtor(op1);
2889
0
        ZVAL_COPY_VALUE(op1, &tmp);
2890
0
        goto try_again;
2891
0
      }
2892
50
      ZEND_FALLTHROUGH;
2893
50
    }
2894
50
    case IS_RESOURCE:
2895
54
    case IS_ARRAY:
2896
54
      zend_type_error("Cannot decrement %s", zend_zval_value_name(op1));
2897
54
      return FAILURE;
2898
13.5k
    EMPTY_SWITCH_DEFAULT_CASE()
2899
13.5k
  }
2900
2901
13.5k
  return SUCCESS;
2902
13.5k
}
2903
/* }}} */
2904
2905
ZEND_API bool ZEND_FASTCALL zend_is_true(const zval *op) /* {{{ */
2906
276k
{
2907
276k
  return i_zend_is_true(op);
2908
276k
}
2909
/* }}} */
2910
2911
ZEND_API bool ZEND_FASTCALL zend_object_is_true(const zval *op) /* {{{ */
2912
0
{
2913
0
  zend_object *zobj = Z_OBJ_P(op);
2914
0
  zval tmp;
2915
0
  if (zobj->handlers->cast_object(zobj, &tmp, _IS_BOOL) == SUCCESS) {
2916
0
    return Z_TYPE(tmp) == IS_TRUE;
2917
0
  }
2918
0
  zend_error(E_RECOVERABLE_ERROR, "Object of class %s could not be converted to bool", ZSTR_VAL(zobj->ce->name));
2919
0
  return false;
2920
0
}
2921
/* }}} */
2922
2923
ZEND_API void zend_update_current_locale(void) /* {{{ */
2924
16
{
2925
#ifdef ZEND_USE_TOLOWER_L
2926
# if defined(ZEND_WIN32) && defined(_MSC_VER)
2927
  current_locale = _get_current_locale();
2928
# else
2929
  current_locale = uselocale(0);
2930
# endif
2931
#endif
2932
#if defined(ZEND_WIN32) && defined(_MSC_VER)
2933
  if (MB_CUR_MAX > 1) {
2934
    unsigned int cp = ___lc_codepage_func();
2935
    CG(variable_width_locale) = 1;
2936
    // TODO: EUC-* are also ASCII compatible ???
2937
    CG(ascii_compatible_locale) =
2938
      cp == 65001; /* UTF-8 */
2939
  } else {
2940
    CG(variable_width_locale) = 0;
2941
    CG(ascii_compatible_locale) = 1;
2942
  }
2943
#elif defined(MB_CUR_MAX)
2944
  /* Check if current locale uses variable width encoding */
2945
16
  if (MB_CUR_MAX > 1) {
2946
16
#ifdef HAVE_NL_LANGINFO
2947
16
    const char *charmap = nl_langinfo(CODESET);
2948
#else
2949
    char buf[16];
2950
    const char *charmap = NULL;
2951
    const char *locale = setlocale(LC_CTYPE, NULL);
2952
2953
    if (locale) {
2954
      const char *dot = strchr(locale, '.');
2955
      const char *modifier;
2956
2957
      if (dot) {
2958
        dot++;
2959
        modifier = strchr(dot, '@');
2960
        if (!modifier) {
2961
          charmap = dot;
2962
        } else if (modifier - dot < sizeof(buf)) {
2963
          memcpy(buf, dot, modifier - dot);
2964
                    buf[modifier - dot] = '\0';
2965
                    charmap = buf;
2966
        }
2967
      }
2968
    }
2969
#endif
2970
16
    CG(variable_width_locale) = 1;
2971
16
    CG(ascii_compatible_locale) = 0;
2972
2973
16
    if (charmap) {
2974
16
      size_t len = strlen(charmap);
2975
16
      static const char *ascii_compatible_charmaps[] = {
2976
16
        "utf-8",
2977
16
        "utf8",
2978
        // TODO: EUC-* are also ASCII compatible ???
2979
16
        NULL
2980
16
      };
2981
16
      const char **p;
2982
      /* Check if current locale is ASCII compatible */
2983
16
      for (p = ascii_compatible_charmaps; *p; p++) {
2984
16
        if (zend_binary_strcasecmp(charmap, len, *p, strlen(*p)) == 0) {
2985
16
          CG(ascii_compatible_locale) = 1;
2986
16
          break;
2987
16
        }
2988
16
      }
2989
16
    }
2990
2991
16
  } else {
2992
0
    CG(variable_width_locale) = 0;
2993
0
    CG(ascii_compatible_locale) = 1;
2994
0
  }
2995
#else
2996
  /* We can't determine current charset. Assume the worst case */
2997
  CG(variable_width_locale) = 1;
2998
  CG(ascii_compatible_locale) = 0;
2999
#endif
3000
16
}
3001
/* }}} */
3002
3003
ZEND_API void zend_reset_lc_ctype_locale(void)
3004
16
{
3005
  /* Use the C.UTF-8 locale so that readline can process UTF-8 input, while not interfering
3006
   * with single-byte locale-dependent functions used by PHP. */
3007
16
  if (!setlocale(LC_CTYPE, "C.UTF-8")) {
3008
0
    setlocale(LC_CTYPE, "C");
3009
0
  }
3010
16
}
3011
3012
13.7M
static zend_always_inline void zend_str_tolower_impl(char *dest, const char *str, size_t length) /* {{{ */ {
3013
13.7M
  unsigned char *p = (unsigned char*)str;
3014
13.7M
  unsigned char *q = (unsigned char*)dest;
3015
13.7M
  unsigned char *end = p + length;
3016
13.7M
#ifdef HAVE_BLOCKCONV
3017
13.7M
  if (length >= BLOCKCONV_STRIDE) {
3018
780k
    BLOCKCONV_INIT_RANGE('A', 'Z');
3019
780k
    BLOCKCONV_INIT_DELTA('a' - 'A');
3020
881k
    do {
3021
881k
      BLOCKCONV_LOAD(p);
3022
881k
      BLOCKCONV_STORE(q);
3023
881k
      p += BLOCKCONV_STRIDE;
3024
881k
      q += BLOCKCONV_STRIDE;
3025
881k
    } while (p + BLOCKCONV_STRIDE <= end);
3026
780k
  }
3027
13.7M
#endif
3028
109M
  while (p < end) {
3029
96.2M
    *q++ = zend_tolower_ascii(*p++);
3030
96.2M
  }
3031
13.7M
}
3032
/* }}} */
3033
3034
13
static zend_always_inline void zend_str_toupper_impl(char *dest, const char *str, size_t length) /* {{{ */ {
3035
13
  unsigned char *p = (unsigned char*)str;
3036
13
  unsigned char *q = (unsigned char*)dest;
3037
13
  unsigned char *end = p + length;
3038
13
#ifdef HAVE_BLOCKCONV
3039
13
  if (length >= BLOCKCONV_STRIDE) {
3040
5
    BLOCKCONV_INIT_RANGE('a', 'z');
3041
5
    BLOCKCONV_INIT_DELTA('A' - 'a');
3042
10
    do {
3043
10
      BLOCKCONV_LOAD(p);
3044
10
      BLOCKCONV_STORE(q);
3045
10
      p += BLOCKCONV_STRIDE;
3046
10
      q += BLOCKCONV_STRIDE;
3047
10
    } while (p + BLOCKCONV_STRIDE <= end);
3048
5
  }
3049
13
#endif
3050
42
  while (p < end) {
3051
29
    *q++ = zend_toupper_ascii(*p++);
3052
29
  }
3053
13
}
3054
/* }}} */
3055
3056
ZEND_API char* ZEND_FASTCALL zend_str_tolower_copy(char *dest, const char *source, size_t length) /* {{{ */
3057
2.92M
{
3058
2.92M
  zend_str_tolower_impl(dest, source, length);
3059
2.92M
  dest[length] = '\0';
3060
2.92M
  return dest;
3061
2.92M
}
3062
/* }}} */
3063
3064
ZEND_API char* ZEND_FASTCALL zend_str_toupper_copy(char *dest, const char *source, size_t length) /* {{{ */
3065
0
{
3066
0
  zend_str_toupper_impl(dest, source, length);
3067
0
  dest[length] = '\0';
3068
0
  return dest;
3069
0
}
3070
/* }}} */
3071
3072
ZEND_API char* ZEND_FASTCALL zend_str_tolower_dup(const char *source, size_t length) /* {{{ */
3073
1.13k
{
3074
1.13k
  return zend_str_tolower_copy((char *)emalloc(length+1), source, length);
3075
1.13k
}
3076
/* }}} */
3077
3078
ZEND_API char* ZEND_FASTCALL zend_str_toupper_dup(const char *source, size_t length) /* {{{ */
3079
0
{
3080
0
  return zend_str_toupper_copy((char *)emalloc(length+1), source, length);
3081
0
}
3082
/* }}} */
3083
3084
ZEND_API void ZEND_FASTCALL zend_str_tolower(char *str, size_t length) /* {{{ */
3085
6.98M
{
3086
6.98M
  zend_str_tolower_impl(str, (const char*)str, length);
3087
6.98M
}
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
16
{
3099
16
  const unsigned char *p = (const unsigned char*)source;
3100
16
  const unsigned char *end = p + length;
3101
3102
154
  while (p < end) {
3103
153
    if (*p != zend_tolower_ascii(*p)) {
3104
15
      char *res = (char*)emalloc(length + 1);
3105
15
      unsigned char *r;
3106
3107
15
      if (p != (const unsigned char*)source) {
3108
15
        memcpy(res, source, p - (const unsigned char*)source);
3109
15
      }
3110
15
      r = (unsigned char*)p + (res - source);
3111
15
      zend_str_tolower_impl((char *)r, (const char*)p, end - p);
3112
15
      res[length] = '\0';
3113
15
      return res;
3114
15
    }
3115
138
    p++;
3116
138
  }
3117
1
  return NULL;
3118
16
}
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
9.51M
{
3147
9.51M
  size_t length = ZSTR_LEN(str);
3148
9.51M
  unsigned char *p = (unsigned char *) ZSTR_VAL(str);
3149
9.51M
  unsigned char *end = p + length;
3150
3151
9.51M
#ifdef HAVE_BLOCKCONV
3152
9.51M
  BLOCKCONV_INIT_RANGE('A', 'Z');
3153
20.2M
  while (p + BLOCKCONV_STRIDE <= end) {
3154
14.5M
    BLOCKCONV_LOAD(p);
3155
14.5M
    if (BLOCKCONV_FOUND()) {
3156
3.79M
      zend_string *res = zend_string_alloc(length, persistent);
3157
3.79M
      memcpy(ZSTR_VAL(res), ZSTR_VAL(str), p - (unsigned char *) ZSTR_VAL(str));
3158
3.79M
      unsigned char *q = (unsigned char*) ZSTR_VAL(res) + (p - (unsigned char*) ZSTR_VAL(str));
3159
3160
      /* Lowercase the chunk we already compared. */
3161
3.79M
      BLOCKCONV_INIT_DELTA('a' - 'A');
3162
3.79M
      BLOCKCONV_STORE(q);
3163
3164
      /* Lowercase the rest of the string. */
3165
3.79M
      p += BLOCKCONV_STRIDE;
3166
3.79M
      q += BLOCKCONV_STRIDE;
3167
3.79M
      zend_str_tolower_impl((char *) q, (const char *) p, end - p);
3168
3.79M
      ZSTR_VAL(res)[length] = '\0';
3169
3.79M
      return res;
3170
3.79M
    }
3171
10.7M
    p += BLOCKCONV_STRIDE;
3172
10.7M
  }
3173
5.72M
#endif
3174
3175
27.0M
  while (p < end) {
3176
25.3M
    if (*p != zend_tolower_ascii(*p)) {
3177
4.07M
      zend_string *res = zend_string_alloc(length, persistent);
3178
4.07M
      memcpy(ZSTR_VAL(res), ZSTR_VAL(str), p - (unsigned char*) ZSTR_VAL(str));
3179
3180
4.07M
      unsigned char *q = (unsigned char*) ZSTR_VAL(res) + (p - (unsigned char*) ZSTR_VAL(str));
3181
36.9M
      while (p < end) {
3182
32.8M
        *q++ = zend_tolower_ascii(*p++);
3183
32.8M
      }
3184
4.07M
      ZSTR_VAL(res)[length] = '\0';
3185
4.07M
      return res;
3186
4.07M
    }
3187
21.3M
    p++;
3188
21.3M
  }
3189
3190
1.64M
  return zend_string_copy(str);
3191
5.72M
}
3192
/* }}} */
3193
3194
ZEND_API zend_string* ZEND_FASTCALL zend_string_toupper_ex(zend_string *str, bool persistent) /* {{{ */
3195
1.05k
{
3196
1.05k
  size_t length = ZSTR_LEN(str);
3197
1.05k
  unsigned char *p = (unsigned char *) ZSTR_VAL(str);
3198
1.05k
  unsigned char *end = p + length;
3199
3200
1.05k
#ifdef HAVE_BLOCKCONV
3201
1.05k
  BLOCKCONV_INIT_RANGE('a', 'z');
3202
1.05k
  while (p + BLOCKCONV_STRIDE <= end) {
3203
16
    BLOCKCONV_LOAD(p);
3204
16
    if (BLOCKCONV_FOUND()) {
3205
13
      zend_string *res = zend_string_alloc(length, persistent);
3206
13
      memcpy(ZSTR_VAL(res), ZSTR_VAL(str), p - (unsigned char *) ZSTR_VAL(str));
3207
13
      unsigned char *q = (unsigned char *) ZSTR_VAL(res) + (p - (unsigned char *) ZSTR_VAL(str));
3208
3209
      /* Uppercase the chunk we already compared. */
3210
13
      BLOCKCONV_INIT_DELTA('A' - 'a');
3211
13
      BLOCKCONV_STORE(q);
3212
3213
      /* Uppercase the rest of the string. */
3214
13
      p += BLOCKCONV_STRIDE;
3215
13
      q += BLOCKCONV_STRIDE;
3216
13
      zend_str_toupper_impl((char *) q, (const char *) p, end - p);
3217
13
      ZSTR_VAL(res)[length] = '\0';
3218
13
      return res;
3219
13
    }
3220
3
    p += BLOCKCONV_STRIDE;
3221
3
  }
3222
1.04k
#endif
3223
3224
1.06k
  while (p < end) {
3225
919
    if (*p != zend_toupper_ascii(*p)) {
3226
890
      zend_string *res = zend_string_alloc(length, persistent);
3227
890
      memcpy(ZSTR_VAL(res), ZSTR_VAL(str), p - (unsigned char*) ZSTR_VAL(str));
3228
3229
890
      unsigned char *q = (unsigned char *) ZSTR_VAL(res) + (p - (unsigned char *) ZSTR_VAL(str));
3230
7.65k
      while (p < end) {
3231
6.76k
        *q++ = zend_toupper_ascii(*p++);
3232
6.76k
      }
3233
890
      ZSTR_VAL(res)[length] = '\0';
3234
890
      return res;
3235
890
    }
3236
29
    p++;
3237
29
  }
3238
3239
150
  return zend_string_copy(str);
3240
1.04k
}
3241
/* }}} */
3242
3243
ZEND_API int ZEND_FASTCALL zend_binary_strcmp(const char *s1, size_t len1, const char *s2, size_t len2) /* {{{ */
3244
83.0k
{
3245
83.0k
  int retval;
3246
3247
83.0k
  if (s1 == s2) {
3248
12.8k
    return 0;
3249
12.8k
  }
3250
70.1k
  retval = memcmp(s1, s2, MIN(len1, len2));
3251
70.1k
  if (!retval) {
3252
12.9k
    return ZEND_THREEWAY_COMPARE(len1, len2);
3253
57.1k
  } else {
3254
57.1k
    return retval;
3255
57.1k
  }
3256
70.1k
}
3257
/* }}} */
3258
3259
ZEND_API int ZEND_FASTCALL zend_binary_strncmp(const char *s1, size_t len1, const char *s2, size_t len2, size_t length) /* {{{ */
3260
48
{
3261
48
  int retval;
3262
3263
48
  if (s1 == s2) {
3264
15
    return 0;
3265
15
  }
3266
33
  retval = memcmp(s1, s2, MIN(length, MIN(len1, len2)));
3267
33
  if (!retval) {
3268
27
    return ZEND_THREEWAY_COMPARE(MIN(length, len1), MIN(length, len2));
3269
27
  } else {
3270
6
    return retval;
3271
6
  }
3272
33
}
3273
/* }}} */
3274
3275
ZEND_API int ZEND_FASTCALL zend_binary_strcasecmp(const char *s1, size_t len1, const char *s2, size_t len2) /* {{{ */
3276
1.48M
{
3277
1.48M
  size_t len;
3278
1.48M
  int c1, c2;
3279
3280
1.48M
  if (s1 == s2) {
3281
18.9k
    return 0;
3282
18.9k
  }
3283
3284
1.46M
  len = MIN(len1, len2);
3285
4.18M
  while (len--) {
3286
3.94M
    c1 = zend_tolower_ascii(*(unsigned char *)s1++);
3287
3.94M
    c2 = zend_tolower_ascii(*(unsigned char *)s2++);
3288
3.94M
    if (c1 != c2) {
3289
1.21M
      return c1 - c2;
3290
1.21M
    }
3291
3.94M
  }
3292
3293
246k
  return ZEND_THREEWAY_COMPARE(len1, len2);
3294
1.46M
}
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
72
{
3299
72
  size_t len;
3300
72
  int c1, c2;
3301
3302
72
  if (s1 == s2) {
3303
5
    return 0;
3304
5
  }
3305
67
  len = MIN(length, MIN(len1, len2));
3306
212
  while (len--) {
3307
158
    c1 = zend_tolower_ascii(*(unsigned char *)s1++);
3308
158
    c2 = zend_tolower_ascii(*(unsigned char *)s2++);
3309
158
    if (c1 != c2) {
3310
13
      return c1 - c2;
3311
13
    }
3312
158
  }
3313
3314
54
  return ZEND_THREEWAY_COMPARE(MIN(length, len1), MIN(length, len2));
3315
67
}
3316
/* }}} */
3317
3318
ZEND_API int ZEND_FASTCALL zend_binary_strcasecmp_l(const char *s1, size_t len1, const char *s2, size_t len2) /* {{{ */
3319
0
{
3320
0
  size_t len;
3321
0
  int c1, c2;
3322
3323
0
  if (s1 == s2) {
3324
0
    return 0;
3325
0
  }
3326
3327
0
  len = MIN(len1, len2);
3328
0
  while (len--) {
3329
0
    c1 = zend_tolower((int)*(unsigned char *)s1++);
3330
0
    c2 = zend_tolower((int)*(unsigned char *)s2++);
3331
0
    if (c1 != c2) {
3332
0
      return c1 - c2;
3333
0
    }
3334
0
  }
3335
3336
0
  return ZEND_THREEWAY_COMPARE(len1, len2);
3337
0
}
3338
/* }}} */
3339
3340
ZEND_API int ZEND_FASTCALL zend_binary_strncasecmp_l(const char *s1, size_t len1, const char *s2, size_t len2, size_t length) /* {{{ */
3341
0
{
3342
0
  size_t len;
3343
0
  int c1, c2;
3344
3345
0
  if (s1 == s2) {
3346
0
    return 0;
3347
0
  }
3348
0
  len = MIN(length, MIN(len1, len2));
3349
0
  while (len--) {
3350
0
    c1 = zend_tolower((int)*(unsigned char *)s1++);
3351
0
    c2 = zend_tolower((int)*(unsigned char *)s2++);
3352
0
    if (c1 != c2) {
3353
0
      return c1 - c2;
3354
0
    }
3355
0
  }
3356
3357
0
  return ZEND_THREEWAY_COMPARE(MIN(length, len1), MIN(length, len2));
3358
0
}
3359
/* }}} */
3360
3361
ZEND_API int ZEND_FASTCALL zend_binary_zval_strcmp(zval *s1, zval *s2) /* {{{ */
3362
0
{
3363
0
  return zend_binary_strcmp(Z_STRVAL_P(s1), Z_STRLEN_P(s1), Z_STRVAL_P(s2), Z_STRLEN_P(s2));
3364
0
}
3365
/* }}} */
3366
3367
ZEND_API int ZEND_FASTCALL zend_binary_zval_strncmp(zval *s1, zval *s2, zval *s3) /* {{{ */
3368
0
{
3369
0
  return zend_binary_strncmp(Z_STRVAL_P(s1), Z_STRLEN_P(s1), Z_STRVAL_P(s2), Z_STRLEN_P(s2), Z_LVAL_P(s3));
3370
0
}
3371
/* }}} */
3372
3373
ZEND_API bool ZEND_FASTCALL zendi_smart_streq(zend_string *s1, zend_string *s2) /* {{{ */
3374
1.26k
{
3375
1.26k
  uint8_t ret1, ret2;
3376
1.26k
  int oflow1, oflow2;
3377
1.26k
  zend_long lval1 = 0, lval2 = 0;
3378
1.26k
  double dval1 = 0.0, dval2 = 0.0;
3379
3380
1.26k
  if ((ret1 = is_numeric_string_ex(s1->val, s1->len, &lval1, &dval1, false, &oflow1, NULL)) &&
3381
839
    (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
758
    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
758
    if ((ret1 == IS_DOUBLE) || (ret2 == IS_DOUBLE)) {
3394
400
      if (ret1 != IS_DOUBLE) {
3395
130
        if (oflow2) {
3396
          /* 2nd operand is integer > LONG_MAX (oflow2==1) or < LONG_MIN (-1) */
3397
111
          return 0;
3398
111
        }
3399
19
        dval1 = (double) lval1;
3400
270
      } else if (ret2 != IS_DOUBLE) {
3401
130
        if (oflow1) {
3402
93
          return 0;
3403
93
        }
3404
37
        dval2 = (double) lval2;
3405
140
      } 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
81
        goto string_cmp;
3409
81
      }
3410
115
      return dval1 == dval2;
3411
400
    } else { /* they both have to be long's */
3412
358
      return lval1 == lval2;
3413
358
    }
3414
758
  } else {
3415
585
string_cmp:
3416
585
    return zend_string_equal_content(s1, s2);
3417
504
  }
3418
1.26k
}
3419
/* }}} */
3420
3421
ZEND_API int ZEND_FASTCALL zendi_smart_strcmp(zend_string *s1, zend_string *s2) /* {{{ */
3422
6.47k
{
3423
6.47k
  uint8_t ret1, ret2;
3424
6.47k
  int oflow1, oflow2;
3425
6.47k
  zend_long lval1 = 0, lval2 = 0;
3426
6.47k
  double dval1 = 0.0, dval2 = 0.0;
3427
3428
6.47k
  if ((ret1 = is_numeric_string_ex(s1->val, s1->len, &lval1, &dval1, false, &oflow1, NULL)) &&
3429
2.75k
    (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
1.72k
    if (oflow1 != 0 && oflow1 == oflow2 && dval1 - dval2 == 0.) {
3436
42
#endif
3437
      /* both values are integers overflowed to the same side, and the
3438
       * double comparison may have resulted in crucial accuracy lost */
3439
42
      goto string_cmp;
3440
42
    }
3441
1.68k
    if ((ret1 == IS_DOUBLE) || (ret2 == IS_DOUBLE)) {
3442
1.02k
      if (ret1 != IS_DOUBLE) {
3443
169
        if (oflow2) {
3444
          /* 2nd operand is integer > LONG_MAX (oflow2==1) or < LONG_MIN (-1) */
3445
77
          return -1 * oflow2;
3446
77
        }
3447
92
        dval1 = (double) lval1;
3448
852
      } else if (ret2 != IS_DOUBLE) {
3449
441
        if (oflow1) {
3450
76
          return oflow1;
3451
76
        }
3452
365
        dval2 = (double) lval2;
3453
411
      } else if (dval1 == dval2 && !zend_finite(dval1)) {
3454
        /* Both values overflowed and have the same sign,
3455
         * so a numeric comparison would be inaccurate */
3456
48
        goto string_cmp;
3457
48
      }
3458
820
      dval1 = dval1 - dval2;
3459
820
      return ZEND_NORMALIZE_BOOL(dval1);
3460
1.02k
    } else { /* they both have to be long's */
3461
659
      return lval1 > lval2 ? 1 : (lval1 < lval2 ? -1 : 0);
3462
659
    }
3463
4.75k
  } else {
3464
4.75k
    int strcmp_ret;
3465
4.84k
string_cmp:
3466
4.84k
    strcmp_ret = zend_binary_strcmp(s1->val, s1->len, s2->val, s2->len);
3467
4.84k
    return ZEND_NORMALIZE_BOOL(strcmp_ret);
3468
4.75k
  }
3469
6.47k
}
3470
/* }}} */
3471
3472
static int hash_zval_compare_function(zval *z1, zval *z2) /* {{{ */
3473
347
{
3474
347
  return zend_compare(z1, z2);
3475
347
}
3476
/* }}} */
3477
3478
ZEND_API int ZEND_FASTCALL zend_compare_symbol_tables(HashTable *ht1, HashTable *ht2) /* {{{ */
3479
4.45k
{
3480
4.45k
  if (ht1 == ht2) {
3481
911
    return 0;
3482
911
  }
3483
3484
3.54k
  GC_TRY_ADDREF(ht1);
3485
3.54k
  GC_TRY_ADDREF(ht2);
3486
3487
3.54k
  int ret = zend_hash_compare(ht1, ht2, (compare_func_t) hash_zval_compare_function, 0);
3488
3489
3.54k
  GC_TRY_DTOR_NO_REF(ht1);
3490
3.54k
  GC_TRY_DTOR_NO_REF(ht2);
3491
3492
3.54k
  return ret;
3493
4.45k
}
3494
/* }}} */
3495
3496
ZEND_API int ZEND_FASTCALL zend_compare_arrays(zval *a1, zval *a2) /* {{{ */
3497
4.40k
{
3498
4.40k
  return zend_compare_symbol_tables(Z_ARRVAL_P(a1), Z_ARRVAL_P(a2));
3499
4.40k
}
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
979k
{
3518
979k
  if ((zend_ulong)num <= 9) {
3519
856k
    return ZSTR_CHAR((zend_uchar)'0' + (zend_uchar)num);
3520
856k
  } else {
3521
122k
    char buf[MAX_LENGTH_OF_LONG + 1];
3522
122k
    char *res = zend_print_long_to_buf(buf + sizeof(buf) - 1, num);
3523
122k
    zend_string *str =  zend_string_init(res, buf + sizeof(buf) - 1 - res, 0);
3524
122k
    GC_ADD_FLAGS(str, IS_STR_VALID_UTF8);
3525
122k
    return str;
3526
122k
  }
3527
979k
}
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
73.9k
{
3602
73.9k
  char buf[ZEND_DOUBLE_MAX_LENGTH];
3603
  /* Model snprintf precision behavior. */
3604
73.9k
  int precision = (int) EG(precision);
3605
73.9k
  zend_gcvt(num, precision ? precision : 1, '.', 'E', buf);
3606
73.9k
  zend_string *str =  zend_string_init(buf, strlen(buf), 0);
3607
73.9k
  if (UNEXPECTED(zend_isnan(num))) {
3608
347
    zend_nan_coerced_to_type_warning(IS_STRING);
3609
347
  }
3610
73.9k
  GC_ADD_FLAGS(str, IS_STR_VALID_UTF8);
3611
73.9k
  return str;
3612
73.9k
}
3613
3614
ZEND_API uint8_t ZEND_FASTCALL is_numeric_str_function(const zend_string *str, zend_long *lval, double *dval) /* {{{ */
3615
11.5k
{
3616
11.5k
  return is_numeric_string(ZSTR_VAL(str), ZSTR_LEN(str), lval, dval, false);
3617
11.5k
}
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
155k
{
3623
155k
  const char *ptr;
3624
155k
  int digits = 0, dp_or_e = 0;
3625
155k
  double local_dval = 0.0;
3626
155k
  uint8_t type;
3627
155k
  zend_ulong tmp_lval = 0;
3628
155k
  int neg = 0;
3629
3630
155k
  if (!length) {
3631
16.3k
    return 0;
3632
16.3k
  }
3633
3634
139k
  if (oflow_info != NULL) {
3635
9.19k
    *oflow_info = 0;
3636
9.19k
  }
3637
139k
  if (trailing_data != NULL) {
3638
35.4k
    *trailing_data = false;
3639
35.4k
  }
3640
3641
  /* Skip any whitespace
3642
   * This is much faster than the isspace() function */
3643
171k
  while (*str == ' ' || *str == '\t' || *str == '\n' || *str == '\r' || *str == '\v' || *str == '\f') {
3644
31.6k
    str++;
3645
31.6k
    length--;
3646
31.6k
  }
3647
139k
  ptr = str;
3648
3649
139k
  if (*ptr == '-') {
3650
31.2k
    neg = 1;
3651
31.2k
    ptr++;
3652
108k
  } else if (*ptr == '+') {
3653
1.38k
    ptr++;
3654
1.38k
  }
3655
3656
139k
  if (ZEND_IS_DIGIT(*ptr)) {
3657
    /* Skip any leading 0s */
3658
151M
    while (*ptr == '0') {
3659
151M
      ptr++;
3660
151M
    }
3661
3662
    /* Count the number of digits. If a decimal point/exponent is found,
3663
     * it's a double. Otherwise, if there's a dval or no need to check for
3664
     * a full match, stop when there are too many digits for a long */
3665
862k
    for (type = IS_LONG; !(digits >= MAX_LENGTH_OF_LONG && (dval || allow_errors)); digits++, ptr++) {
3666
861k
check_digits:
3667
861k
      if (ZEND_IS_DIGIT(*ptr)) {
3668
760k
        tmp_lval = tmp_lval * 10 + (*ptr) - '0';
3669
760k
        continue;
3670
760k
      } else if (*ptr == '.' && dp_or_e < 1) {
3671
16.7k
        goto process_double;
3672
84.4k
      } else if ((*ptr == 'e' || *ptr == 'E') && dp_or_e < 2) {
3673
10.3k
        const char *e = ptr + 1;
3674
3675
10.3k
        if (*e == '-' || *e == '+') {
3676
1.34k
          ptr = e++;
3677
1.34k
        }
3678
10.3k
        if (ZEND_IS_DIGIT(*e)) {
3679
4.99k
          goto process_double;
3680
4.99k
        }
3681
10.3k
      }
3682
3683
79.4k
      break;
3684
861k
    }
3685
3686
85.3k
    if (digits >= MAX_LENGTH_OF_LONG) {
3687
10.0k
      if (oflow_info != NULL) {
3688
873
        *oflow_info = *str == '-' ? -1 : 1;
3689
873
      }
3690
10.0k
      dp_or_e = -1;
3691
10.0k
      goto process_double;
3692
10.0k
    }
3693
85.3k
  } else if (*ptr == '.' && ZEND_IS_DIGIT(ptr[1])) {
3694
34.3k
process_double:
3695
34.3k
    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
34.3k
    if (dval) {
3700
24.5k
      local_dval = zend_strtod(str, &ptr);
3701
24.5k
    } else if (!allow_errors && dp_or_e != -1) {
3702
5.26k
      dp_or_e = (*ptr++ == '.') ? 1 : 2;
3703
5.26k
      goto check_digits;
3704
5.26k
    }
3705
35.1k
  } else {
3706
35.1k
    return 0;
3707
35.1k
  }
3708
3709
104k
  if (ptr != str + length) {
3710
46.7k
    const char *endptr = ptr;
3711
61.2k
    while (*endptr == ' ' || *endptr == '\t' || *endptr == '\n' || *endptr == '\r' || *endptr == '\v' || *endptr == '\f') {
3712
14.4k
      endptr++;
3713
14.4k
      length--;
3714
14.4k
    }
3715
46.7k
    if (ptr != str + length) {
3716
45.6k
      if (!allow_errors) {
3717
16.4k
        return 0;
3718
16.4k
      }
3719
29.2k
      if (trailing_data != NULL) {
3720
17.6k
        *trailing_data = true;
3721
17.6k
      }
3722
29.2k
    }
3723
46.7k
  }
3724
3725
87.9k
  if (type == IS_LONG) {
3726
59.2k
    if (digits == MAX_LENGTH_OF_LONG - 1) {
3727
5.49k
      int cmp = strcmp(&ptr[-digits], long_min_digits);
3728
3729
5.49k
      if (!(cmp < 0 || (cmp == 0 && *str == '-'))) {
3730
2.87k
        if (dval) {
3731
2.22k
          *dval = zend_strtod(str, NULL);
3732
2.22k
        }
3733
2.87k
        if (oflow_info != NULL) {
3734
158
          *oflow_info = *str == '-' ? -1 : 1;
3735
158
        }
3736
3737
2.87k
        return IS_DOUBLE;
3738
2.87k
      }
3739
5.49k
    }
3740
3741
56.3k
    if (lval) {
3742
45.3k
      if (neg) {
3743
15.6k
        tmp_lval = -tmp_lval;
3744
15.6k
      }
3745
45.3k
      *lval = (zend_long) tmp_lval;
3746
45.3k
    }
3747
3748
56.3k
    return IS_LONG;
3749
59.2k
  } else {
3750
28.6k
    if (dval) {
3751
23.2k
      *dval = local_dval;
3752
23.2k
    }
3753
3754
28.6k
    return IS_DOUBLE;
3755
28.6k
  }
3756
87.9k
}
3757
/* }}} */
3758
3759
/*
3760
 * String matching - Sunday algorithm
3761
 * http://www.iti.fh-flensburg.de/lang/algorithmen/pattern/sundayen.htm
3762
 */
3763
81
static zend_always_inline void zend_memnstr_ex_pre(unsigned int td[], const char *needle, size_t needle_len, int reverse) /* {{{ */ {
3764
81
  int i;
3765
3766
20.8k
  for (i = 0; i < 256; i++) {
3767
20.7k
    td[i] = needle_len + 1;
3768
20.7k
  }
3769
3770
81
  if (reverse) {
3771
0
    for (i = needle_len - 1; i >= 0; i--) {
3772
0
      td[(unsigned char)needle[i]] = i + 1;
3773
0
    }
3774
81
  } else {
3775
81
    size_t i;
3776
3777
8.59k
    for (i = 0; i < needle_len; i++) {
3778
8.51k
      td[(unsigned char)needle[i]] = (int)needle_len - i;
3779
8.51k
    }
3780
81
  }
3781
81
}
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
81
{
3786
81
  unsigned int td[256];
3787
81
  size_t i;
3788
81
  const char *p;
3789
3790
81
  if (needle_len == 0 || (end - haystack) < needle_len) {
3791
0
    return NULL;
3792
0
  }
3793
3794
81
  zend_memnstr_ex_pre(td, needle, needle_len, 0);
3795
3796
81
  p = haystack;
3797
81
  end -= needle_len;
3798
3799
2.93k
  while (p <= end) {
3800
2.99k
    for (i = 0; i < needle_len; i++) {
3801
2.99k
      if (needle[i] != p[i]) {
3802
2.85k
        break;
3803
2.85k
      }
3804
2.99k
    }
3805
2.85k
    if (i == needle_len) {
3806
0
      return p;
3807
0
    }
3808
2.85k
    if (UNEXPECTED(p == end)) {
3809
5
      return NULL;
3810
5
    }
3811
2.84k
    p += td[(unsigned char)(p[needle_len])];
3812
2.84k
  }
3813
3814
76
  return NULL;
3815
81
}
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
29.9k
{
3872
29.9k
  double  two_pow_64 = pow(2., 64.),
3873
29.9k
      dmod;
3874
3875
29.9k
  dmod = fmod(d, two_pow_64);
3876
29.9k
  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.38k
    dmod += two_pow_64;
3880
4.38k
  }
3881
29.9k
  return (zend_long)(zend_ulong)dmod;
3882
29.9k
}
3883
/* }}} */
3884
#endif