Skip to content

Commit 348297d

Browse files
committed
Alter php_stristr() instead of creating new function
1 parent 70bd296 commit 348297d

File tree

7 files changed

+14
-33
lines changed

7 files changed

+14
-33
lines changed

UPGRADING.INTERNALS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ PHP 8.2 INTERNALS UPGRADE NOTES
1717
zend_binary_str(n)casecmp() as one would expect. Call the appropriate
1818
wrapped function directly instead.
1919
* Removed the (ZEND_)WRONG_PARAM_COUNT_WITH_RETVAL() macros.
20+
* php_stristr() no longer lowercases the haystack and needle as a side effect.
21+
Call zend_str_tolower() yourself if necessary. You no longer need to copy
22+
the haystack and needle before passing them to php_stristr().
2023

2124
========================
2225
2. Build system changes

ext/libxml/libxml.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -378,9 +378,8 @@ php_libxml_input_buffer_create_filename(const char *URI, xmlCharEncoding enc)
378378
const char buf[] = "Content-Type:";
379379
if (Z_TYPE_P(header) == IS_STRING &&
380380
!zend_binary_strncasecmp(Z_STRVAL_P(header), Z_STRLEN_P(header), buf, sizeof(buf)-1, sizeof(buf)-1)) {
381-
char *needle = estrdup("charset=");
382-
char *haystack = estrndup(Z_STRVAL_P(header), Z_STRLEN_P(header));
383-
char *encoding = php_stristr(haystack, needle, Z_STRLEN_P(header), sizeof("charset=")-1);
381+
char needle[] = "charset=";
382+
char *encoding = php_stristr(Z_STRVAL_P(header), needle, Z_STRLEN_P(header), strlen(needle));
384383

385384
if (encoding) {
386385
char *end;
@@ -407,8 +406,6 @@ php_libxml_input_buffer_create_filename(const char *URI, xmlCharEncoding enc)
407406
enc = XML_CHAR_ENCODING_NONE;
408407
}
409408
}
410-
efree(haystack);
411-
efree(needle);
412409
break; /* found content-type */
413410
}
414411
} ZEND_HASH_FOREACH_END();

ext/phar/phar.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2531,7 +2531,6 @@ int phar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int conv
25312531
{
25322532
char halt_stub[] = "__HALT_COMPILER();";
25332533
zend_string *newstub;
2534-
char *tmp;
25352534
phar_entry_info *entry, *newentry;
25362535
size_t halt_offset;
25372536
int restore_alias_len, global_flags = 0, closeoldfile;
@@ -2635,9 +2634,7 @@ int phar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int conv
26352634
} else {
26362635
free_user_stub = 0;
26372636
}
2638-
tmp = estrndup(user_stub, len);
2639-
if ((pos = php_stristr(tmp, halt_stub, len, sizeof(halt_stub) - 1)) == NULL) {
2640-
efree(tmp);
2637+
if ((pos = php_stristr(user_stub, halt_stub, len, sizeof(halt_stub) - 1)) == NULL) {
26412638
if (closeoldfile) {
26422639
php_stream_close(oldfile);
26432640
}
@@ -2650,8 +2647,7 @@ int phar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int conv
26502647
}
26512648
return EOF;
26522649
}
2653-
pos = user_stub + (pos - tmp);
2654-
efree(tmp);
2650+
pos = user_stub + (pos - user_stub);
26552651
len = pos - user_stub + 18;
26562652
if ((size_t)len != php_stream_write(newfile, user_stub, len)
26572653
|| 5 != php_stream_write(newfile, " ?>\r\n", 5)) {

ext/phar/tar.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -967,7 +967,7 @@ int phar_tar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int
967967
int closeoldfile, free_user_stub;
968968
size_t signature_length;
969969
struct _phar_pass_tar_info pass;
970-
char *buf, *signature, *tmp, sigbuf[8];
970+
char *buf, *signature, sigbuf[8];
971971
char halt_stub[] = "__HALT_COMPILER();";
972972

973973
entry.flags = PHAR_ENT_PERM_DEF_FILE;
@@ -1063,9 +1063,7 @@ int phar_tar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int
10631063
free_user_stub = 0;
10641064
}
10651065

1066-
tmp = estrndup(user_stub, len);
1067-
if ((pos = php_stristr(tmp, halt_stub, len, sizeof(halt_stub) - 1)) == NULL) {
1068-
efree(tmp);
1066+
if ((pos = php_stristr(user_stub, halt_stub, len, sizeof(halt_stub) - 1)) == NULL) {
10691067
if (error) {
10701068
spprintf(error, 0, "illegal stub for tar-based phar \"%s\"", phar->fname);
10711069
}
@@ -1074,8 +1072,7 @@ int phar_tar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int
10741072
}
10751073
return EOF;
10761074
}
1077-
pos = user_stub + (pos - tmp);
1078-
efree(tmp);
1075+
pos = user_stub + (pos - user_stub);
10791076

