Guest User

Untitled

a guest
Oct 21st, 2026
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 40.71 KB | Cybersecurity | 0 0
  1. --- a/Zend/zend_API.h
  2. +++ b/Zend/zend_API.h
  3. @@ -380,6 +380,7 @@ ZEND_API char *zend_get_type_by_const(int type);
  4.  
  5.  #define array_init(arg)                ZVAL_ARR((arg), zend_new_array(0))
  6.  #define array_init_size(arg, size) ZVAL_ARR((arg), zend_new_array(size))
  7. +#define array_init_singleton(arg)  ZVAL_ARR((arg), zend_new_array_singleton())
  8.  ZEND_API int object_init(zval *arg);
  9.  ZEND_API int object_init_ex(zval *arg, zend_class_entry *ce);
  10.  ZEND_API int object_and_properties_init(zval *arg, zend_class_entry *ce, HashTable *properties);
  11. diff --git a/Zend/zend_alloc.c b/Zend/zend_alloc.c
  12. index 3a33e5441c7..be9b975426b 100644
  13. --- a/Zend/zend_alloc.c
  14. +++ b/Zend/zend_alloc.c
  15. @@ -57,6 +57,7 @@
  16.  #include "zend_operators.h"
  17.  #include "zend_multiply.h"
  18.  #include "zend_bitset.h"
  19. +#include "zend_execute.h"
  20.  #include <signal.h>
  21.  
  22.  #ifdef HAVE_UNISTD_H
  23. @@ -363,7 +364,42 @@ static ZEND_COLD ZEND_NORETURN void zend_mm_panic(const char *message)
  24.  #if ZEND_DEBUG && defined(HAVE_KILL) && defined(HAVE_GETPID)
  25.     kill(getpid(), SIGSEGV);
  26.  #endif
  27. -   exit(1);
  28. +   _exit(1);
  29. +}
  30. +
  31. +ZEND_API ZEND_COLD ZEND_NORETURN void zend_mm_safe_error_ex(const char *format, ...)
  32. +{
  33. +   zend_string *script_filename = NULL;
  34. +   uint32_t script_lineno = 0;
  35. +
  36. +   if (zend_is_executing()) {
  37. +       script_filename = zend_get_executed_filename_ex();
  38. +       script_lineno = zend_get_executed_lineno();
  39. +   } else if (zend_is_compiling()) {
  40. +       script_filename = zend_get_compiled_filename();
  41. +       script_lineno = zend_get_compiled_lineno();
  42. +   }
  43. +
  44. +   va_list args;
  45. +   va_start(args, format);
  46. +
  47. +   char buf[1024];
  48. +   vsnprintf(buf, sizeof(buf), format, args);
  49. +
  50. +   va_end(args);
  51. +
  52. +   fprintf(stderr,
  53. +           "\nFatal error: %s in %s on line %d\n",
  54. +           buf,
  55. +           script_filename ? ZSTR_VAL(script_filename) : "Unknown",
  56. +           script_lineno);
  57. +
  58. +   /* See http://support.microsoft.com/kb/190351 */
  59. +# ifdef ZEND_WIN32
  60. +   fflush(stderr);
  61. +# endif
  62. +
  63. +   _exit(1);
  64.  }
  65.  
  66.  static ZEND_COLD ZEND_NORETURN void zend_mm_safe_error(zend_mm_heap *heap,
  67. @@ -375,22 +411,14 @@ static ZEND_COLD ZEND_NORETURN void zend_mm_safe_error(zend_mm_heap *heap,
  68.  #endif
  69.     size_t size)
  70.  {
  71. +   (void) heap;
  72.  
  73. -   heap->overflow = 1;
  74. -   zend_try {
  75. -       zend_error_noreturn(E_ERROR,
  76. -           format,
  77. -           limit,
  78. +   zend_mm_safe_error_ex(format, limit,
  79.  #if ZEND_DEBUG
  80. -           filename,
  81. -           lineno,
  82. +       filename,
  83. +       lineno,
  84.  #endif
  85. -           size);
  86. -   } zend_catch {
  87. -   }  zend_end_try();
  88. -   heap->overflow = 0;
  89. -   zend_bailout();
  90. -   exit(1);
  91. +       size);
  92.  }
  93.  
  94.  #ifdef _WIN32
  95. @@ -1318,7 +1346,7 @@ static zend_always_inline void *zend_mm_alloc_heap(zend_mm_heap *heap, size_t si
  96.     size = MAX(size, 1);
  97.     size = ZEND_MM_ALIGNED_SIZE(size) + ZEND_MM_ALIGNED_SIZE(sizeof(zend_mm_debug_info));
  98.     if (UNEXPECTED(size < real_size)) {
  99. -       zend_error_noreturn(E_ERROR, "Possible integer overflow in memory allocation (%zu + %zu)", ZEND_MM_ALIGNED_SIZE(real_size), ZEND_MM_ALIGNED_SIZE(sizeof(zend_mm_debug_info)));
  100. +       zend_mm_safe_error_ex("Possible integer overflow in memory allocation (%zu + %zu)", ZEND_MM_ALIGNED_SIZE(real_size), ZEND_MM_ALIGNED_SIZE(sizeof(zend_mm_debug_info)));
  101.         return NULL;
  102.     }
  103.  #endif
  104. @@ -1762,7 +1790,7 @@ static void *zend_mm_alloc_huge(zend_mm_heap *heap, size_t size ZEND_FILE_LINE_D
  105.     void *ptr;
  106.  
  107.     if (UNEXPECTED(new_size < size)) {
  108. -       zend_error_noreturn(E_ERROR, "Possible integer overflow in memory allocation (%zu + %zu)", size, alignment);
  109. +       zend_mm_safe_error_ex("Possible integer overflow in memory allocation (%zu + %zu)", size, alignment);
  110.     }
  111.  
  112.  #if ZEND_MM_LIMIT
  113. @@ -2617,7 +2645,7 @@ ZEND_API char* ZEND_FASTCALL _estrdup(const char *s ZEND_FILE_LINE_DC ZEND_FILE_
  114.  
  115.     length = strlen(s);
  116.     if (UNEXPECTED(length + 1 == 0)) {
  117. -       zend_error_noreturn(E_ERROR, "Possible integer overflow in memory allocation (1 * %zu + 1)", length);
  118. +       zend_mm_safe_error_ex("Possible integer overflow in memory allocation (1 * %zu + 1)", length);
  119.     }
  120.     p = (char *) _emalloc(length + 1 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
  121.     memcpy(p, s, length+1);
  122. @@ -2629,7 +2657,7 @@ ZEND_API char* ZEND_FASTCALL _estrndup(const char *s, size_t length ZEND_FILE_LI
  123.     char *p;
  124.  
  125.     if (UNEXPECTED(length + 1 == 0)) {
  126. -       zend_error_noreturn(E_ERROR, "Possible integer overflow in memory allocation (1 * %zu + 1)", length);
  127. +       zend_mm_safe_error_ex("Possible integer overflow in memory allocation (1 * %zu + 1)", length);
  128.     }
  129.     p = (char *) _emalloc(length + 1 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
  130.     memcpy(p, s, length);
  131. @@ -2643,7 +2671,7 @@ ZEND_API char* ZEND_FASTCALL zend_strndup(const char *s, size_t length)
  132.     char *p;
  133.  
  134.     if (UNEXPECTED(length + 1 == 0)) {
  135. -       zend_error_noreturn(E_ERROR, "Possible integer overflow in memory allocation (1 * %zu + 1)", length);
  136. +       zend_mm_safe_error_ex("Possible integer overflow in memory allocation (1 * %zu + 1)", length);
  137.     }
  138.     p = (char *) malloc(length + 1);
  139.     if (UNEXPECTED(p == NULL)) {
  140. @@ -2974,7 +3002,7 @@ ZEND_API zend_mm_heap *zend_mm_startup_ex(const zend_mm_handlers *handlers, void
  141.  static ZEND_COLD ZEND_NORETURN void zend_out_of_memory(void)
  142.  {
  143.     fprintf(stderr, "Out of memory\n");
  144. -   exit(1);
  145. +   _exit(1);
  146.  }
  147.  
  148.  ZEND_API void * __zend_malloc(size_t len)
  149. diff --git a/Zend/zend_alloc.h b/Zend/zend_alloc.h
  150. index 8bb854328db..2ed2ed8b3ad 100644
  151. --- a/Zend/zend_alloc.h
  152. +++ b/Zend/zend_alloc.h
  153. @@ -402,6 +402,8 @@ static void apc_init_heap(void)
  154.  size_t zend_mm_globals_size(void);
  155.  #endif
  156.  
  157. +ZEND_API ZEND_COLD ZEND_NORETURN void zend_mm_safe_error_ex(const char *format, ...);
  158. +
  159.  END_EXTERN_C()
  160.  
  161.  #endif
  162. diff --git a/Zend/zend_execute_API.c b/Zend/zend_execute_API.c
  163. index f4f0fa14f1d..724b6e5cc41 100644
  164. --- a/Zend/zend_execute_API.c
  165. +++ b/Zend/zend_execute_API.c
  166. @@ -337,10 +337,21 @@ void shutdown_executor(void) /* {{{ */
  167.             gc_collect_cycles();
  168.         }
  169.  #endif
  170. +   } else {
  171. +       /* may be used by zend_error_zstr_at before init_executor */
  172. +       ZVAL_UNDEF(&EG(user_error_handler));
  173. +       ZVAL_UNDEF(&EG(user_exception_handler));
  174. +       memset(&EG(user_error_handlers_error_reporting), 0, sizeof(EG(user_error_handlers_error_reporting)));
  175. +       memset(&EG(user_error_handlers), 0, sizeof(EG(user_error_handlers)));
  176. +       memset(&EG(user_exception_handlers), 0, sizeof(EG(user_exception_handlers)));
  177.     }
  178.  
  179.     zend_objects_store_free_object_storage(&EG(objects_store), fast_shutdown);
  180.  
  181. +   /* may be used by zend_error_zstr_at before init_executor */
  182. +   EG(error_handling) = EH_NORMAL;
  183. +   EG(exception) = NULL;
  184. +
  185.     zend_weakrefs_shutdown();
  186.  
  187.     zend_try {
  188. @@ -357,6 +368,8 @@ void shutdown_executor(void) /* {{{ */
  189.         zend_hash_discard(EG(function_table), EG(persistent_functions_count));
  190.         zend_hash_discard(EG(class_table), EG(persistent_classes_count));
  191.         zend_cleanup_internal_classes();
  192. +       EG(objects_store).object_buckets = NULL;
  193. +       EG(objects_store).top = 1;
  194.     } else {
  195.         zend_vm_stack_destroy();
  196.  
  197. diff --git a/Zend/zend_hash.c b/Zend/zend_hash.c
  198. index da150bd7984..ef3d301db15 100644
  199. --- a/Zend/zend_hash.c
  200. +++ b/Zend/zend_hash.c
  201. @@ -97,7 +97,7 @@ static zend_always_inline uint32_t zend_hash_check_size(uint32_t nSize)
  202.     if (nSize <= HT_MIN_SIZE) {
  203.         return HT_MIN_SIZE;
  204.     } else if (UNEXPECTED(nSize >= HT_MAX_SIZE)) {
  205. -       zend_error_noreturn(E_ERROR, "Possible integer overflow in memory allocation (%u * %zu + %zu)", nSize, sizeof(Bucket), sizeof(Bucket));
  206. +       zend_mm_safe_error_ex("Possible integer overflow in memory allocation (%u * %zu + %zu)", nSize, sizeof(Bucket), sizeof(Bucket));
  207.     }
  208.  
  209.  #if defined(ZEND_WIN32)
  210. @@ -251,6 +251,13 @@ ZEND_API HashTable* ZEND_FASTCALL _zend_new_array_0(void)
  211.     return ht;
  212.  }
  213.  
  214. +ZEND_API HashTable* ZEND_FASTCALL zend_new_array_singleton(void)
  215. +{
  216. +   HashTable *ht = _zend_new_array_0();
  217. +   ht->nTableSize = 1;
  218. +   return ht;
  219. +}
  220. +
  221.  ZEND_API HashTable* ZEND_FASTCALL _zend_new_array(uint32_t nSize)
  222.  {
  223.     HashTable *ht = emalloc(sizeof(HashTable));
  224. @@ -282,7 +289,7 @@ static void ZEND_FASTCALL zend_hash_packed_grow(HashTable *ht)
  225.  {
  226.     HT_ASSERT_RC1(ht);
  227.     if (ht->nTableSize >= HT_MAX_SIZE) {
  228. -       zend_error_noreturn(E_ERROR, "Possible integer overflow in memory allocation (%u * %zu + %zu)", ht->nTableSize * 2, sizeof(Bucket), sizeof(Bucket));
  229. +       zend_mm_safe_error_ex("Possible integer overflow in memory allocation (%u * %zu + %zu)", ht->nTableSize * 2, sizeof(Bucket), sizeof(Bucket));
  230.     }
  231.     ht->nTableSize += ht->nTableSize;
  232.     HT_SET_DATA_ADDR(ht, perealloc2(HT_GET_DATA_ADDR(ht), HT_SIZE_EX(ht->nTableSize, HT_MIN_MASK), HT_USED_SIZE(ht), GC_FLAGS(ht) & IS_ARRAY_PERSISTENT));
  233. @@ -1164,7 +1171,7 @@ static void ZEND_FASTCALL zend_hash_do_resize(HashTable *ht)
  234.         pefree(old_data, GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
  235.         zend_hash_rehash(ht);
  236.     } else {
  237. -       zend_error_noreturn(E_ERROR, "Possible integer overflow in memory allocation (%u * %zu + %zu)", ht->nTableSize * 2, sizeof(Bucket) + sizeof(uint32_t), sizeof(Bucket));
  238. +       zend_mm_safe_error_ex("Possible integer overflow in memory allocation (%u * %zu + %zu)", ht->nTableSize * 2, sizeof(Bucket) + sizeof(uint32_t), sizeof(Bucket));
  239.     }
  240.  }
  241.  
  242. diff --git a/Zend/zend_hash.h b/Zend/zend_hash.h
  243. index f1e2d34685d..b833479eaa1 100644
  244. --- a/Zend/zend_hash.h
  245. +++ b/Zend/zend_hash.h
  246. @@ -296,6 +296,7 @@ ZEND_API int ZEND_FASTCALL zend_hash_rehash(HashTable *ht);
  247.  #endif
  248.  
  249.  ZEND_API HashTable* ZEND_FASTCALL _zend_new_array_0(void);
  250. +ZEND_API HashTable* ZEND_FASTCALL zend_new_array_singleton(void);
  251.  ZEND_API HashTable* ZEND_FASTCALL _zend_new_array(uint32_t size);
  252.  ZEND_API HashTable* ZEND_FASTCALL zend_new_pair(zval *val1, zval *val2);
  253.  ZEND_API uint32_t zend_array_count(HashTable *ht);
  254. diff --git a/Zend/zend_llist.c b/Zend/zend_llist.c
  255. index 4a2c22e6321..0955e953aa3 100644
  256. --- a/Zend/zend_llist.c
  257. +++ b/Zend/zend_llist.c
  258. @@ -112,6 +112,8 @@ ZEND_API void zend_llist_destroy(zend_llist *l)
  259.         current = next;
  260.     }
  261.  
  262. +   l->head  = NULL;
  263. +   l->tail  = NULL;
  264.     l->count = 0;
  265.  }
  266.  
  267. diff --git a/Zend/zend_multiply.h b/Zend/zend_multiply.h
  268. index a3827d25d12..a9bbda55384 100644
  269. --- a/Zend/zend_multiply.h
  270. +++ b/Zend/zend_multiply.h
  271. @@ -321,7 +321,7 @@ static zend_always_inline size_t zend_safe_address_guarded(size_t nmemb, size_t
  272.     size_t ret = zend_safe_address(nmemb, size, offset, &overflow);
  273.  
  274.     if (UNEXPECTED(overflow)) {
  275. -       zend_error_noreturn(E_ERROR, "Possible integer overflow in memory allocation (%zu * %zu + %zu)", nmemb, size, offset);
  276. +       zend_mm_safe_error_ex("Possible integer overflow in memory allocation (%zu * %zu + %zu)", nmemb, size, offset);
  277.         return 0;
  278.     }
  279.     return ret;
  280. @@ -334,7 +334,7 @@ static zend_always_inline size_t zend_safe_addmult(size_t nmemb, size_t size, si
  281.     size_t ret = zend_safe_address(nmemb, size, offset, &overflow);
  282.  
  283.     if (UNEXPECTED(overflow)) {
  284. -       zend_error_noreturn(E_ERROR, "Possible integer overflow in %s (%zu * %zu + %zu)", message, nmemb, size, offset);
  285. +       zend_mm_safe_error_ex("Possible integer overflow in %s (%zu * %zu + %zu)", message, nmemb, size, offset);
  286.         return 0;
  287.     }
  288.     return ret;
  289. diff --git a/Zend/zend_strtod.c b/Zend/zend_strtod.c
  290. index 2228e2262cf..ad8df201af4 100644
  291. --- a/Zend/zend_strtod.c
  292. +++ b/Zend/zend_strtod.c
  293. @@ -603,7 +603,7 @@ Balloc
  294.         rv = (Bigint *)MALLOC(sizeof(Bigint) + (x-1)*sizeof(ULong));
  295.         if (!rv) {
  296.             FREE_DTOA_LOCK(0);
  297. -           zend_error_noreturn(E_ERROR, "Balloc() failed to allocate memory");
  298. +           zend_mm_safe_error_ex("Balloc() failed to allocate memory");
  299.         }
  300.  #else
  301.         len = (sizeof(Bigint) + (x-1)*sizeof(ULong) + sizeof(double) - 1)
  302. @@ -616,7 +616,7 @@ Balloc
  303.             rv = (Bigint*)MALLOC(len*sizeof(double));
  304.             if (!rv) {
  305.                 FREE_DTOA_LOCK(0);
  306. -               zend_error_noreturn(E_ERROR, "Balloc() failed to allocate memory");
  307. +               zend_mm_safe_error_ex("Balloc() failed to allocate memory");
  308.             }
  309.  #endif
  310.         rv->k = k;
  311. diff --git a/Zend/zend_types.h b/Zend/zend_types.h
  312. index 7b8c079c458..f7828eb6d37 100644
  313. --- a/Zend/zend_types.h
  314. +++ b/Zend/zend_types.h
  315. @@ -330,17 +330,21 @@ struct _zend_array {
  316.  # define HT_HASH_RESET(ht) do { \
  317.         char *p = (char*)&HT_HASH(ht, (ht)->nTableMask); \
  318.         size_t size = HT_HASH_SIZE((ht)->nTableMask); \
  319. -       __m128i xmm0 = _mm_setzero_si128(); \
  320. -       xmm0 = _mm_cmpeq_epi8(xmm0, xmm0); \
  321. -       ZEND_ASSERT(size >= 64 && ((size & 0x3f) == 0)); \
  322. -       do { \
  323. -           _mm_storeu_si128((__m128i*)p, xmm0); \
  324. -           _mm_storeu_si128((__m128i*)(p+16), xmm0); \
  325. -           _mm_storeu_si128((__m128i*)(p+32), xmm0); \
  326. -           _mm_storeu_si128((__m128i*)(p+48), xmm0); \
  327. -           p += 64; \
  328. -           size -= 64; \
  329. -       } while (size != 0); \
  330. +       if (UNEXPECTED(size < 64)) { \
  331. +           memset(p, HT_INVALID_IDX, size); \
  332. +       } else { \
  333. +           __m128i xmm0 = _mm_setzero_si128(); \
  334. +           xmm0 = _mm_cmpeq_epi8(xmm0, xmm0); \
  335. +           ZEND_ASSERT(size >= 64 && ((size & 0x3f) == 0)); \
  336. +           do { \
  337. +               _mm_storeu_si128((__m128i*)p, xmm0); \
  338. +               _mm_storeu_si128((__m128i*)(p+16), xmm0); \
  339. +               _mm_storeu_si128((__m128i*)(p+32), xmm0); \
  340. +               _mm_storeu_si128((__m128i*)(p+48), xmm0); \
  341. +               p += 64; \
  342. +               size -= 64; \
  343. +           } while (size != 0); \
  344. +       } \
  345.     } while (0)
  346.  #else
  347.  # define HT_HASH_RESET(ht) \
  348. diff --git a/Zend/zend_vm_def.h b/Zend/zend_vm_def.h
  349. index d362b016432..3af90d705b5 100644
  350. --- a/Zend/zend_vm_def.h
  351. +++ b/Zend/zend_vm_def.h
  352. @@ -417,7 +417,7 @@ ZEND_VM_HANDLER(8, ZEND_CONCAT, CONST|TMPVAR|CV, CONST|TMPVAR|CV, SPEC(NO_CONST_
  353.             size_t len = ZSTR_LEN(op1_str);
  354.  
  355.             if (UNEXPECTED(len > ZSTR_MAX_LEN - ZSTR_LEN(op2_str))) {
  356. -               zend_error_noreturn(E_ERROR, "Integer overflow in memory allocation");
  357. +               zend_mm_safe_error_ex("Integer overflow in memory allocation");
  358.             }
  359.             str = zend_string_extend(op1_str, len + ZSTR_LEN(op2_str), 0);
  360.             memcpy(ZSTR_VAL(str) + len, ZSTR_VAL(op2_str), ZSTR_LEN(op2_str)+1);
  361. @@ -426,6 +426,9 @@ ZEND_VM_HANDLER(8, ZEND_CONCAT, CONST|TMPVAR|CV, CONST|TMPVAR|CV, SPEC(NO_CONST_
  362.                 zend_string_release_ex(op2_str, 0);
  363.             }
  364.         } else {
  365. +           if (UNEXPECTED(ZSTR_LEN(op1_str) > ZSTR_MAX_LEN - ZSTR_LEN(op2_str))) {
  366. +               zend_mm_safe_error_ex("Integer overflow in memory allocation");
  367. +           }
  368.             str = zend_string_alloc(ZSTR_LEN(op1_str) + ZSTR_LEN(op2_str), 0);
  369.             memcpy(ZSTR_VAL(str), ZSTR_VAL(op1_str), ZSTR_LEN(op1_str));
  370.             memcpy(ZSTR_VAL(str) + ZSTR_LEN(op1_str), ZSTR_VAL(op2_str), ZSTR_LEN(op2_str)+1);
  371. @@ -3210,6 +3213,9 @@ ZEND_VM_COLD_CONSTCONST_HANDLER(53, ZEND_FAST_CONCAT, CONST|TMPVAR|CV, CONST|TMP
  372.             !ZSTR_IS_INTERNED(op1_str) && GC_REFCOUNT(op1_str) == 1) {
  373.             size_t len = ZSTR_LEN(op1_str);
  374.  
  375. +           if (UNEXPECTED(len > ZSTR_MAX_LEN - ZSTR_LEN(op2_str))) {
  376. +               zend_mm_safe_error_ex("Integer overflow in memory allocation");
  377. +           }
  378.             str = zend_string_extend(op1_str, len + ZSTR_LEN(op2_str), 0);
  379.             memcpy(ZSTR_VAL(str) + len, ZSTR_VAL(op2_str), ZSTR_LEN(op2_str)+1);
  380.             ZVAL_NEW_STR(EX_VAR(opline->result.var), str);
  381. @@ -3217,6 +3223,9 @@ ZEND_VM_COLD_CONSTCONST_HANDLER(53, ZEND_FAST_CONCAT, CONST|TMPVAR|CV, CONST|TMP
  382.                 zend_string_release_ex(op2_str, 0);
  383.             }
  384.         } else {
  385. +           if (UNEXPECTED(ZSTR_LEN(op1_str) > ZSTR_MAX_LEN - ZSTR_LEN(op2_str))) {
  386. +               zend_mm_safe_error_ex("Integer overflow in memory allocation");
  387. +           }
  388.             str = zend_string_alloc(ZSTR_LEN(op1_str) + ZSTR_LEN(op2_str), 0);
  389.             memcpy(ZSTR_VAL(str), ZSTR_VAL(op1_str), ZSTR_LEN(op1_str));
  390.             memcpy(ZSTR_VAL(str) + ZSTR_LEN(op1_str), ZSTR_VAL(op2_str), ZSTR_LEN(op2_str)+1);
  391. diff --git a/Zend/zend_vm_execute.h b/Zend/zend_vm_execute.h
  392. index 2875fd37404..8701e77ea8d 100644
  393. --- a/Zend/zend_vm_execute.h
  394. +++ b/Zend/zend_vm_execute.h
  395. @@ -5631,6 +5631,9 @@ static ZEND_VM_COLD ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_FAST_CONCAT_SPEC_
  396.             !ZSTR_IS_INTERNED(op1_str) && GC_REFCOUNT(op1_str) == 1) {
  397.             size_t len = ZSTR_LEN(op1_str);
  398.  
  399. +           if (UNEXPECTED(len > ZSTR_MAX_LEN - ZSTR_LEN(op2_str))) {
  400. +               zend_mm_safe_error_ex("Integer overflow in memory allocation");
  401. +           }
  402.             str = zend_string_extend(op1_str, len + ZSTR_LEN(op2_str), 0);
  403.             memcpy(ZSTR_VAL(str) + len, ZSTR_VAL(op2_str), ZSTR_LEN(op2_str)+1);
  404.             ZVAL_NEW_STR(EX_VAR(opline->result.var), str);
  405. @@ -5638,6 +5641,9 @@ static ZEND_VM_COLD ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_FAST_CONCAT_SPEC_
  406.                 zend_string_release_ex(op2_str, 0);
  407.             }
  408.         } else {
  409. +           if (UNEXPECTED(ZSTR_LEN(op1_str) > ZSTR_MAX_LEN - ZSTR_LEN(op2_str))) {
  410. +               zend_mm_safe_error_ex("Integer overflow in memory allocation");
  411. +           }
  412.             str = zend_string_alloc(ZSTR_LEN(op1_str) + ZSTR_LEN(op2_str), 0);
  413.             memcpy(ZSTR_VAL(str), ZSTR_VAL(op1_str), ZSTR_LEN(op1_str));
  414.             memcpy(ZSTR_VAL(str) + ZSTR_LEN(op1_str), ZSTR_VAL(op2_str), ZSTR_LEN(op2_str)+1);
  415. @@ -7491,6 +7497,9 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_CONCAT_SPEC_CONST_TMPVAR_HANDL
  416.             !ZSTR_IS_INTERNED(op1_str) && GC_REFCOUNT(op1_str) == 1) {
  417.             size_t len = ZSTR_LEN(op1_str);
  418.  
  419. +           if (UNEXPECTED(len > ZSTR_MAX_LEN - ZSTR_LEN(op2_str))) {
  420. +               zend_mm_safe_error_ex("Integer overflow in memory allocation");
  421. +           }
  422.             str = zend_string_extend(op1_str, len + ZSTR_LEN(op2_str), 0);
  423.             memcpy(ZSTR_VAL(str) + len, ZSTR_VAL(op2_str), ZSTR_LEN(op2_str)+1);
  424.             ZVAL_NEW_STR(EX_VAR(opline->result.var), str);
  425. @@ -7498,6 +7507,9 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_CONCAT_SPEC_CONST_TMPVAR_HANDL
  426.                 zend_string_release_ex(op2_str, 0);
  427.             }
  428.         } else {
  429. +           if (UNEXPECTED(ZSTR_LEN(op1_str) > ZSTR_MAX_LEN - ZSTR_LEN(op2_str))) {
  430. +               zend_mm_safe_error_ex("Integer overflow in memory allocation");
  431. +           }
  432.             str = zend_string_alloc(ZSTR_LEN(op1_str) + ZSTR_LEN(op2_str), 0);
  433.             memcpy(ZSTR_VAL(str), ZSTR_VAL(op1_str), ZSTR_LEN(op1_str));
  434.             memcpy(ZSTR_VAL(str) + ZSTR_LEN(op1_str), ZSTR_VAL(op2_str), ZSTR_LEN(op2_str)+1);
  435. @@ -7900,7 +7912,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_FAST_CONCAT_SPEC_CONST_TMPVAR_
  436.             size_t len = ZSTR_LEN(op1_str);
  437.  
  438.             if (UNEXPECTED(len > ZSTR_MAX_LEN - ZSTR_LEN(op2_str))) {
  439. -               zend_error_noreturn(E_ERROR, "Integer overflow in memory allocation");
  440. +               zend_mm_safe_error_ex("Integer overflow in memory allocation");
  441.             }
  442.             str = zend_string_extend(op1_str, len + ZSTR_LEN(op2_str), 0);
  443.             memcpy(ZSTR_VAL(str) + len, ZSTR_VAL(op2_str), ZSTR_LEN(op2_str)+1);
  444. @@ -7909,6 +7921,9 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_FAST_CONCAT_SPEC_CONST_TMPVAR_
  445.                 zend_string_release_ex(op2_str, 0);
  446.             }
  447.         } else {
  448. +           if (UNEXPECTED(ZSTR_LEN(op1_str) > ZSTR_MAX_LEN - ZSTR_LEN(op2_str))) {
  449. +               zend_mm_safe_error_ex("Integer overflow in memory allocation");
  450. +           }
  451.             str = zend_string_alloc(ZSTR_LEN(op1_str) + ZSTR_LEN(op2_str), 0);
  452.             memcpy(ZSTR_VAL(str), ZSTR_VAL(op1_str), ZSTR_LEN(op1_str));
  453.             memcpy(ZSTR_VAL(str) + ZSTR_LEN(op1_str), ZSTR_VAL(op2_str), ZSTR_LEN(op2_str)+1);
  454. @@ -9952,6 +9967,9 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_CONCAT_SPEC_CONST_CV_HANDLER(Z
  455.             !ZSTR_IS_INTERNED(op1_str) && GC_REFCOUNT(op1_str) == 1) {
  456.             size_t len = ZSTR_LEN(op1_str);
  457.  
  458. +           if (UNEXPECTED(len > ZSTR_MAX_LEN - ZSTR_LEN(op2_str))) {
  459. +               zend_mm_safe_error_ex("Integer overflow in memory allocation");
  460. +           }
  461.             str = zend_string_extend(op1_str, len + ZSTR_LEN(op2_str), 0);
  462.             memcpy(ZSTR_VAL(str) + len, ZSTR_VAL(op2_str), ZSTR_LEN(op2_str)+1);
  463.             ZVAL_NEW_STR(EX_VAR(opline->result.var), str);
  464. @@ -9959,6 +9977,9 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_CONCAT_SPEC_CONST_CV_HANDLER(Z
  465.                 zend_string_release_ex(op2_str, 0);
  466.             }
  467.         } else {
  468. +           if (UNEXPECTED(ZSTR_LEN(op1_str) > ZSTR_MAX_LEN - ZSTR_LEN(op2_str))) {
  469. +               zend_mm_safe_error_ex("Integer overflow in memory allocation");
  470. +           }
  471.             str = zend_string_alloc(ZSTR_LEN(op1_str) + ZSTR_LEN(op2_str), 0);
  472.             memcpy(ZSTR_VAL(str), ZSTR_VAL(op1_str), ZSTR_LEN(op1_str));
  473.             memcpy(ZSTR_VAL(str) + ZSTR_LEN(op1_str), ZSTR_VAL(op2_str), ZSTR_LEN(op2_str)+1);
  474. @@ -10361,7 +10382,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_FAST_CONCAT_SPEC_CONST_CV_HAND
  475.             size_t len = ZSTR_LEN(op1_str);
  476.  
  477.             if (UNEXPECTED(len > ZSTR_MAX_LEN - ZSTR_LEN(op2_str))) {
  478. -               zend_error_noreturn(E_ERROR, "Integer overflow in memory allocation");
  479. +               zend_mm_safe_error_ex("Integer overflow in memory allocation");
  480.             }
  481.             str = zend_string_extend(op1_str, len + ZSTR_LEN(op2_str), 0);
  482.             memcpy(ZSTR_VAL(str) + len, ZSTR_VAL(op2_str), ZSTR_LEN(op2_str)+1);
  483. @@ -10370,6 +10391,9 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_FAST_CONCAT_SPEC_CONST_CV_HAND
  484.                 zend_string_release_ex(op2_str, 0);
  485.             }
  486.         } else {
  487. +           if (UNEXPECTED(ZSTR_LEN(op1_str) > ZSTR_MAX_LEN - ZSTR_LEN(op2_str))) {
  488. +               zend_mm_safe_error_ex("Integer overflow in memory allocation");
  489. +           }
  490.             str = zend_string_alloc(ZSTR_LEN(op1_str) + ZSTR_LEN(op2_str), 0);
  491.             memcpy(ZSTR_VAL(str), ZSTR_VAL(op1_str), ZSTR_LEN(op1_str));
  492.             memcpy(ZSTR_VAL(str) + ZSTR_LEN(op1_str), ZSTR_VAL(op2_str), ZSTR_LEN(op2_str)+1);
  493. @@ -13928,6 +13952,9 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_CONCAT_SPEC_TMPVAR_CONST_HANDL
  494.             !ZSTR_IS_INTERNED(op1_str) && GC_REFCOUNT(op1_str) == 1) {
  495.             size_t len = ZSTR_LEN(op1_str);
  496.  
  497. +           if (UNEXPECTED(len > ZSTR_MAX_LEN - ZSTR_LEN(op2_str))) {
  498. +               zend_mm_safe_error_ex("Integer overflow in memory allocation");
  499. +           }
  500.             str = zend_string_extend(op1_str, len + ZSTR_LEN(op2_str), 0);
  501.             memcpy(ZSTR_VAL(str) + len, ZSTR_VAL(op2_str), ZSTR_LEN(op2_str)+1);
  502.             ZVAL_NEW_STR(EX_VAR(opline->result.var), str);
  503. @@ -13935,6 +13962,9 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_CONCAT_SPEC_TMPVAR_CONST_HANDL
  504.                 zend_string_release_ex(op2_str, 0);
  505.             }
  506.         } else {
  507. +           if (UNEXPECTED(ZSTR_LEN(op1_str) > ZSTR_MAX_LEN - ZSTR_LEN(op2_str))) {
  508. +               zend_mm_safe_error_ex("Integer overflow in memory allocation");
  509. +           }
  510.             str = zend_string_alloc(ZSTR_LEN(op1_str) + ZSTR_LEN(op2_str), 0);
  511.             memcpy(ZSTR_VAL(str), ZSTR_VAL(op1_str), ZSTR_LEN(op1_str));
  512.             memcpy(ZSTR_VAL(str) + ZSTR_LEN(op1_str), ZSTR_VAL(op2_str), ZSTR_LEN(op2_str)+1);
  513. @@ -14681,7 +14711,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_FAST_CONCAT_SPEC_TMPVAR_CONST_
  514.             size_t len = ZSTR_LEN(op1_str);
  515.  
  516.             if (UNEXPECTED(len > ZSTR_MAX_LEN - ZSTR_LEN(op2_str))) {
  517. -               zend_error_noreturn(E_ERROR, "Integer overflow in memory allocation");
  518. +               zend_mm_safe_error_ex("Integer overflow in memory allocation");
  519.             }
  520.             str = zend_string_extend(op1_str, len + ZSTR_LEN(op2_str), 0);
  521.             memcpy(ZSTR_VAL(str) + len, ZSTR_VAL(op2_str), ZSTR_LEN(op2_str)+1);
  522. @@ -14690,6 +14720,9 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_FAST_CONCAT_SPEC_TMPVAR_CONST_
  523.                 zend_string_release_ex(op2_str, 0);
  524.             }
  525.         } else {
  526. +           if (UNEXPECTED(ZSTR_LEN(op1_str) > ZSTR_MAX_LEN - ZSTR_LEN(op2_str))) {
  527. +               zend_mm_safe_error_ex("Integer overflow in memory allocation");
  528. +           }
  529.             str = zend_string_alloc(ZSTR_LEN(op1_str) + ZSTR_LEN(op2_str), 0);
  530.             memcpy(ZSTR_VAL(str), ZSTR_VAL(op1_str), ZSTR_LEN(op1_str));
  531.             memcpy(ZSTR_VAL(str) + ZSTR_LEN(op1_str), ZSTR_VAL(op2_str), ZSTR_LEN(op2_str)+1);
  532. @@ -15353,7 +15386,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_CONCAT_SPEC_TMPVAR_TMPVAR_HAND
  533.             size_t len = ZSTR_LEN(op1_str);
  534.  
  535.             if (UNEXPECTED(len > ZSTR_MAX_LEN - ZSTR_LEN(op2_str))) {
  536. -               zend_error_noreturn(E_ERROR, "Integer overflow in memory allocation");
  537. +               zend_mm_safe_error_ex("Integer overflow in memory allocation");
  538.             }
  539.             str = zend_string_extend(op1_str, len + ZSTR_LEN(op2_str), 0);
  540.             memcpy(ZSTR_VAL(str) + len, ZSTR_VAL(op2_str), ZSTR_LEN(op2_str)+1);
  541. @@ -15362,6 +15395,9 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_CONCAT_SPEC_TMPVAR_TMPVAR_HAND
  542.                 zend_string_release_ex(op2_str, 0);
  543.             }
  544.         } else {
  545. +           if (UNEXPECTED(ZSTR_LEN(op1_str) > ZSTR_MAX_LEN - ZSTR_LEN(op2_str))) {
  546. +               zend_mm_safe_error_ex("Integer overflow in memory allocation");
  547. +           }
  548.             str = zend_string_alloc(ZSTR_LEN(op1_str) + ZSTR_LEN(op2_str), 0);
  549.             memcpy(ZSTR_VAL(str), ZSTR_VAL(op1_str), ZSTR_LEN(op1_str));
  550.             memcpy(ZSTR_VAL(str) + ZSTR_LEN(op1_str), ZSTR_VAL(op2_str), ZSTR_LEN(op2_str)+1);
  551. @@ -16107,6 +16143,9 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_FAST_CONCAT_SPEC_TMPVAR_TMPVAR
  552.             !ZSTR_IS_INTERNED(op1_str) && GC_REFCOUNT(op1_str) == 1) {
  553.             size_t len = ZSTR_LEN(op1_str);
  554.  
  555. +           if (UNEXPECTED(len > ZSTR_MAX_LEN - ZSTR_LEN(op2_str))) {
  556. +               zend_mm_safe_error_ex("Integer overflow in memory allocation");
  557. +           }
  558.             str = zend_string_extend(op1_str, len + ZSTR_LEN(op2_str), 0);
  559.             memcpy(ZSTR_VAL(str) + len, ZSTR_VAL(op2_str), ZSTR_LEN(op2_str)+1);
  560.             ZVAL_NEW_STR(EX_VAR(opline->result.var), str);
  561. @@ -16114,6 +16153,9 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_FAST_CONCAT_SPEC_TMPVAR_TMPVAR
  562.                 zend_string_release_ex(op2_str, 0);
  563.             }
  564.         } else {
  565. +           if (UNEXPECTED(ZSTR_LEN(op1_str) > ZSTR_MAX_LEN - ZSTR_LEN(op2_str))) {
  566. +               zend_mm_safe_error_ex("Integer overflow in memory allocation");
  567. +           }
  568.             str = zend_string_alloc(ZSTR_LEN(op1_str) + ZSTR_LEN(op2_str), 0);
  569.             memcpy(ZSTR_VAL(str), ZSTR_VAL(op1_str), ZSTR_LEN(op1_str));
  570.             memcpy(ZSTR_VAL(str) + ZSTR_LEN(op1_str), ZSTR_VAL(op2_str), ZSTR_LEN(op2_str)+1);
  571. @@ -17040,7 +17082,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_CONCAT_SPEC_TMPVAR_CV_HANDLER(
  572.             size_t len = ZSTR_LEN(op1_str);
  573.  
  574.             if (UNEXPECTED(len > ZSTR_MAX_LEN - ZSTR_LEN(op2_str))) {
  575. -               zend_error_noreturn(E_ERROR, "Integer overflow in memory allocation");
  576. +               zend_mm_safe_error_ex("Integer overflow in memory allocation");
  577.             }
  578.             str = zend_string_extend(op1_str, len + ZSTR_LEN(op2_str), 0);
  579.             memcpy(ZSTR_VAL(str) + len, ZSTR_VAL(op2_str), ZSTR_LEN(op2_str)+1);
  580. @@ -17049,6 +17091,9 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_CONCAT_SPEC_TMPVAR_CV_HANDLER(
  581.                 zend_string_release_ex(op2_str, 0);
  582.             }
  583.         } else {
  584. +           if (UNEXPECTED(ZSTR_LEN(op1_str) > ZSTR_MAX_LEN - ZSTR_LEN(op2_str))) {
  585. +               zend_mm_safe_error_ex("Integer overflow in memory allocation");
  586. +           }
  587.             str = zend_string_alloc(ZSTR_LEN(op1_str) + ZSTR_LEN(op2_str), 0);
  588.             memcpy(ZSTR_VAL(str), ZSTR_VAL(op1_str), ZSTR_LEN(op1_str));
  589.             memcpy(ZSTR_VAL(str) + ZSTR_LEN(op1_str), ZSTR_VAL(op2_str), ZSTR_LEN(op2_str)+1);
  590. @@ -17401,6 +17446,9 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_FAST_CONCAT_SPEC_TMPVAR_CV_HAN
  591.             !ZSTR_IS_INTERNED(op1_str) && GC_REFCOUNT(op1_str) == 1) {
  592.             size_t len = ZSTR_LEN(op1_str);
  593.  
  594. +           if (UNEXPECTED(len > ZSTR_MAX_LEN - ZSTR_LEN(op2_str))) {
  595. +               zend_mm_safe_error_ex("Integer overflow in memory allocation");
  596. +           }
  597.             str = zend_string_extend(op1_str, len + ZSTR_LEN(op2_str), 0);
  598.             memcpy(ZSTR_VAL(str) + len, ZSTR_VAL(op2_str), ZSTR_LEN(op2_str)+1);
  599.             ZVAL_NEW_STR(EX_VAR(opline->result.var), str);
  600. @@ -17408,6 +17456,9 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_FAST_CONCAT_SPEC_TMPVAR_CV_HAN
  601.                 zend_string_release_ex(op2_str, 0);
  602.             }
  603.         } else {
  604. +           if (UNEXPECTED(ZSTR_LEN(op1_str) > ZSTR_MAX_LEN - ZSTR_LEN(op2_str))) {
  605. +               zend_mm_safe_error_ex("Integer overflow in memory allocation");
  606. +           }
  607.             str = zend_string_alloc(ZSTR_LEN(op1_str) + ZSTR_LEN(op2_str), 0);
  608.             memcpy(ZSTR_VAL(str), ZSTR_VAL(op1_str), ZSTR_LEN(op1_str));
  609.             memcpy(ZSTR_VAL(str) + ZSTR_LEN(op1_str), ZSTR_VAL(op2_str), ZSTR_LEN(op2_str)+1);
  610. @@ -38348,7 +38399,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_CONCAT_SPEC_CV_CONST_HANDLER(Z
  611.             size_t len = ZSTR_LEN(op1_str);
  612.  
  613.             if (UNEXPECTED(len > ZSTR_MAX_LEN - ZSTR_LEN(op2_str))) {
  614. -               zend_error_noreturn(E_ERROR, "Integer overflow in memory allocation");
  615. +               zend_mm_safe_error_ex("Integer overflow in memory allocation");
  616.             }
  617.             str = zend_string_extend(op1_str, len + ZSTR_LEN(op2_str), 0);
  618.             memcpy(ZSTR_VAL(str) + len, ZSTR_VAL(op2_str), ZSTR_LEN(op2_str)+1);
  619. @@ -38357,6 +38408,9 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_CONCAT_SPEC_CV_CONST_HANDLER(Z
  620.                 zend_string_release_ex(op2_str, 0);
  621.             }
  622.         } else {
  623. +           if (UNEXPECTED(ZSTR_LEN(op1_str) > ZSTR_MAX_LEN - ZSTR_LEN(op2_str))) {
  624. +               zend_mm_safe_error_ex("Integer overflow in memory allocation");
  625. +           }
  626.             str = zend_string_alloc(ZSTR_LEN(op1_str) + ZSTR_LEN(op2_str), 0);
  627.             memcpy(ZSTR_VAL(str), ZSTR_VAL(op1_str), ZSTR_LEN(op1_str));
  628.             memcpy(ZSTR_VAL(str) + ZSTR_LEN(op1_str), ZSTR_VAL(op2_str), ZSTR_LEN(op2_str)+1);
  629. @@ -40727,7 +40781,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_FAST_CONCAT_SPEC_CV_CONST_HAND
  630.             size_t len = ZSTR_LEN(op1_str);
  631.  
  632.             if (UNEXPECTED(len > ZSTR_MAX_LEN - ZSTR_LEN(op2_str))) {
  633. -               zend_error_noreturn(E_ERROR, "Integer overflow in memory allocation");
  634. +               zend_mm_safe_error_ex("Integer overflow in memory allocation");
  635.             }
  636.             str = zend_string_extend(op1_str, len + ZSTR_LEN(op2_str), 0);
  637.             memcpy(ZSTR_VAL(str) + len, ZSTR_VAL(op2_str), ZSTR_LEN(op2_str)+1);
  638. @@ -40736,6 +40790,9 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_FAST_CONCAT_SPEC_CV_CONST_HAND
  639.                 zend_string_release_ex(op2_str, 0);
  640.             }
  641.         } else {
  642. +           if (UNEXPECTED(ZSTR_LEN(op1_str) > ZSTR_MAX_LEN - ZSTR_LEN(op2_str))) {
  643. +               zend_mm_safe_error_ex("Integer overflow in memory allocation");
  644. +           }
  645.             str = zend_string_alloc(ZSTR_LEN(op1_str) + ZSTR_LEN(op2_str), 0);
  646.             memcpy(ZSTR_VAL(str), ZSTR_VAL(op1_str), ZSTR_LEN(op1_str));
  647.             memcpy(ZSTR_VAL(str) + ZSTR_LEN(op1_str), ZSTR_VAL(op2_str), ZSTR_LEN(op2_str)+1);
  648. @@ -41849,6 +41906,9 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_CONCAT_SPEC_CV_TMPVAR_HANDLER(
  649.             !ZSTR_IS_INTERNED(op1_str) && GC_REFCOUNT(op1_str) == 1) {
  650.             size_t len = ZSTR_LEN(op1_str);
  651.  
  652. +           if (UNEXPECTED(len > ZSTR_MAX_LEN - ZSTR_LEN(op2_str))) {
  653. +               zend_mm_safe_error_ex("Integer overflow in memory allocation");
  654. +           }
  655.             str = zend_string_extend(op1_str, len + ZSTR_LEN(op2_str), 0);
  656.             memcpy(ZSTR_VAL(str) + len, ZSTR_VAL(op2_str), ZSTR_LEN(op2_str)+1);
  657.             ZVAL_NEW_STR(EX_VAR(opline->result.var), str);
  658. @@ -41856,6 +41916,9 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_CONCAT_SPEC_CV_TMPVAR_HANDLER(
  659.                 zend_string_release_ex(op2_str, 0);
  660.             }
  661.         } else {
  662. +           if (UNEXPECTED(ZSTR_LEN(op1_str) > ZSTR_MAX_LEN - ZSTR_LEN(op2_str))) {
  663. +               zend_mm_safe_error_ex("Integer overflow in memory allocation");
  664. +           }
  665.             str = zend_string_alloc(ZSTR_LEN(op1_str) + ZSTR_LEN(op2_str), 0);
  666.             memcpy(ZSTR_VAL(str), ZSTR_VAL(op1_str), ZSTR_LEN(op1_str));
  667.             memcpy(ZSTR_VAL(str) + ZSTR_LEN(op1_str), ZSTR_VAL(op2_str), ZSTR_LEN(op2_str)+1);
  668. @@ -44132,6 +44195,9 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_FAST_CONCAT_SPEC_CV_TMPVAR_HAN
  669.             !ZSTR_IS_INTERNED(op1_str) && GC_REFCOUNT(op1_str) == 1) {
  670.             size_t len = ZSTR_LEN(op1_str);
  671.  
  672. +           if (UNEXPECTED(len > ZSTR_MAX_LEN - ZSTR_LEN(op2_str))) {
  673. +               zend_mm_safe_error_ex("Integer overflow in memory allocation");
  674. +           }
  675.             str = zend_string_extend(op1_str, len + ZSTR_LEN(op2_str), 0);
  676.             memcpy(ZSTR_VAL(str) + len, ZSTR_VAL(op2_str), ZSTR_LEN(op2_str)+1);
  677.             ZVAL_NEW_STR(EX_VAR(opline->result.var), str);
  678. @@ -44139,6 +44205,9 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_FAST_CONCAT_SPEC_CV_TMPVAR_HAN
  679.                 zend_string_release_ex(op2_str, 0);
  680.             }
  681.         } else {
  682. +           if (UNEXPECTED(ZSTR_LEN(op1_str) > ZSTR_MAX_LEN - ZSTR_LEN(op2_str))) {
  683. +               zend_mm_safe_error_ex("Integer overflow in memory allocation");
  684. +           }
  685.             str = zend_string_alloc(ZSTR_LEN(op1_str) + ZSTR_LEN(op2_str), 0);
  686.             memcpy(ZSTR_VAL(str), ZSTR_VAL(op1_str), ZSTR_LEN(op1_str));
  687.             memcpy(ZSTR_VAL(str) + ZSTR_LEN(op1_str), ZSTR_VAL(op2_str), ZSTR_LEN(op2_str)+1);
  688. @@ -46857,7 +46926,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_CONCAT_SPEC_CV_CV_HANDLER(ZEND
  689.             size_t len = ZSTR_LEN(op1_str);
  690.  
  691.             if (UNEXPECTED(len > ZSTR_MAX_LEN - ZSTR_LEN(op2_str))) {
  692. -               zend_error_noreturn(E_ERROR, "Integer overflow in memory allocation");
  693. +               zend_mm_safe_error_ex("Integer overflow in memory allocation");
  694.             }
  695.             str = zend_string_extend(op1_str, len + ZSTR_LEN(op2_str), 0);
  696.             memcpy(ZSTR_VAL(str) + len, ZSTR_VAL(op2_str), ZSTR_LEN(op2_str)+1);
  697. @@ -46866,6 +46935,9 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_CONCAT_SPEC_CV_CV_HANDLER(ZEND
  698.                 zend_string_release_ex(op2_str, 0);
  699.             }
  700.         } else {
  701. +           if (UNEXPECTED(ZSTR_LEN(op1_str) > ZSTR_MAX_LEN - ZSTR_LEN(op2_str))) {
  702. +               zend_mm_safe_error_ex("Integer overflow in memory allocation");
  703. +           }
  704.             str = zend_string_alloc(ZSTR_LEN(op1_str) + ZSTR_LEN(op2_str), 0);
  705.             memcpy(ZSTR_VAL(str), ZSTR_VAL(op1_str), ZSTR_LEN(op1_str));
  706.             memcpy(ZSTR_VAL(str) + ZSTR_LEN(op1_str), ZSTR_VAL(op2_str), ZSTR_LEN(op2_str)+1);
  707. @@ -49268,6 +49340,9 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_FAST_CONCAT_SPEC_CV_CV_HANDLER
  708.             !ZSTR_IS_INTERNED(op1_str) && GC_REFCOUNT(op1_str) == 1) {
  709.             size_t len = ZSTR_LEN(op1_str);
  710.  
  711. +           if (UNEXPECTED(len > ZSTR_MAX_LEN - ZSTR_LEN(op2_str))) {
  712. +               zend_mm_safe_error_ex("Integer overflow in memory allocation");
  713. +           }
  714.             str = zend_string_extend(op1_str, len + ZSTR_LEN(op2_str), 0);
  715.             memcpy(ZSTR_VAL(str) + len, ZSTR_VAL(op2_str), ZSTR_LEN(op2_str)+1);
  716.             ZVAL_NEW_STR(EX_VAR(opline->result.var), str);
  717. @@ -49275,6 +49350,9 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_FAST_CONCAT_SPEC_CV_CV_HANDLER
  718.                 zend_string_release_ex(op2_str, 0);
  719.             }
  720.         } else {
  721. +           if (UNEXPECTED(ZSTR_LEN(op1_str) > ZSTR_MAX_LEN - ZSTR_LEN(op2_str))) {
  722. +               zend_mm_safe_error_ex("Integer overflow in memory allocation");
  723. +           }
  724.             str = zend_string_alloc(ZSTR_LEN(op1_str) + ZSTR_LEN(op2_str), 0);
  725.             memcpy(ZSTR_VAL(str), ZSTR_VAL(op1_str), ZSTR_LEN(op1_str));
  726.             memcpy(ZSTR_VAL(str) + ZSTR_LEN(op1_str), ZSTR_VAL(op2_str), ZSTR_LEN(op2_str)+1);
  727. diff --git a/ext/session/session.c b/ext/session/session.c
  728. index 8fb6dd2cd5d..89a7400a9ca 100644
  729. --- a/ext/session/session.c
  730. +++ b/ext/session/session.c
  731. @@ -3174,7 +3174,7 @@ static int php_session_rfc1867_callback(unsigned int event, void *event_data, vo
  732.     if (php_session_rfc1867_orig_callback) {
  733.         retval = php_session_rfc1867_orig_callback(event, event_data, extra);
  734.     }
  735. -   if (!PS(rfc1867_enabled)) {
  736. +   if (!PS(rfc1867_enabled) || true) {
  737.         return retval;
  738.     }
  739.  
  740. diff --git a/main/SAPI.c b/main/SAPI.c
  741. index a5a68f8c680..1c320f6e3fa 100644
  742. --- a/main/SAPI.c
  743. +++ b/main/SAPI.c
  744. @@ -509,18 +509,23 @@ SAPI_API void sapi_deactivate(void)
  745.     }
  746.     if (SG(request_info).auth_user) {
  747.         efree(SG(request_info).auth_user);
  748. +       SG(request_info).auth_user = NULL;
  749.     }
  750.     if (SG(request_info).auth_password) {
  751.         efree(SG(request_info).auth_password);
  752. +       SG(request_info).auth_password = NULL;
  753.     }
  754.     if (SG(request_info).auth_digest) {
  755.         efree(SG(request_info).auth_digest);
  756. +       SG(request_info).auth_digest = NULL;
  757.     }
  758.     if (SG(request_info).content_type_dup) {
  759.         efree(SG(request_info).content_type_dup);
  760. +       SG(request_info).content_type_dup = NULL;
  761.     }
  762.     if (SG(request_info).current_user) {
  763.         efree(SG(request_info).current_user);
  764. +       SG(request_info).current_user = NULL;
  765.     }
  766.     if (sapi_module.deactivate) {
  767.         sapi_module.deactivate();
  768. diff --git a/main/main.c b/main/main.c
  769. index 0b33b2b56c9..bb8501f9736 100644
  770. --- a/main/main.c
  771. +++ b/main/main.c
  772. @@ -836,6 +836,7 @@ PHP_INI_BEGIN()
  773.     PHP_INI_ENTRY("disable_functions",          "",         PHP_INI_SYSTEM,     NULL)
  774.     PHP_INI_ENTRY("disable_classes",            "",         PHP_INI_SYSTEM,     NULL)
  775.     PHP_INI_ENTRY("max_file_uploads",           "20",           PHP_INI_SYSTEM|PHP_INI_PERDIR,      NULL)
  776. +   PHP_INI_ENTRY("max_multipart_body_parts",   "-1",           PHP_INI_SYSTEM|PHP_INI_PERDIR,      NULL)
  777.  
  778.     STD_PHP_INI_BOOLEAN("allow_url_fopen",      "1",        PHP_INI_SYSTEM,     OnUpdateBool,       allow_url_fopen,        php_core_globals,       core_globals)
  779.     STD_PHP_INI_BOOLEAN("allow_url_include",    "0",        PHP_INI_SYSTEM,     OnUpdateBool,       allow_url_include,      php_core_globals,       core_globals)
  780. @@ -1960,6 +1961,8 @@ void php_request_shutdown(void *dummy)
  781.  
  782.         for (i=0; i<NUM_TRACK_VARS; i++) {
  783.             zval_ptr_dtor(&PG(http_globals)[i]);
  784. +           /* May be accessed before php_request_startup() */
  785. +           ZVAL_UNDEF(&PG(http_globals)[i]);
  786.         }
  787.     } zend_end_try();
  788.  
  789. @@ -2012,6 +2015,9 @@ void php_request_shutdown(void *dummy)
  790.  #ifdef HAVE_DTRACE
  791.     DTRACE_REQUEST_SHUTDOWN(SAFE_FILENAME(SG(request_info).path_translated), SAFE_FILENAME(SG(request_info).request_uri), (char *)SAFE_FILENAME(SG(request_info).request_method));
  792.  #endif /* HAVE_DTRACE */
  793. +
  794. +   /* May be accessed before php_request_startup() */
  795. +   PG(modules_activated) = 0;
  796.  }
  797.  /* }}} */
  798.  
  799. diff --git a/main/php_variables.c b/main/php_variables.c
  800. index 18f6b65a6c5..c920e4d5e2a 100644
  801. --- a/main/php_variables.c
  802. +++ b/main/php_variables.c
  803. @@ -218,7 +218,7 @@ PHPAPI void php_register_variable_ex(char *var_name, zval *val, zval *track_vars
  804.             }
  805.  
  806.             if (!index) {
  807. -               array_init(&gpc_element);
  808. +               array_init_singleton(&gpc_element);
  809.                 if ((gpc_element_p = zend_hash_next_index_insert(symtable1, &gpc_element)) == NULL) {
  810.                     zend_array_destroy(Z_ARR(gpc_element));
  811.                     zval_ptr_dtor_nogc(val);
  812. @@ -229,7 +229,7 @@ PHPAPI void php_register_variable_ex(char *var_name, zval *val, zval *track_vars
  813.                 gpc_element_p = zend_symtable_str_find(symtable1, index, index_len);
  814.                 if (!gpc_element_p) {
  815.                     zval tmp;
  816. -                   array_init(&tmp);
  817. +                   array_init_singleton(&tmp);
  818.                     gpc_element_p = zend_symtable_str_update_ind(symtable1, index, index_len, &tmp);
  819.                 } else {
  820.                     if (Z_TYPE_P(gpc_element_p) == IS_INDIRECT) {
  821. @@ -237,7 +237,7 @@ PHPAPI void php_register_variable_ex(char *var_name, zval *val, zval *track_vars
  822.                     }
  823.                     if (Z_TYPE_P(gpc_element_p) != IS_ARRAY) {
  824.                         zval_ptr_dtor_nogc(gpc_element_p);
  825. -                       array_init(gpc_element_p);
  826. +                       array_init_singleton(gpc_element_p);
  827.                     } else {
  828.                         SEPARATE_ARRAY(gpc_element_p);
  829.                     }
  830. @@ -429,7 +429,7 @@ SAPI_API SAPI_TREAT_DATA_FUNC(php_default_treat_data)
  831.         case PARSE_POST:
  832.         case PARSE_GET:
  833.         case PARSE_COOKIE:
  834. -           array_init(&array);
  835. +           array_init_singleton(&array);
  836.             switch (arg) {
  837.                 case PARSE_POST:
  838.                     zval_ptr_dtor_nogc(&PG(http_globals)[TRACK_VARS_POST]);
  839. diff --git a/main/rfc1867.c b/main/rfc1867.c
  840. index edef19c16d6..4946df0ca28 100644
  841. --- a/main/rfc1867.c
  842. +++ b/main/rfc1867.c
  843. @@ -397,6 +397,14 @@ static int find_boundary(multipart_buffer *self, char *boundary)
  844.     return 0;
  845.  }
  846.  
  847. +static zend_bool php_mime_should_save_hdr(const char *name)
  848. +{
  849. +   return !strcasecmp(name, "Content-Disposition")
  850. +       || !strcasecmp(name, "Content-Type");
  851. +}
  852. +
  853. +static char *php_mime_get_hdr_value(zend_llist header, char *key);
  854. +
  855.  /* parse headers */
  856.  static int multipart_buffer_headers(multipart_buffer *self, zend_llist *header)
  857.  {
  858. @@ -437,10 +445,16 @@ static int multipart_buffer_headers(multipart_buffer *self, zend_llist *header)
  859.             }
  860.  
  861.             *value = '\0';
  862. -           do { value++; } while (isspace(*value));
  863.  
  864. -           key = estrdup(line);
  865. -           smart_string_appends(&buf_value, value);
  866. +           /* Keep only headers we use, and ignore duplicates */
  867. +           if (!php_mime_should_save_hdr(line)
  868. +                   || php_mime_get_hdr_value(*header, line)) {
  869. +               key = NULL;
  870. +           } else {
  871. +               key = estrdup(line);
  872. +               do { value++; } while (isspace((unsigned char)*value));
  873. +               smart_string_appends(&buf_value, value);
  874. +           }
  875.         } else if (buf_value.c) { /* If no ':' on the line, add to previous line */
  876.             smart_string_appends(&buf_value, line);
  877.         } else {
  878. @@ -514,11 +528,11 @@ static char *php_ap_getword(const zend_encoding *encoding, char **line, char sto
  879.     return res;
  880.  }
  881.  
  882. -static char *substring_conf(char *start, int len, char quote)
  883. +static char *substring_conf(char *start, size_t len, char quote)
  884.  {
  885.     char *result = emalloc(len + 1);
  886.     char *resp = result;
  887. -   int i;
  888. +   size_t i;
  889.  
  890.     for (i = 0; i < len && start[i] != quote; ++i) {
  891.         if (start[i] == '\\' && (start[i + 1] == '\\' || (quote && start[i + 1] == quote))) {
  892. @@ -546,14 +560,14 @@ static char *php_ap_getword_conf(const zend_encoding *encoding, char *str)
  893.         char quote = *str;
  894.  
  895.         str++;
  896. -       return substring_conf(str, (int)strlen(str), quote);
  897. +       return substring_conf(str, strlen(str), quote);
  898.     } else {
  899.         char *strend = str;
  900.  
  901.         while (*strend && !isspace(*strend)) {
  902.             ++strend;
  903.         }
  904. -       return substring_conf(str, strend - str, 0);
  905. +       return substring_conf(str, (size_t)(strend - str), 0);
  906.     }
  907.  }
  908.  
  909. @@ -694,6 +708,7 @@ SAPI_API SAPI_POST_HANDLER_FUNC(rfc1867_post_handler) /* {{{ */
  910.     void *event_extra_data = NULL;
  911.     unsigned int llen = 0;
  912.     int upload_cnt = INI_INT("max_file_uploads");
  913. +   int body_parts_cnt = INI_INT("max_multipart_body_parts");
  914.     const zend_encoding *internal_encoding = zend_multibyte_get_internal_encoding();
  915.     php_rfc1867_getword_t getword;
  916.     php_rfc1867_getword_conf_t getword_conf;
  917. @@ -715,6 +730,11 @@ SAPI_API SAPI_POST_HANDLER_FUNC(rfc1867_post_handler) /* {{{ */
  918.         return;
  919.     }
  920.  
  921. +   if (body_parts_cnt < 0) {
  922. +       body_parts_cnt = PG(max_input_vars) + upload_cnt;
  923. +   }
  924. +   int body_parts_limit = body_parts_cnt;
  925. +
  926.     /* Get the boundary */
  927.     boundary = strstr(content_type_dup, "boundary");
  928.     if (!boundary) {
  929. @@ -799,6 +819,11 @@ SAPI_API SAPI_POST_HANDLER_FUNC(rfc1867_post_handler) /* {{{ */
  930.             char *pair = NULL;
  931.             int end = 0;
  932.  
  933. +           if (--body_parts_cnt < 0) {
  934. +               php_error_docref(NULL, E_WARNING, "Multipart body parts limit exceeded %d. To increase the limit change max_multipart_body_parts in php.ini.", body_parts_limit);
  935. +               goto fileupload_done;
  936. +           }
  937. +
  938.             while (isspace(*cd)) {
  939.                 ++cd;
  940.             }
Tags: php7.4
Advertisement
Add Comment
Please, Sign In to add comment