Skip to content

bytearray().count() #48217

Closed
Closed

Description

@vstinner
BPO 3967
Nosy @amauryfa, @vstinner, @benjaminp
Files
  • string_count.patch
  • 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 2008-09-26.22:49:58.295>
    created_at = <Date 2008-09-25.21:39:32.363>
    labels = ['interpreter-core', 'type-crash', 'release-blocker']
    title = 'bytearray().count()'
    updated_at = <Date 2008-09-26.22:49:58.293>
    user = 'https://github.com/vstinner'

    bugs.python.org fields:

    activity = <Date 2008-09-26.22:49:58.293>
    actor = 'amaury.forgeotdarc'
    assignee = 'none'
    closed = True
    closed_date = <Date 2008-09-26.22:49:58.295>
    closer = 'amaury.forgeotdarc'
    components = ['Interpreter Core']
    creation = <Date 2008-09-25.21:39:32.363>
    creator = 'vstinner'
    dependencies = []
    files = ['11606']
    hgrepos = []
    issue_num = 3967
    keywords = ['patch', 'needs review']
    message_count = 8.0
    messages = ['73817', '73820', '73821', '73828', '73829', '73898', '73902', '73903']
    nosy_count = 3.0
    nosy_names = ['amaury.forgeotdarc', 'vstinner', 'benjamin.peterson']
    pr_nums = []
    priority = 'release blocker'
    resolution = 'fixed'
    stage = None
    status = 'closed'
    superseder = None
    type = 'crash'
    url = 'https://bugs.python.org/issue3967'
    versions = ['Python 2.6', 'Python 2.5', 'Python 3.0']

    Activity

    1. vstinner commented on Sep 25, 2008

      @vstinner
      MemberAuthor

      bytes_count() doesn't check start maximum value: _adjust_indices()
      should check that start is smaller than len (smaller or egal? len or
      len-1?). Example:

      >> b = bytearray(3)
      >> b.count("x", 1491491034, 0)

      start=1491491034 should be replaced by 3 (or 2 or 4? I don't know
      bytearray enough).
    2. added
      interpreter-core
      type-crash
      on Sep 25, 2008
    3. amauryfa commented on Sep 25, 2008

      @amauryfa
      Contributor

      Is there a problem, a potential crash?
      looking at the code, I could not find any: in fastsearch, the "if (w <
      0)" will quickly exit the function, without reading the contents of the
      array.

    4. vstinner commented on Sep 25, 2008

      @vstinner
      MemberAuthor

      Here is a trace of Valgrind:

       >>> b=bytearray(2)
       >>> b.count("xxxx", 3493403, 0)
      0
       >>> b.count("xxxx", 23131230123012010231023, 0)
      ==13650== Invalid read of size 1
      ==13650==    at 0x812718A: fastsearch (fastsearch.h:67)
      ==13650==    by 0x81272CB: stringlib_count (count.h:22)
      ==13650==    by 0x8128611: bytes_count (bytearrayobject.c:1198)
      ==13650==    by 0x81376B6: PyCFunction_Call (methodobject.c:81)
      ==13650==    by 0x80D913D: call_function (ceval.c:3679)
      ==13650==    by 0x80D603A: PyEval_EvalFrameEx (ceval.c:2370)
      ==13650==    by 0x80D7891: PyEval_EvalCodeEx (ceval.c:2942)
      ==13650==    by 0x80D0A8F: PyEval_EvalCode (ceval.c:515)
      ==13650==    by 0x80FB4FE: run_mod (pythonrun.c:1330)
      ==13650==    by 0x80FA166: PyRun_InteractiveOneFlags (pythonrun.c:836)
      ==13650==    by 0x80F9EEA: PyRun_InteractiveLoopFlags (pythonrun.c:756)
      ==13650==    by 0x80F9DAC: PyRun_AnyFileExFlags (pythonrun.c:725)
      ==13650==  Address 0x8444553a is not stack'd, malloc'd or (recently) free'd
      ==13650==
      ==13650== Process terminating with default action of signal 11 (SIGSEGV)

      Trace of gdb:
      0x0812718a in fastsearch (s=0x89b9fcaf <Address 0x89b9fcaf out of
      bounds>, n=-2147483647,
      p=0xb7b54274 "xxxx", m=4, mode=0) at Objects/stringlib/fastsearch.h:67
      67 if (s[i+m-1] == p[m-1]) {
      (gdb) where
      #0 0x0812718a in fastsearch (s=0x89b9fcaf <Address 0x89b9fcaf out of
      bounds>, n=-2147483647,
      p=0xb7b54274 "xxxx", m=4, mode=0) at Objects/stringlib/fastsearch.h:67
      #1 0x081272cc in stringlib_count (str=0x89b9fcaf <Address 0x89b9fcaf
      out of bounds>,
      str_len=-2147483647, sub=0xb7b54274 "xxxx", sub_len=4) at
      Objects/stringlib/count.h:22
      #2 0x08128612 in bytes_count (self=0xb7d025f0, args=0xb7b5075c)
      at Objects/bytearrayobject.c:1198
      (gdb) print w
      $1 = 2147483645
      (gdb) print m
      $2 = 4

      Oh, look at stringlib_count (..., str_len=-2147483647, ..., sub_len=4):
      str_len is negative.

    5. amauryfa commented on Sep 26, 2008

      @amauryfa
      Contributor

      Ah, big numbers that overflow: even if 'start' is (silently) capped to
      sys.maxint-1, len(b)-start-len("xx") will wrap and yield a positive
      number...

      The find/rfind/index/rindex methods have the same problem.

      Attached a patch and unit tests, needs review.

    6. amauryfa commented on Sep 26, 2008

      @amauryfa
      Contributor

      All versions are involved.

      Even 2.4 has surprises:
      >>> "aa".count("", sys.maxint, 0)
      -2147483646
    7. benjaminp commented on Sep 26, 2008

      @benjaminp
      Contributor

      I think the patch is good.

    8. vstinner commented on Sep 26, 2008

      @vstinner
      MemberAuthor

      Fixed in Python trunk, rev66631, by amaury.forgeotdarc.

    9. amauryfa commented on Sep 26, 2008

      @amauryfa
      Contributor

      Committed in trunk: r66631, in release25-maint: r66632 and py3k: r66633.

    10. 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-corerelease-blockertype-crash

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions