From f25e9ce0fe348b7e5e7e01823704773f1dae5d36 Mon Sep 17 00:00:00 2001 From: Gina Peter Bnayard Date: Tue, 13 Aug 2024 17:48:39 +0200 Subject: [PATCH 1/7] ext/standard/quot_print.c: Minor refactoring to php_hex2int() We already check, and assume, that the value is hexadecimal --- ext/standard/quot_print.c | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/ext/standard/quot_print.c b/ext/standard/quot_print.c index e6aa2e43f4c6c..704fe55cbeab1 100644 --- a/ext/standard/quot_print.c +++ b/ext/standard/quot_print.c @@ -34,16 +34,12 @@ static char php_hex2int(int c) /* {{{ */ { if (isdigit(c)) { return c - '0'; - } - else if (c >= 'A' && c <= 'F') { + } else if (c >= 'A' && c <= 'F') { return c - 'A' + 10; - } - else if (c >= 'a' && c <= 'f') { + } else { + ZEND_ASSERT(c >= 'a' && c <= 'f'); return c - 'a' + 10; } - else { - return -1; - } } /* }}} */ From f74483cccf52e144d1fa2c8dd478758e00e5e5bd Mon Sep 17 00:00:00 2001 From: Gina Peter Bnayard Date: Tue, 13 Aug 2024 17:53:34 +0200 Subject: [PATCH 2/7] ext/standard/quot_print.c: Mark readonly string as const --- ext/standard/quot_print.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/standard/quot_print.c b/ext/standard/quot_print.c index 704fe55cbeab1..a7132df2f7fd3 100644 --- a/ext/standard/quot_print.c +++ b/ext/standard/quot_print.c @@ -141,7 +141,7 @@ PHPAPI zend_string *php_quot_print_encode(const unsigned char *str, size_t lengt { zend_ulong lp = 0; unsigned char c, *d; - char *hex = "0123456789ABCDEF"; + const char *hex = "0123456789ABCDEF"; zend_string *ret; ret = zend_string_safe_alloc(3, (length + (((3 * length)/(PHP_QPRINT_MAXL-9)) + 1)), 0, 0); From 05cc32129f751d577ec036282bc40df867469206 Mon Sep 17 00:00:00 2001 From: Gina Peter Bnayard Date: Tue, 13 Aug 2024 18:12:43 +0200 Subject: [PATCH 3/7] ext/standard/string.c: Refactor php_spn_common_handler() Main objective is to remove the PHP_STR_STR(C)SPN symbols which are only used with this static function --- ext/standard/php_string.h | 3 --- ext/standard/string.c | 9 ++++----- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/ext/standard/php_string.h b/ext/standard/php_string.h index 1be7d9b283772..f00ff830682ce 100644 --- a/ext/standard/php_string.h +++ b/ext/standard/php_string.h @@ -80,7 +80,4 @@ PHPAPI bool php_binary_string_shuffle(php_random_algo_with_state engine, char *s #define PHP_PATHINFO_FILENAME 8 #define PHP_PATHINFO_ALL (PHP_PATHINFO_DIRNAME | PHP_PATHINFO_BASENAME | PHP_PATHINFO_EXTENSION | PHP_PATHINFO_FILENAME) -#define PHP_STR_STRSPN 0 -#define PHP_STR_STRCSPN 1 - #endif /* PHP_STRING_H */ diff --git a/ext/standard/string.c b/ext/standard/string.c index 1082066dcec55..70b5ffd0bd0e9 100644 --- a/ext/standard/string.c +++ b/ext/standard/string.c @@ -207,7 +207,7 @@ PHP_FUNCTION(hex2bin) } /* }}} */ -static void php_spn_common_handler(INTERNAL_FUNCTION_PARAMETERS, int behavior) /* {{{ */ +static void php_spn_common_handler(INTERNAL_FUNCTION_PARAMETERS, bool is_strspn) /* {{{ */ { zend_string *s11, *s22; zend_long start = 0, len = 0; @@ -249,13 +249,12 @@ static void php_spn_common_handler(INTERNAL_FUNCTION_PARAMETERS, int behavior) / RETURN_LONG(0); } - if (behavior == PHP_STR_STRSPN) { + if (is_strspn) { RETURN_LONG(php_strspn(ZSTR_VAL(s11) + start /*str1_start*/, ZSTR_VAL(s22) /*str2_start*/, ZSTR_VAL(s11) + start + len /*str1_end*/, ZSTR_VAL(s22) + ZSTR_LEN(s22) /*str2_end*/)); } else { - ZEND_ASSERT(behavior == PHP_STR_STRCSPN); RETURN_LONG(php_strcspn(ZSTR_VAL(s11) + start /*str1_start*/, ZSTR_VAL(s22) /*str2_start*/, ZSTR_VAL(s11) + start + len /*str1_end*/, @@ -267,14 +266,14 @@ static void php_spn_common_handler(INTERNAL_FUNCTION_PARAMETERS, int behavior) / /* {{{ Finds length of initial segment consisting entirely of characters found in mask. If start or/and length is provided works like strspn(substr($s,$start,$len),$good_chars) */ PHP_FUNCTION(strspn) { - php_spn_common_handler(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_STR_STRSPN); + php_spn_common_handler(INTERNAL_FUNCTION_PARAM_PASSTHRU, /* is_strspn */ true); } /* }}} */ /* {{{ Finds length of initial segment consisting entirely of characters not found in mask. If start or/and length is provide works like strcspn(substr($s,$start,$len),$bad_chars) */ PHP_FUNCTION(strcspn) { - php_spn_common_handler(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_STR_STRCSPN); + php_spn_common_handler(INTERNAL_FUNCTION_PARAM_PASSTHRU, /* is_strspn */ false); } /* }}} */ From 578659cbaa955bf6e986feaa608b42ecde2143a5 Mon Sep 17 00:00:00 2001 From: Gina Peter Bnayard Date: Tue, 13 Aug 2024 19:00:14 +0200 Subject: [PATCH 4/7] ext/standard/string.c: Remove to(upper|lower) PHP API in favour of Zend APIs --- UPGRADING.INTERNALS | 4 ++++ ext/standard/php_string.h | 4 ---- ext/standard/string.c | 30 ------------------------------ 3 files changed, 4 insertions(+), 34 deletions(-) diff --git a/UPGRADING.INTERNALS b/UPGRADING.INTERNALS index bbcc1223c634a..ec2c57854ecca 100644 --- a/UPGRADING.INTERNALS +++ b/UPGRADING.INTERNALS @@ -376,6 +376,10 @@ PHP 8.4 INTERNALS UPGRADE NOTES - The deprecated php_uint32 and php_int32 typedefs have been removed from ext/standard/basic_functions.h. Use the standard uint32_t and int32_t types instead. + - The php_strtoupper(), php_string_toupper(), php_strtolower(), and + php_string_tolower() functions has been removed, use zend_str_toupper(), + zend_string_toupper(), zend_str_tolower(), and zend_string_tolower() + respectively instead. h. ext/session - Added the php_get_session_status() API to get the session status, which is diff --git a/ext/standard/php_string.h b/ext/standard/php_string.h index f00ff830682ce..afb156ad7d8b2 100644 --- a/ext/standard/php_string.h +++ b/ext/standard/php_string.h @@ -34,10 +34,6 @@ PHP_MINIT_FUNCTION(string_intrin); strnatcmp_ex(a, strlen(a), b, strlen(b), 1) PHPAPI int strnatcmp_ex(char const *a, size_t a_len, char const *b, size_t b_len, bool is_case_insensitive); PHPAPI struct lconv *localeconv_r(struct lconv *out); -PHPAPI char *php_strtoupper(char *s, size_t len); -PHPAPI char *php_strtolower(char *s, size_t len); -PHPAPI zend_string *php_string_toupper(zend_string *s); -PHPAPI zend_string *php_string_tolower(zend_string *s); PHPAPI char *php_strtr(char *str, size_t len, const char *str_from, const char *str_to, size_t trlen); PHPAPI zend_string *php_addslashes(zend_string *str); PHPAPI void php_stripslashes(zend_string *str); diff --git a/ext/standard/string.c b/ext/standard/string.c index 70b5ffd0bd0e9..6ebe7db08965f 100644 --- a/ext/standard/string.c +++ b/ext/standard/string.c @@ -1211,21 +1211,6 @@ PHP_FUNCTION(strtok) } /* }}} */ -/* {{{ php_strtoupper */ -PHPAPI char *php_strtoupper(char *s, size_t len) -{ - zend_str_toupper(s, len); - return s; -} -/* }}} */ - -/* {{{ php_string_toupper */ -PHPAPI zend_string *php_string_toupper(zend_string *s) -{ - return zend_string_toupper(s); -} -/* }}} */ - /* {{{ Makes a string uppercase */ PHP_FUNCTION(strtoupper) { @@ -1239,21 +1224,6 @@ PHP_FUNCTION(strtoupper) } /* }}} */ -/* {{{ php_strtolower */ -PHPAPI char *php_strtolower(char *s, size_t len) -{ - zend_str_tolower(s, len); - return s; -} -/* }}} */ - -/* {{{ php_string_tolower */ -PHPAPI zend_string *php_string_tolower(zend_string *s) -{ - return zend_string_tolower(s); -} -/* }}} */ - /* {{{ Makes a string lowercase */ PHP_FUNCTION(strtolower) { From 60a585a090548078de41ca4e505ddf77587d1532 Mon Sep 17 00:00:00 2001 From: Gina Peter Bnayard Date: Tue, 13 Aug 2024 19:15:09 +0200 Subject: [PATCH 5/7] ext/standard/type.c: Remove unused include --- ext/standard/type.c | 1 - 1 file changed, 1 deletion(-) diff --git a/ext/standard/type.c b/ext/standard/type.c index 4f3950e278463..4557014ff5a33 100644 --- a/ext/standard/type.c +++ b/ext/standard/type.c @@ -15,7 +15,6 @@ */ #include "php.h" -#include "php_incomplete_class.h" /* {{{ Returns the type of the variable */ PHP_FUNCTION(gettype) From 07ce1b50f2167498f7b429707649eb097d6bc013 Mon Sep 17 00:00:00 2001 From: Gina Peter Bnayard Date: Tue, 13 Aug 2024 19:24:02 +0200 Subject: [PATCH 6/7] ext/standard/url.c: Stop exposing php_replace_controlchars_ex() This is not used from a quick search on SourceGraph and this allows us to refactor it --- UPGRADING.INTERNALS | 1 + ext/standard/url.c | 36 ++++++++++++------------------------ ext/standard/url.h | 1 - 3 files changed, 13 insertions(+), 25 deletions(-) diff --git a/UPGRADING.INTERNALS b/UPGRADING.INTERNALS index ec2c57854ecca..0ae0ed6bf72e1 100644 --- a/UPGRADING.INTERNALS +++ b/UPGRADING.INTERNALS @@ -380,6 +380,7 @@ PHP 8.4 INTERNALS UPGRADE NOTES php_string_tolower() functions has been removed, use zend_str_toupper(), zend_string_toupper(), zend_str_tolower(), and zend_string_tolower() respectively instead. + - The php_replace_controlchars_ex() function is no longer exposed. h. ext/session - Added the php_get_session_status() API to get the session status, which is diff --git a/ext/standard/url.c b/ext/standard/url.c index 093650bf991b9..7d564b510bc9f 100644 --- a/ext/standard/url.c +++ b/ext/standard/url.c @@ -49,31 +49,19 @@ PHPAPI void php_url_free(php_url *theurl) } /* }}} */ -/* {{{ php_replace_controlchars_ex */ -PHPAPI char *php_replace_controlchars_ex(char *str, size_t len) +static void php_replace_controlchars(char *str, size_t len) { unsigned char *s = (unsigned char *)str; unsigned char *e = (unsigned char *)str + len; - if (!str) { - return (NULL); - } + ZEND_ASSERT(str != NULL); while (s < e) { - if (iscntrl(*s)) { *s='_'; } s++; } - - return (str); -} -/* }}} */ - -PHPAPI char *php_replace_controlchars(char *str) -{ - return php_replace_controlchars_ex(str, strlen(str)); } PHPAPI php_url *php_url_parse(char const *str) @@ -133,7 +121,7 @@ PHPAPI php_url *php_url_parse_ex2(char const *str, size_t length, bool *has_port if (e + 1 == ue) { /* only scheme is available */ ret->scheme = zend_string_init(s, (e - s), 0); - php_replace_controlchars_ex(ZSTR_VAL(ret->scheme), ZSTR_LEN(ret->scheme)); + php_replace_controlchars(ZSTR_VAL(ret->scheme), ZSTR_LEN(ret->scheme)); return ret; } @@ -155,13 +143,13 @@ PHPAPI php_url *php_url_parse_ex2(char const *str, size_t length, bool *has_port } ret->scheme = zend_string_init(s, (e-s), 0); - php_replace_controlchars_ex(ZSTR_VAL(ret->scheme), ZSTR_LEN(ret->scheme)); + php_replace_controlchars(ZSTR_VAL(ret->scheme), ZSTR_LEN(ret->scheme)); s = e + 1; goto just_path; } else { ret->scheme = zend_string_init(s, (e-s), 0); - php_replace_controlchars_ex(ZSTR_VAL(ret->scheme), ZSTR_LEN(ret->scheme)); + php_replace_controlchars(ZSTR_VAL(ret->scheme), ZSTR_LEN(ret->scheme)); if (e + 2 < ue && *(e + 2) == '/') { s = e + 3; @@ -227,14 +215,14 @@ PHPAPI php_url *php_url_parse_ex2(char const *str, size_t length, bool *has_port if ((p = zend_memrchr(s, '@', (e-s)))) { if ((pp = memchr(s, ':', (p-s)))) { ret->user = zend_string_init(s, (pp-s), 0); - php_replace_controlchars_ex(ZSTR_VAL(ret->user), ZSTR_LEN(ret->user)); + php_replace_controlchars(ZSTR_VAL(ret->user), ZSTR_LEN(ret->user)); pp++; ret->pass = zend_string_init(pp, (p-pp), 0); - php_replace_controlchars_ex(ZSTR_VAL(ret->pass), ZSTR_LEN(ret->pass)); + php_replace_controlchars(ZSTR_VAL(ret->pass), ZSTR_LEN(ret->pass)); } else { ret->user = zend_string_init(s, (p-s), 0); - php_replace_controlchars_ex(ZSTR_VAL(ret->user), ZSTR_LEN(ret->user)); + php_replace_controlchars(ZSTR_VAL(ret->user), ZSTR_LEN(ret->user)); } s = p + 1; @@ -283,7 +271,7 @@ PHPAPI php_url *php_url_parse_ex2(char const *str, size_t length, bool *has_port } ret->host = zend_string_init(s, (p-s), 0); - php_replace_controlchars_ex(ZSTR_VAL(ret->host), ZSTR_LEN(ret->host)); + php_replace_controlchars(ZSTR_VAL(ret->host), ZSTR_LEN(ret->host)); if (e == ue) { return ret; @@ -299,7 +287,7 @@ PHPAPI php_url *php_url_parse_ex2(char const *str, size_t length, bool *has_port p++; if (p < e) { ret->fragment = zend_string_init(p, (e - p), 0); - php_replace_controlchars_ex(ZSTR_VAL(ret->fragment), ZSTR_LEN(ret->fragment)); + php_replace_controlchars(ZSTR_VAL(ret->fragment), ZSTR_LEN(ret->fragment)); } else { ret->fragment = ZSTR_EMPTY_ALLOC(); } @@ -311,7 +299,7 @@ PHPAPI php_url *php_url_parse_ex2(char const *str, size_t length, bool *has_port p++; if (p < e) { ret->query = zend_string_init(p, (e - p), 0); - php_replace_controlchars_ex(ZSTR_VAL(ret->query), ZSTR_LEN(ret->query)); + php_replace_controlchars(ZSTR_VAL(ret->query), ZSTR_LEN(ret->query)); } else { ret->query = ZSTR_EMPTY_ALLOC(); } @@ -320,7 +308,7 @@ PHPAPI php_url *php_url_parse_ex2(char const *str, size_t length, bool *has_port if (s < e || s == ue) { ret->path = zend_string_init(s, (e - s), 0); - php_replace_controlchars_ex(ZSTR_VAL(ret->path), ZSTR_LEN(ret->path)); + php_replace_controlchars(ZSTR_VAL(ret->path), ZSTR_LEN(ret->path)); } return ret; diff --git a/ext/standard/url.h b/ext/standard/url.h index 5ce9e756eef06..4126ee6c6db40 100644 --- a/ext/standard/url.h +++ b/ext/standard/url.h @@ -36,7 +36,6 @@ PHPAPI size_t php_url_decode(char *str, size_t len); /* return value: length of PHPAPI size_t php_raw_url_decode(char *str, size_t len); /* return value: length of decoded string */ PHPAPI zend_string *php_url_encode(char const *s, size_t len); PHPAPI zend_string *php_raw_url_encode(char const *s, size_t len); -PHPAPI char *php_replace_controlchars_ex(char *str, size_t len); #define PHP_URL_SCHEME 0 #define PHP_URL_HOST 1 From 74330b9efcd57050903db62e12cdead969489310 Mon Sep 17 00:00:00 2001 From: Gina Peter Bnayard Date: Tue, 13 Aug 2024 19:33:53 +0200 Subject: [PATCH 7/7] ext/standard/versioning.c: Slightly refactor compare_special_version_forms() --- ext/standard/versioning.c | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/ext/standard/versioning.c b/ext/standard/versioning.c index aa60d97467229..6995569fbf875 100644 --- a/ext/standard/versioning.c +++ b/ext/standard/versioning.c @@ -77,36 +77,36 @@ php_canonicalize_version(const char *version) typedef struct { const char *name; + uint8_t name_len; int order; } special_forms_t; -static int -compare_special_version_forms(char *form1, char *form2) +static int compare_special_version_forms(char *form1, char *form2) { int found1 = -1, found2 = -1; special_forms_t special_forms[11] = { - {"dev", 0}, - {"alpha", 1}, - {"a", 1}, - {"beta", 2}, - {"b", 2}, - {"RC", 3}, - {"rc", 3}, - {"#", 4}, - {"pl", 5}, - {"p", 5}, - {NULL, 0}, + {ZEND_STRL("dev"), 0}, + {ZEND_STRL("alpha"), 1}, + {ZEND_STRL("a"), 1}, + {ZEND_STRL("beta"), 2}, + {ZEND_STRL("b"), 2}, + {ZEND_STRL("RC"), 3}, + {ZEND_STRL("rc"), 3}, + {ZEND_STRL("#"), 4}, + {ZEND_STRL("pl"), 5}, + {ZEND_STRL("p"), 5}, + {NULL, 0, 0}, }; special_forms_t *pp; for (pp = special_forms; pp && pp->name; pp++) { - if (strncmp(form1, pp->name, strlen(pp->name)) == 0) { + if (strncmp(form1, pp->name, pp->name_len) == 0) { found1 = pp->order; break; } } for (pp = special_forms; pp && pp->name; pp++) { - if (strncmp(form2, pp->name, strlen(pp->name)) == 0) { + if (strncmp(form2, pp->name, pp->name_len) == 0) { found2 = pp->order; break; }