diff options
| author | Scott Shawcroft <scott@tannewt.org> | 2018-04-01 22:41:59 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-04-01 22:41:59 -0700 |
| commit | e4ae1e3d59f71200a01cbf8cf2128939f181863f (patch) | |
| tree | 813ffd718011a1ae6030909a46c79a0616298a61 | |
| parent | b62d8faaec57899abff13cb70a7e0007cba4479c (diff) | |
| parent | 0041df0c6b493458000d7e815e094995cfbc8043 (diff) | |
Merge pull request #734 from jepler/str-find-backwards-circuitpython
py/objstr: Don't crash when end < start
| -rw-r--r-- | py/objstr.c | 5 | ||||
| -rw-r--r-- | tests/basics/string_find.py | 1 | ||||
| -rw-r--r-- | tests/basics/string_rfind.py | 1 |
3 files changed, 7 insertions, 0 deletions
diff --git a/py/objstr.c b/py/objstr.c index 64306f54f..7ace1cf73 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -692,8 +692,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); } + if (end < start) { + goto out_error; + } + const byte *p = find_subbytes(start, end - start, needle, needle_len, direction); if (p == NULL) { + out_error: // not found if (is_index) { mp_raise_ValueError("substring not found"); diff --git a/tests/basics/string_find.py b/tests/basics/string_find.py index 4a206eb0e..f9fcad3e5 100644 --- a/tests/basics/string_find.py +++ b/tests/basics/string_find.py @@ -21,6 +21,7 @@ print("0000".find('-1', 3)) print("0000".find('1', 3)) print("0000".find('1', 4)) print("0000".find('1', 5)) +print("aaaaaaaaaaa".find("bbb", 9, 2)) try: 'abc'.find(1) diff --git a/tests/basics/string_rfind.py b/tests/basics/string_rfind.py index 4d0e84018..54269d6f5 100644 --- a/tests/basics/string_rfind.py +++ b/tests/basics/string_rfind.py @@ -21,3 +21,4 @@ print("0000".rfind('-1', 3)) print("0000".rfind('1', 3)) print("0000".rfind('1', 4)) print("0000".rfind('1', 5)) +print("aaaaaaaaaaa".rfind("bbb", 9, 2)) |
