Skip to content

Commit 290e9f1

Browse files
committed
Logic fix for reverse case and move mbfl_strlen call to only be done when needle length is 0
1 parent b4c3f25 commit 290e9f1

File tree

3 files changed

+20
-21
lines changed

3 files changed

+20
-21
lines changed

ext/mbstring/libmbfl/mbfl/mbfilter.c

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -817,7 +817,6 @@ mbfl_strpos(
817817
int reverse)
818818
{
819819
size_t result;
820-
size_t haystack_length;
821820
mbfl_string _haystack_u8, _needle_u8;
822821
const mbfl_string *haystack_u8, *needle_u8 = NULL;
823822
const unsigned char *u8_tbl;
@@ -856,27 +855,27 @@ mbfl_strpos(
856855
needle_u8 = needle;
857856
}
858857

859-
/* Check if offset is out of bound */
860-
haystack_length = mbfl_strlen(haystack_u8);
861-
if (
862-
(offset > 0 && offset > haystack_length)
863-
|| (offset < 0 && -offset > haystack_length)
864-
) {
865-
result = -16;
866-
goto out;
867-
}
868-
869858
result = (size_t) -1;
870859
if (haystack_u8->len < needle_u8->len) {
871860
goto out;
872861
}
873862

874863
if (needle_u8->len == 0) {
864+
size_t haystack_length = mbfl_strlen(haystack_u8);
865+
/* Check if offset is out of bound */
866+
if (
867+
(offset > 0 && offset > haystack_length)
868+
|| (offset < 0 && -offset > haystack_length)
869+
) {
870+
result = -16;
871+
goto out;
872+
}
873+
875874
if (reverse) {
876875
if (offset < 0) {
877-
result = (size_t) -offset;
876+
result = haystack_length + offset;
878877
} else {
879-
result = haystack_length - offset;;
878+
result = haystack_length;
880879
}
881880
} else {
882881
if (offset < 0) {

ext/mbstring/tests/mb_strripos_empty_needle.phpt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ var_dump(mb_strripos($string_mb, '', -150));
5151
int(7)
5252

5353
-- ASCII string with in range positive offset --
54-
int(5)
54+
int(7)
5555

5656
-- ASCII string with in range negative offset --
57-
int(2)
57+
int(5)
5858

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

@@ -70,10 +70,10 @@ bool(false)
7070
int(21)
7171

7272
-- Multi-byte string with in range positive offset --
73-
int(19)
73+
int(21)
7474

7575
-- Multi-byte string with in range negative offset --
76-
int(2)
76+
int(19)
7777

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

ext/mbstring/tests/mb_strrpos_empty_needle.phpt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ var_dump(mb_strrpos($string_mb, '', -150));
5151
int(7)
5252

5353
-- ASCII string with in range positive offset --
54-
int(5)
54+
int(7)
5555

5656
-- ASCII string with in range negative offset --
57-
int(2)
57+
int(5)
5858

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

@@ -70,10 +70,10 @@ bool(false)
7070
int(21)
7171

7272
-- Multi-byte string with in range positive offset --
73-
int(19)
73+
int(21)
7474

7575
-- Multi-byte string with in range negative offset --
76-
int(2)
76+
int(19)
7777

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

0 commit comments

Comments
 (0)