Skip to content

crash in str.rfind() with an invalid start value #51707

Closed

Description

@vstinner
BPO 7458
Nosy @pitrou, @vstinner, @florentx
Files
  • str_find-4.patch
  • issue7458_rfind.diff: Patch, apply to trunk
  • 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 = None
    closed_at = <Date 2010-01-02.21:55:59.397>
    created_at = <Date 2009-12-08.10:21:09.224>
    labels = ['interpreter-core']
    title = 'crash in str.rfind() with an invalid start value'
    updated_at = <Date 2010-01-10.03:08:18.855>
    user = 'https://github.com/vstinner'

    bugs.python.org fields:

    activity = <Date 2010-01-10.03:08:18.855>
    actor = 'vstinner'
    assignee = 'none'
    closed = True
    closed_date = <Date 2010-01-02.21:55:59.397>
    closer = 'pitrou'
    components = ['Interpreter Core']
    creation = <Date 2009-12-08.10:21:09.224>
    creator = 'vstinner'
    dependencies = []
    files = ['15497', '15501']
    hgrepos = []
    issue_num = 7458
    keywords = ['patch']
    message_count = 15.0
    messages = ['96117', '96118', '96119', '96120', '96122', '96123', '96124', '96128', '96132', '96133', '96143', '96148', '96553', '97150', '97496']
    nosy_count = 3.0
    nosy_names = ['pitrou', 'vstinner', 'flox']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = None
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue7458'
    versions = ['Python 2.6', 'Python 3.1', 'Python 2.7', 'Python 3.2']

    Activity

    1. vstinner commented on Dec 8, 2009

      @vstinner
      MemberAuthor

      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.

    2. added
      interpreter-core
      on Dec 8, 2009
    3. vstinner commented on Dec 8, 2009

      @vstinner
      MemberAuthor

      In my test, start=6287518193 is an arbitrary value, it may crash or
      not. The test might use any random integer > 0.

    4. vstinner commented on Dec 8, 2009

      @vstinner
      MemberAuthor

      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.

    5. florentx commented on Dec 8, 2009

      florentxmannequin
      Mannequin

      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)

    6. vstinner commented on Dec 8, 2009

      @vstinner
      MemberAuthor

      This bug does not occur on Debian 64 bits.

      It does, sometime :-) Read uninitiliazed memory doesn't always crash.

         $ python -c "'ab'.rfind('xxx', (1 << 63) + 10, 0)"
         Erreur de segmentation

      Note: my 64 bits test in CRASH_rfind.py is wrong, ctypes.c_long should
      be used instead of ctypes.c_int.

    7. vstinner commented on Dec 8, 2009

      @vstinner
      MemberAuthor

      New patch with a more stable test. test_unicode does also fail (error
      or crash) without the patch on find.h.

    8. florentx commented on Dec 8, 2009

      florentxmannequin
      Mannequin

      I got it to crash (2.7):

      ~ $ ./python Lib/test/regrtest.py string_tests test_unicode test_str
      test_unicode
      test test_unicode failed -- Traceback (most recent call last):
      AssertionError: -1 != -8276732
      
      test_str
      test test_str failed -- Traceback (most recent call last):
      AssertionError: -1 != -255833

      2 tests failed:
      test_unicode test_str
      [56425 refs]
      ~ $

      Thank you haypo :)

    9. pitrou commented on Dec 8, 2009

      @pitrou
      Member

      You shouldn't harcode 1 << 63 and 1 << 64, but calculate them based on
      sys.maxsize instead.

    10. vstinner commented on Dec 8, 2009

      @vstinner
      MemberAuthor

      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 :-)

    11. vstinner commented on Dec 8, 2009

      @vstinner
      MemberAuthor

      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).

    12. florentx commented on Dec 8, 2009

      florentxmannequin
      Mannequin

      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.

    13. vstinner commented on Dec 8, 2009

      @vstinner
      MemberAuthor

      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.

    14. 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
    15. florentx commented on Dec 18, 2009

      florentxmannequin
      Mannequin

      I proposed a patch which solve this issue and improve performance of
      str.rfind. See bpo-7462.

    16. pitrou commented on Jan 2, 2010

      @pitrou
      Member

      Ok, I've committed the tests after the patch for bpo-7462 removed the offending code. Thanks!

    17. vstinner commented on Jan 10, 2010

      @vstinner
      MemberAuthor

      Ok, I've committed the tests after the patch for bpo-7462 removed the offending code. Thanks!

      r77241 and r77247 in trunk

    18. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      interpreter-core

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions