Skip to content

Commit 898e157

Browse files
committed
fixed trim() and strtok() to work with big strings
1 parent 8e05b91 commit 898e157

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

Zend/zend_execute.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -759,11 +759,14 @@ static inline void zend_assign_to_object(zval *retval, zval *object_ptr, zval *p
759759
static void zend_assign_to_string_offset(zval *str_offset, zval *value, int value_type, zval *result TSRMLS_DC)
760760
{
761761
zval *str = Z_STR_OFFSET_STR_P(str_offset);
762-
uint32_t offset = Z_STR_OFFSET_IDX_P(str_offset);
762+
/* XXX String offset is uint32_t in _zval_struct, so can address only 2^32+1 space.
763+
To make the offset get over that barier, we need to make str_offset size_t and that
764+
would grow zval size by 8 bytes (currently from 16 to 24) on 64 bit build. */
765+
size_t offset = (size_t)Z_STR_OFFSET_IDX_P(str_offset);
763766
zend_string *old_str;
764767

765-
if ((int)offset < 0) {
766-
zend_error(E_WARNING, "Illegal string offset: %d", offset);
768+
if ((zend_long)offset < 0) {
769+
zend_error(E_WARNING, "Illegal string offset: %zd", offset);
767770
zend_string_release(Z_STR_P(str));
768771
if (result) {
769772
ZVAL_NULL(result);
@@ -773,7 +776,7 @@ static void zend_assign_to_string_offset(zval *str_offset, zval *value, int valu
773776

774777
old_str = Z_STR_P(str);
775778
if (offset >= Z_STRLEN_P(str)) {
776-
int old_len = Z_STRLEN_P(str);
779+
size_t old_len = Z_STRLEN_P(str);
777780
Z_STR_P(str) = zend_string_realloc(Z_STR_P(str), offset + 1, 0);
778781
Z_TYPE_INFO_P(str) = IS_STRING_EX;
779782
memset(Z_STRVAL_P(str) + old_len, ' ', offset - old_len);

ext/standard/string.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,7 @@ static inline int php_charmask(unsigned char *input, size_t len, char *mask TSRM
781781
PHPAPI char *php_trim(char *c, size_t len, char *what, size_t what_len, zval *return_value, int mode TSRMLS_DC)
782782
{
783783
register zend_long i;
784-
int trimmed = 0;
784+
size_t trimmed = 0;
785785
char mask[256];
786786

787787
if (what) {
@@ -1257,7 +1257,7 @@ PHP_FUNCTION(strtok)
12571257
char *token_end;
12581258
char *p;
12591259
char *pe;
1260-
int skipped = 0;
1260+
size_t skipped = 0;
12611261

12621262
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "S|S", &str, &tok) == FAILURE) {
12631263
return;
@@ -1465,7 +1465,7 @@ PHPAPI zend_string *php_basename(const char *s, size_t len, char *suffix, size_t
14651465
if (state == 1) {
14661466
cend = c;
14671467
}
1468-
if (suffix != NULL && sufflen < (uint)(cend - comp) &&
1468+
if (suffix != NULL && sufflen < (size_t)(cend - comp) &&
14691469
memcmp(cend - sufflen, suffix, sufflen) == 0) {
14701470
cend -= sufflen;
14711471
}
@@ -1554,7 +1554,7 @@ PHP_FUNCTION(pathinfo)
15541554

15551555
if ((opt & PHP_PATHINFO_EXTENSION) == PHP_PATHINFO_EXTENSION) {
15561556
const char *p;
1557-
int idx;
1557+
ptrdiff_t idx;
15581558

15591559
if (!have_basename) {
15601560
ret = php_basename(path, path_len, NULL, 0 TSRMLS_CC);
@@ -1570,7 +1570,7 @@ PHP_FUNCTION(pathinfo)
15701570

15711571
if ((opt & PHP_PATHINFO_FILENAME) == PHP_PATHINFO_FILENAME) {
15721572
const char *p;
1573-
int idx;
1573+
ptrdiff_t idx;
15741574

15751575
/* Have we already looked up the basename? */
15761576
if (!have_basename && !ret) {

0 commit comments

Comments
 (0)