py/objstr: In find/rfind, don't crash when end < start.
This commit is contained in:
parent
b9c78425a6
commit
d6cf5c6749
@ -699,8 +699,13 @@ STATIC mp_obj_t str_finder(size_t n_args, const mp_obj_t *args, int direction, b
|
|||||||
end = str_index_to_ptr(self_type, haystack, haystack_len, args[3], true);
|
end = str_index_to_ptr(self_type, haystack, haystack_len, args[3], true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (end < start) {
|
||||||
|
goto out_error;
|
||||||
|
}
|
||||||
|
|
||||||
const byte *p = find_subbytes(start, end - start, needle, needle_len, direction);
|
const byte *p = find_subbytes(start, end - start, needle, needle_len, direction);
|
||||||
if (p == NULL) {
|
if (p == NULL) {
|
||||||
|
out_error:
|
||||||
// not found
|
// not found
|
||||||
if (is_index) {
|
if (is_index) {
|
||||||
mp_raise_ValueError("substring not found");
|
mp_raise_ValueError("substring not found");
|
||||||
|
@ -21,6 +21,7 @@ print("0000".find('-1', 3))
|
|||||||
print("0000".find('1', 3))
|
print("0000".find('1', 3))
|
||||||
print("0000".find('1', 4))
|
print("0000".find('1', 4))
|
||||||
print("0000".find('1', 5))
|
print("0000".find('1', 5))
|
||||||
|
print("aaaaaaaaaaa".find("bbb", 9, 2))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
'abc'.find(1)
|
'abc'.find(1)
|
||||||
|
@ -21,3 +21,4 @@ print("0000".rfind('-1', 3))
|
|||||||
print("0000".rfind('1', 3))
|
print("0000".rfind('1', 3))
|
||||||
print("0000".rfind('1', 4))
|
print("0000".rfind('1', 4))
|
||||||
print("0000".rfind('1', 5))
|
print("0000".rfind('1', 5))
|
||||||
|
print("aaaaaaaaaaa".rfind("bbb", 9, 2))
|
||||||
|
Loading…
Reference in New Issue
Block a user