Skip to content

Allow empty needles in mbstring functions #4977

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 27 additions & 5 deletions ext/mbstring/libmbfl/mbfl/mbfilter.c
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,7 @@ filter_count_output(int c, void *data)
}

size_t
mbfl_strlen(mbfl_string *string)
mbfl_strlen(const mbfl_string *string)
{
size_t len, n, k;
unsigned char *p;
Expand Down Expand Up @@ -855,13 +855,35 @@ mbfl_strpos(
needle_u8 = needle;
}

if (needle_u8->len < 1) {
result = (size_t) -8;
result = (size_t) -1;
if (haystack_u8->len < needle_u8->len) {
goto out;
}

result = (size_t) -1;
if (haystack_u8->len < needle_u8->len) {
if (needle_u8->len == 0) {
size_t haystack_length = mbfl_strlen(haystack_u8);
/* Check if offset is out of bound */
if (
(offset > 0 && offset > haystack_length)
|| (offset < 0 && -offset > haystack_length)
) {
result = -16;
goto out;
}

if (reverse) {
if (offset < 0) {
result = haystack_length + offset;
} else {
result = haystack_length;
}
} else {
if (offset < 0) {
result = haystack_length + offset;
} else {
Comment on lines +881 to +883
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I move the if (offset < 0) condition out? As it's the same for both now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds reasonable.

result = (size_t) offset;
}
}
goto out;
}

Expand Down
2 changes: 1 addition & 1 deletion ext/mbstring/libmbfl/mbfl/mbfilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ static inline int mbfl_is_error(size_t len) {
* strlen
*/
MBFLAPI extern size_t
mbfl_strlen(mbfl_string *string);
mbfl_strlen(const mbfl_string *string);

/*
* oddlen
Expand Down
24 changes: 0 additions & 24 deletions ext/mbstring/mbstring.c
Original file line number Diff line number Diff line change
Expand Up @@ -2110,11 +2110,6 @@ PHP_FUNCTION(mb_strpos)
}
}

if (needle.len == 0) {
php_error_docref(NULL, E_WARNING, "Empty delimiter");
RETURN_FALSE;
}

n = mbfl_strpos(&haystack, &needle, offset, reverse);
if (!mbfl_is_error(n)) {
RETVAL_LONG(n);
Expand Down Expand Up @@ -2189,11 +2184,6 @@ PHP_FUNCTION(mb_stripos)
RETURN_THROWS();
}

if (needle.len == 0) {
php_error_docref(NULL, E_WARNING, "Empty delimiter");
RETURN_FALSE;
}

n = php_mb_stripos(0, (char *)haystack.val, haystack.len, (char *)needle.val, needle.len, offset, from_encoding);

if (!mbfl_is_error(n)) {
Expand Down Expand Up @@ -2246,11 +2236,6 @@ PHP_FUNCTION(mb_strstr)
RETURN_FALSE;
}

if (needle.len == 0) {
php_error_docref(NULL, E_WARNING, "Empty delimiter");
RETURN_FALSE;
}

n = mbfl_strpos(&haystack, &needle, 0, 0);
if (!mbfl_is_error(n)) {
if (part) {
Expand Down Expand Up @@ -2350,11 +2335,6 @@ PHP_FUNCTION(mb_stristr)
RETURN_FALSE;
}

if (!needle.len) {
php_error_docref(NULL, E_WARNING, "Empty delimiter");
RETURN_FALSE;
}

n = php_mb_stripos(0, (char *)haystack.val, haystack.len, (char *)needle.val, needle.len, 0, from_encoding);
if (mbfl_is_error(n)) {
RETURN_FALSE;
Expand Down Expand Up @@ -4849,10 +4829,6 @@ MBSTRING_API size_t php_mb_stripos(int mode, const char *old_haystack, size_t ol
break;
}

if (needle.len == 0) {
break;
}

if (offset != 0) {
size_t haystack_char_len = mbfl_strlen(&haystack);

Expand Down
86 changes: 86 additions & 0 deletions ext/mbstring/tests/mb_stripos_empty_needle.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
--TEST--
Test mb_stripos() function : with empty needle
--SKIPIF--
<?php
extension_loaded('mbstring') or die('skip');
function_exists('mb_stripos') or die("skip mb_stripos() is not available in this build");
?>
--FILE--
<?php

mb_internal_encoding('UTF-8');

$string_ascii = 'abc def';
// Japanese string in UTF-8
$string_mb = "日本語テキストです。0123456789。";

echo "\n-- ASCII string without offset --\n";
var_dump(mb_stripos($string_ascii, ''));

echo "\n-- ASCII string with in range positive offset --\n";
var_dump(mb_stripos($string_ascii, '', 2));

echo "\n-- ASCII string with in range negative offset --\n";
var_dump(mb_stripos($string_ascii, '', -2));

echo "\n-- ASCII string with out of bound positive offset --\n";
var_dump(mb_stripos($string_ascii, '', 150));

echo "\n-- ASCII string with out of bound negative offset --\n";
var_dump(mb_stripos($string_ascii, '', -150));


echo "\n-- Multi-byte string without offset --\n";
var_dump(mb_stripos($string_mb, ''));

echo "\n-- Multi-byte string with in range positive offset --\n";
var_dump(mb_stripos($string_mb, '', 2));

echo "\n-- Multi-byte string with in range negative offset --\n";
var_dump(mb_stripos($string_mb, '', -2));

echo "\n-- Multi-byte string with out of bound positive offset --\n";
var_dump(mb_stripos($string_mb, '', 150));

echo "\n-- Multi-byte string with out of bound negative offset --\n";
var_dump(mb_stripos($string_mb, '', -150));

?>
--EXPECTF--
-- ASCII string without offset --
int(0)

-- ASCII string with in range positive offset --
int(2)

-- ASCII string with in range negative offset --
int(5)

-- ASCII string with out of bound positive offset --

Warning: mb_stripos(): Offset not contained in string in %s on line %d
bool(false)

-- ASCII string with out of bound negative offset --

Warning: mb_stripos(): Offset not contained in string in %s on line %d
bool(false)

-- Multi-byte string without offset --
int(0)

-- Multi-byte string with in range positive offset --
int(2)

-- Multi-byte string with in range negative offset --
int(19)

-- Multi-byte string with out of bound positive offset --

Warning: mb_stripos(): Offset not contained in string in %s on line %d
bool(false)

-- Multi-byte string with out of bound negative offset --

Warning: mb_stripos(): Offset not contained in string in %s on line %d
bool(false)
37 changes: 37 additions & 0 deletions ext/mbstring/tests/mb_stristr_empty_needle.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
--TEST--
Test mb_stristr() function : with empty needle
--SKIPIF--
<?php
extension_loaded('mbstring') or die('skip');
function_exists('mb_stristr') or die("skip mb_stristr() is not available in this build");
?>
--FILE--
<?php

mb_internal_encoding('UTF-8');

$string_ascii = 'abc def';
// Japanese string in UTF-8
$string_mb = "日本語テキストです。0123456789。";

echo "\n-- ASCII string --\n";
var_dump(mb_stristr($string_ascii, '', false, 'ISO-8859-1'));
var_dump(mb_stristr($string_ascii, ''));
var_dump(mb_stristr($string_ascii, '', true));

echo "\n-- Multibyte string --\n";
var_dump(mb_stristr($string_mb, ''));
var_dump(mb_stristr($string_mb, '', false, 'utf-8'));
var_dump(mb_stristr($string_mb, '', true));

?>
--EXPECT--
-- ASCII string --
string(7) "abc def"
string(7) "abc def"
string(0) ""

-- Multibyte string --
string(53) "日本語テキストです。0123456789。"
string(53) "日本語テキストです。0123456789。"
string(0) ""
86 changes: 86 additions & 0 deletions ext/mbstring/tests/mb_strpos_empty_needle.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
--TEST--
Test mb_strpos() function : with empty needle
--SKIPIF--
<?php
extension_loaded('mbstring') or die('skip');
function_exists('mb_strpos') or die("skip mb_strpos() is not available in this build");
?>
--FILE--
<?php

mb_internal_encoding('UTF-8');

$string_ascii = 'abc def';
// Japanese string in UTF-8
$string_mb = "日本語テキストです。0123456789。";

echo "\n-- ASCII string without offset --\n";
var_dump(mb_strpos($string_ascii, ''));

echo "\n-- ASCII string with in range positive offset --\n";
var_dump(mb_strpos($string_ascii, '', 2));

echo "\n-- ASCII string with in range negative offset --\n";
var_dump(mb_strpos($string_ascii, '', -2));

echo "\n-- ASCII string with out of bound positive offset --\n";
var_dump(mb_strpos($string_ascii, '', 15));

echo "\n-- ASCII string with out of bound negative offset --\n";
var_dump(mb_strpos($string_ascii, '', -15));


echo "\n-- Multi-byte string without offset --\n";
var_dump(mb_strpos($string_mb, ''));

echo "\n-- Multi-byte string with in range positive offset --\n";
var_dump(mb_strpos($string_mb, '', 2));

echo "\n-- Multi-byte string with in range negative offset --\n";
var_dump(mb_strpos($string_mb, '', -2));

echo "\n-- Multi-byte string with out of bound positive offset --\n";
var_dump(mb_strpos($string_mb, '', 150));

echo "\n-- Multi-byte string with out of bound negative offset --\n";
var_dump(mb_strpos($string_mb, '', -150));

?>
--EXPECTF--
-- ASCII string without offset --
int(0)

-- ASCII string with in range positive offset --
int(2)

-- ASCII string with in range negative offset --
int(5)

-- ASCII string with out of bound positive offset --

Warning: mb_strpos(): Offset not contained in string in %s on line %d
bool(false)

-- ASCII string with out of bound negative offset --

Warning: mb_strpos(): Offset not contained in string in %s on line %d
bool(false)

-- Multi-byte string without offset --
int(0)

-- Multi-byte string with in range positive offset --
int(2)

-- Multi-byte string with in range negative offset --
int(19)

-- Multi-byte string with out of bound positive offset --

Warning: mb_strpos(): Offset not contained in string in %s on line %d
bool(false)

-- Multi-byte string with out of bound negative offset --

Warning: mb_strpos(): Offset not contained in string in %s on line %d
bool(false)
37 changes: 37 additions & 0 deletions ext/mbstring/tests/mb_strrchr_empty_needle.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
--TEST--
Test mb_strrchr() function : with empty needle
--SKIPIF--
<?php
extension_loaded('mbstring') or die('skip');
function_exists('mb_strrchr') or die("skip mb_strrchr() is not available in this build");
?>
--FILE--
<?php

mb_internal_encoding('UTF-8');

$string_ascii = 'abc def';
// Japanese string in UTF-8
$string_mb = "日本語テキストです。0123456789。";

echo "\n-- ASCII string --\n";
var_dump(bin2hex(mb_strrchr($string_ascii, '', false, 'ISO-8859-1')));
var_dump(bin2hex(mb_strrchr($string_ascii, '')));
var_dump(bin2hex(mb_strrchr($string_ascii, '', true)));

echo "\n-- Multibyte string --\n";
var_dump(bin2hex(mb_strrchr($string_mb, '')));
var_dump(bin2hex(mb_strrchr($string_mb, '', false, 'utf-8')));
var_dump(bin2hex(mb_strrchr($string_mb, '', true)));

?>
--EXPECT--
-- ASCII string --
string(0) ""
string(0) ""
string(0) ""

-- Multibyte string --
string(0) ""
string(0) ""
string(0) ""
Loading