[New-bugs-announce] [issue31984] startswith and endswith leak implementation details

Ronan Lamy report at bugs.python.org
Wed Nov 8 11:59:43 EST 2017


New submission from Ronan Lamy <ronan.lamy at gmail.com>:

One would think that u.startswith(v, start, end) would be equivalent to u[start: end].startswith(v), but one would be wrong. And the same goes for endswith(). Here is the actual spec (for bytes, but str and bytearray are the same), in the form of passing pytest+hypothesis tests:


from hypothesis import strategies as st, given

def adjust_indices(u, start, end):
    if end < 0:
        end = max(end + len(u), 0)
    else:
        end = min(end, len(u))
    if start < 0:
        start = max(start + len(u), 0)
    return start, end

@given(st.binary(), st.binary(), st.integers(), st.integers())
def test_startswith_3(u, v, start, end):
    if v:
        expected = u[start:end].startswith(v)
    else:
        start0, end0 = adjust_indices(u, start, end)
        expected = start0 <= len(u) and start0 <= end0
    assert u.startswith(v, start, end) is expected

@given(st.binary(), st.binary(), st.integers(), st.integers())
def test_endswith_3(u, v, start, end):
    if v:
        expected = u[start:end].endswith(v)
    else:
        start0, end0 = adjust_indices(u, start, end)
        expected = start0 <= len(u) and start0 <= end0
    assert u.endswith(v, start, end) is expected

Fixing this behaviour to work in the "obvious" way would be simple: just add a check for len(v) == 0 and always return True in that case.

----------
messages: 305881
nosy: Ronan.Lamy
priority: normal
severity: normal
status: open
title: startswith and endswith leak implementation details
type: behavior
versions: Python 3.7

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue31984>
_______________________________________


More information about the New-bugs-announce mailing list