From a2ea45211cc265420213b924b8190afb7ff1591c Mon Sep 17 00:00:00 2001 From: Alex Dowad Date: Tue, 5 Dec 2023 21:04:37 +0200 Subject: [PATCH] Return early from mb_get_substr if 'len' parameter is zero This internal function is used to implement mb_strstr, mb_stristr, mb_strrchr, mb_strrichr, mb_substr, mb_strimwidth, mb_trim, and mb_str_pad. All of these functions will be faster if we return early when requested for a zero-length "substring". --- ext/mbstring/mbstring.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/mbstring/mbstring.c b/ext/mbstring/mbstring.c index 6e51b59034f..f218ac431b5 100644 --- a/ext/mbstring/mbstring.c +++ b/ext/mbstring/mbstring.c @@ -2108,7 +2108,7 @@ static zend_string* mb_get_substr(zend_string *input, size_t from, size_t len, c unsigned char *in = (unsigned char*)ZSTR_VAL(input); size_t in_len = ZSTR_LEN(input); - if (from >= in_len) { + if (from >= in_len || len == 0) { /* No supported text encoding decodes to more than one codepoint per byte * So if the number of codepoints to skip >= number of input bytes, * then definitely the output should be empty */