You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
crash in str.rfind() with an invalid start value #51707
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=<Date2010-01-02.21:55:59.397>created_at=<Date2009-12-08.10:21:09.224>labels= ['interpreter-core']
title='crash in str.rfind() with an invalid start value'updated_at=<Date2010-01-10.03:08:18.855>user='https://github.com/vstinner'
changed the title [-]crash in str.find() and str.rfind() with invalid start value[/-][+]crash in str.rfind() with an invalid start value[/+]on Dec 8, 2009
florentx commented on Dec 18, 2009
florentxmannequin
Mannequin
More actions
I proposed a patch which solve this issue and improve performance of
str.rfind. See bpo-7462.
Activity
vstinner commentedon Dec 8, 2009
str.find() and str.rfind() reads non initialized memory (using
memcmp()) if start is bigger than end.
Attached patch fixes the issue and includes a patch.
vstinner commentedon Dec 8, 2009
In my test, start=6287518193 is an arbitrary value, it may crash or
not. The test might use any random integer > 0.
vstinner commentedon Dec 8, 2009
The bug was introduced in Python 2.5 during the needforspeed sprint:
r46469 (May 27 2006).
http://wiki.python.org/moin/NeedForSpeed
Python < 2.5 is not affected, Python 3.x is affected.
CRASH_rfind.py is more stable and should always crash if your Python
version has the bug.
florentx commentedon Dec 8, 2009
This bug does not occur on Debian 64 bits.
~ $ uname -srvm
Linux 2.6.30-bpo.1-amd64 #1 SMP Mon Aug 17 08:42:50 UTC 2009 x86_64
Tested with variants:
from random import getrandbits
self.checkequal(-1, 'ab', 'find', 'xxx', getrandbits(64), 0)
self.checkequal(-1, 'ab', 'find', 'xxx', getrandbits(96), 0)
self.checkequal(-1, 'ab', 'rfind', 'xxx', getrandbits(64), 0)
self.checkequal(-1, 'ab', 'rfind', 'xxx', getrandbits(96), 0)
vstinner commentedon Dec 8, 2009
It does, sometime :-) Read uninitiliazed memory doesn't always crash.
$ python -c "'ab'.rfind('xxx', (1 << 63) + 10, 0)" Erreur de segmentationNote: my 64 bits test in CRASH_rfind.py is wrong, ctypes.c_long should
be used instead of ctypes.c_int.
vstinner commentedon Dec 8, 2009
New patch with a more stable test. test_unicode does also fail (error
or crash) without the patch on find.h.
florentx commentedon Dec 8, 2009
I got it to crash (2.7):
2 tests failed:
test_unicode test_str
[56425 refs]
~ $
Thank you haypo :)
pitrou commentedon Dec 8, 2009
You shouldn't harcode 1 << 63 and 1 << 64, but calculate them based on
sys.maxsize instead.
vstinner commentedon Dec 8, 2009
pitrou> You shouldn't harcode 1 << 63 and 1 << 64, but calculate
pitrou> them based on sys.maxsize instead.
(sys.maxint) Yes, you're right. Does str_find-3.patch looks better?
It's not easy to always detect an Heisenbug :-)
vstinner commentedon Dec 8, 2009
sys.maxint/sys.maxsize: oops, sys.maxsize *does* exist (in Python >=
2.6), sorry. Here is a new patch using sys.maxsize.
Anyway, sys.maxsize sounds better because the integer overflow occurs
in a Py_ssize_t variable (j).
florentx commentedon Dec 8, 2009
I reviewed the patch, and it seems partially redundant.
Actually the "find" method was not broken.
There is already a test "if (str_len < 0) return -1;" for this one.
See attached patch.
vstinner commentedon Dec 8, 2009
flox> Actually the "find" method was not broken.
Oh, you're right, str.find() was already fixed by r66631 (related to
issue bpo-3967). I prefer your patch flox ;-)
I fixed this issue title.
[-]crash in str.find() and str.rfind() with invalid start value[/-][+]crash in str.rfind() with an invalid start value[/+]florentx commentedon Dec 18, 2009
I proposed a patch which solve this issue and improve performance of
str.rfind. See bpo-7462.
pitrou commentedon Jan 2, 2010
Ok, I've committed the tests after the patch for bpo-7462 removed the offending code. Thanks!
vstinner commentedon Jan 10, 2010
r77241 and r77247 in trunk