10801077
len = pos - user_stub + 18;
10811078
entry.fp = php_stream_fopen_tmpfile();

ext/phar/zip.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,7 +1202,6 @@ int phar_zip_flush(phar_archive_data *phar, char *user_stub, zend_long len, int
12021202
char *pos;
12031203
static const char newstub[] = "<?php // zip-based phar archive stub file\n__HALT_COMPILER();";
12041204
char halt_stub[] = "__HALT_COMPILER();";
1205-
char *tmp;
12061205

12071206
php_stream *stubfile, *oldfile;
12081207
int free_user_stub, closeoldfile = 0;
@@ -1305,9 +1304,7 @@ int phar_zip_flush(phar_archive_data *phar, char *user_stub, zend_long len, int
13051304
free_user_stub = 0;
13061305
}
13071306

1308-
tmp = estrndup(user_stub, len);
1309-
if ((pos = php_stristr(tmp, halt_stub, len, sizeof(halt_stub) - 1)) == NULL) {
1310-
efree(tmp);
1307+
if ((pos = php_stristr(user_stub, halt_stub, len, sizeof(halt_stub) - 1)) == NULL) {
13111308
if (error) {
13121309
spprintf(error, 0, "illegal stub for zip-based phar \"%s\"", phar->fname);
13131310
}
@@ -1316,8 +1313,7 @@ int phar_zip_flush(phar_archive_data *phar, char *user_stub, zend_long len, int
13161313
}
13171314
return EOF;
13181315
}
1319-
pos = user_stub + (pos - tmp);
1320-
efree(tmp);
1316+
pos = user_stub + (pos - user_stub);
13211317

13221318
len = pos - user_stub + 18;
13231319
entry.fp = php_stream_fopen_tmpfile();

ext/standard/php_string.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ PHPAPI void php_stripcslashes(zend_string *str);
4848
PHPAPI zend_string *php_basename(const char *s, size_t len, const char *suffix, size_t sufflen);
4949
PHPAPI size_t php_dirname(char *str, size_t len);
5050
PHPAPI char *php_stristr(char *s, char *t, size_t s_len, size_t t_len);
51-
PHPAPI const char *php_stristr_no_tolower(const char *s, const char *t, size_t s_len, size_t t_len);
5251
PHPAPI zend_string *php_str_to_str(const char *haystack, size_t length, const char *needle,
5352
size_t needle_len, const char *str, size_t str_len);
5453
PHPAPI zend_string *php_trim(zend_string *str, const char *what, size_t what_len, int mode);

ext/standard/string.c

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1687,17 +1687,10 @@ PHP_FUNCTION(pathinfo)
16871687
case insensitive strstr */
16881688
PHPAPI char *php_stristr(char *s, char *t, size_t s_len, size_t t_len)
16891689
{
1690-
zend_str_tolower(s, s_len);
1691-
zend_str_tolower(t, t_len);
1692-
return (char*)php_memnstr(s, t, t_len, s + s_len);
1690+
return (char*)php_memnistr(s, t, t_len, s + s_len);
16931691
}
16941692
/* }}} */
16951693

1696-
PHPAPI const char *php_stristr_no_tolower(const char *s, const char *t, size_t s_len, size_t t_len)
1697-
{
1698-
return (const char*)php_memnistr(s, t, t_len, s + s_len);
1699-
}
1700-
17011694
/* {{{ php_strspn */
17021695
PHPAPI size_t php_strspn(const char *s1, const char *s2, const char *s1_end, const char *s2_end)
17031696
{
@@ -1749,7 +1742,7 @@ PHP_FUNCTION(stristr)
17491742
Z_PARAM_BOOL(part)
17501743
ZEND_PARSE_PARAMETERS_END();
17511744

1752-
found = php_stristr_no_tolower(ZSTR_VAL(haystack), ZSTR_VAL(needle), ZSTR_LEN(haystack), ZSTR_LEN(needle));
1745+
found = php_stristr(ZSTR_VAL(haystack), ZSTR_VAL(needle), ZSTR_LEN(haystack), ZSTR_LEN(needle));
17531746

17541747
if (found) {
17551748
found_offset = found - ZSTR_VAL(haystack);

0 commit comments

Comments
 (0)