Skip to content

Commit 0041df0

Browse files
committed
py/objstr: Don't crash when end < start
.. and add testcases for the same. (crash found by afl-fuzz)
1 parent 3215b85 commit 0041df0

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
@@ -692,8 +692,13 @@ STATIC mp_obj_t str_finder(size_t n_args, const mp_obj_t *args, int direction, b
692692
end = str_index_to_ptr(self_type, haystack, haystack_len, args[3], true);
693693
}
694694

695+
if (end < start) {
696+
goto out_error;
697+
}
698+
695699
const byte *p = find_subbytes(start, end - start, needle, needle_len, direction);
696700
if (p == NULL) {
701+
out_error:
697702
// not found
698703
if (is_index) {
699704
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)