Skip to content

Commit a307ee3

Browse files
committed
zend: avoid potential integer overflow in zend_string_concat2 and zend_string_concat3
1 parent 630555f commit a307ee3

1 file changed

Lines changed: 4 additions & 3 deletions

File tree

Zend/zend_string.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include "zend.h"
1919
#include "zend_globals.h"
20+
#include "zend_multiply.h"
2021

2122
#ifdef HAVE_VALGRIND
2223
# include "valgrind/callgrind.h"
@@ -473,8 +474,7 @@ ZEND_API zend_string *zend_string_concat2(
473474
const char *str1, size_t str1_len,
474475
const char *str2, size_t str2_len)
475476
{
476-
size_t len = str1_len + str2_len;
477-
zend_string *res = zend_string_alloc(len, 0);
477+
zend_string *res = zend_string_safe_alloc(1, str1_len, str2_len, 0);
478478

479479
char *p = ZSTR_VAL(res);
480480
p = zend_mempcpy(p, str1, str1_len);
@@ -489,7 +489,8 @@ ZEND_API zend_string *zend_string_concat3(
489489
const char *str2, size_t str2_len,
490490
const char *str3, size_t str3_len)
491491
{
492-
size_t len = str1_len + str2_len + str3_len;
492+
size_t tmp_len = zend_safe_address_guarded(1, str1_len, str2_len);
493+
size_t len = zend_safe_address_guarded(1, tmp_len, str3_len);
493494
zend_string *res = zend_string_alloc(len, 0);
494495

495496
char *p = ZSTR_VAL(res);

0 commit comments

Comments
 (0)