Skip to content

Commit d6cf5c6

Browse files
jeplerdpgeorge
authored andcommitted
py/objstr: In find/rfind, don't crash when end < start.
1 parent b9c7842 commit d6cf5c6

3 files changed

Lines changed: 7 additions & 0 deletions

File tree

py/objstr.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,8 +699,13 @@ STATIC mp_obj_t str_finder(size_t n_args, const mp_obj_t *args, int direction, b
699699
end = str_index_to_ptr(self_type, haystack, haystack_len, args[3], true);
700700
}
701701

702+
if (end < start) {
703+
goto out_error;
704+
}
705+
702706
const byte *p = find_subbytes(start, end - start, needle, needle_len, direction);
703707
if (p == NULL) {
708+
out_error:
704709
// not found
705710
if (is_index) {
706711
mp_raise_ValueError("substring not found");

tests/basics/string_find.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
print("0000".find('1', 3))
2222
print("0000".find('1', 4))
2323
print("0000".find('1', 5))
24+
print("aaaaaaaaaaa".find("bbb", 9, 2))
2425

2526
try:
2627
'abc'.find(1)

tests/basics/string_rfind.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@
2121
print("0000".rfind('1', 3))
2222
print("0000".rfind('1', 4))
2323
print("0000".rfind('1', 5))
24+
print("aaaaaaaaaaa".rfind("bbb", 9, 2))

0 commit comments

Comments
 (0)