Skip to content

Get rid of C limitation for shift count in right shift #74002

Description

@serhiy-storchaka
BPO 29816
Nosy @mdickinson, @vstinner, @serhiy-storchaka, @orenmn
PRs
  • bpo-29816: Shift operation now has less opportunity to raise OverflowError. #680
  • Remove outdated note about the constraning of the bit shift right operand. #1258
  • Files
  • patchDraft1.diff: a patch draft for reference only. also handles big positive ints
  • long-shift-overflow-long-long.diff
  • long-shift-overflow-divrem1.diff
  • 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 2017-03-30.07:00:24.059>
    created_at = <Date 2017-03-15.08:55:17.278>
    labels = ['interpreter-core', 'type-feature', '3.7']
    title = 'Get rid of C limitation for shift count in right shift'
    updated_at = <Date 2017-04-22.18:50:11.618>
    user = 'https://github.com/serhiy-storchaka'

    bugs.python.org fields:

    activity = <Date 2017-04-22.18:50:11.618>
    actor = 'serhiy.storchaka'
    assignee = 'none'
    closed = True
    closed_date = <Date 2017-03-30.07:00:24.059>
    closer = 'serhiy.storchaka'
    components = ['Interpreter Core']
    creation = <Date 2017-03-15.08:55:17.278>
    creator = 'serhiy.storchaka'
    dependencies = []
    files = ['46727', '46747', '46748']
    hgrepos = []
    issue_num = 29816
    keywords = ['patch']
    message_count = 19.0
    messages = ['289650', '289651', '289652', '289654', '289658', '289660', '289662', '289692', '289697', '289751', '289767', '289878', '289898', '289984', '290011', '290012', '290824', '290826', '292133']
    nosy_count = 4.0
    nosy_names = ['mark.dickinson', 'vstinner', 'serhiy.storchaka', 'Oren Milman']
    pr_nums = ['680', '1258']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue29816'
    versions = ['Python 3.7']

    Activity

    1. serhiy-storchaka commented on Mar 15, 2017

      @serhiy-storchaka
      MemberAuthor

      Currently the value of right operand of the right shift operator is limited by C Py_ssize_t type.

      >>> 1 >> 10**100
      Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
      OverflowError: Python int too large to convert to C ssize_t
      >>> (-1) >> 10**100
      Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
      OverflowError: Python int too large to convert to C ssize_t
      >>> 1 >> -10**100
      Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
      OverflowError: Python int too large to convert to C ssize_t
      >>> (-1) >> -10**100
      Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
      OverflowError: Python int too large to convert to C ssize_t

      But this is artificial limitation. Right shift can be extended to support arbitrary integers. x >> very_large_value should be 0 for non-negative x and -1 for negative x. x >> negative_value should raise ValueError.

      >>> 1 >> 10
      0
      >>> (-1) >> 10
      -1
      >>> 1 >> -10
      Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
      ValueError: negative shift count
      >>> (-1) >> -10
      Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
      ValueError: negative shift count
    2. vstinner commented on Mar 15, 2017

      @vstinner
      Member

      If we change something, I suggest to be consistent with lshift. I expect a memory error on "1 << (1 << 1024)" (no unlimited loop before a global system collapse please ;-))

    3. vstinner commented on Mar 15, 2017

      @vstinner
      Member

      FYI I saw recently that the C limitation of len() was reported in the "owasp-pysec" project:
      https://github.com/ebranca/owasp-pysec/wiki/Overflow-in-len-function

      I don't understand what such "deliberate" limitation was reported in a hardened CPython project?

    4. serhiy-storchaka commented on Mar 15, 2017

      @serhiy-storchaka
      MemberAuthor

      If we change something, I suggest to be consistent with lshift. I expect a memory error on "1 << (1 << 1024)" (no unlimited loop before a global system collapse please ;-))

      I agree that left shift should raise an ValueError rather than OverflowError for large negative shifts. But is hard to handle large positive shifts. 1 << count consumes count*2/15 bytes of memory. There is a gap between the maximal value of bits represented as Py_ssize_t (PY_SSIZE_T_MAX) and the number of bits of maximal Python int (PY_SSIZE_T_MAX*15/2). _PyLong_NumBits() starves from the same issue. I think an OverflowError is appropriate here for denoting the platform and implementation limitation.

    5. serhiy-storchaka commented on Mar 15, 2017

      @serhiy-storchaka
      MemberAuthor

      This may be a part of this issue or a separate issue: bytes(-1) raises a ValueError, but bytes(-10**100) raises an OverflowError.

    6. vstinner commented on Mar 15, 2017

      @vstinner
      Member

      I think an OverflowError is appropriate here for denoting the platform and implementation limitation.

      It's common that integer overflow on memory allocation in C code raises a MemoryError, not an OverflowError.

      >>> "x" * (2**60)
      Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
      MemoryError

      I suggest to raise a MemoryError.

    7. serhiy-storchaka commented on Mar 15, 2017

      @serhiy-storchaka
      MemberAuthor

      This is not MemoryError. On 32-bit platform 1 << (sys.maxsize + 1) raises an OverflowError, but 1 << sys.maxsize << 1 can be calculated.

    8. serhiy-storchaka commented on Mar 15, 2017

      @serhiy-storchaka
      MemberAuthor

      Unfortunately it is hard to totally avoid OverflowError in right shift. Righ shift of huge positive value can get non-zero result even if shift count is larger than PY_SSIZE_T_MAX. PR 680 just decreases the opportunity of getting a OverflowError.

    9. orenmn commented on Mar 15, 2017

      orenmnmannequin
      Mannequin

      i played a little with a patch earlier today, but stopped because I
      am short on time.

      anyway, just in case my code is not totally rubbish, I attach my
      patch draft, which should avoid OverflowError also for big positive
      ints.

      (of course, I don't suggest to use my code instead of PR 680. I just
      put it here in case it might be useful for someone.)

      (on my Windows 10, it passed some manual tests by me, and the test
      module (except for test_venv, which fails also without the patch))

    10. serhiy-storchaka commented on Mar 17, 2017

      @serhiy-storchaka
      MemberAuthor

      Thank you Oren, but your code doesn't work when PY_SSIZE_T_MAX < b < PY_SSIZE_T_MAX * PyLong_SHIFT and a > 2 ** b. When you drop wordshift and left only loshift_d you should drop lower wordshift digits in a.

      The code for left shift would be even more complex.

    11. serhiy-storchaka commented on Mar 17, 2017

      @serhiy-storchaka
      MemberAuthor

      Updated PR. Now OverflowError is never raised if the result is representable.

      Mark, could you please make a review?

    12. mdickinson commented on Mar 20, 2017

      @mdickinson
      Member

      Mark, could you please make a review?

      I'll try to find time this week. At least in principle, the change sounds good to me.

    13. serhiy-storchaka commented on Mar 20, 2017

      @serhiy-storchaka
      MemberAuthor

      Here are two patches. The first uses C long long arithmetic (it corresponds current PR 680), the second uses PyLong arithmetic. What is easier to read and verify?

    14. mdickinson commented on Mar 22, 2017

      @mdickinson
      Member

      I much prefer the divrem1-based version: it makes fewer assumptions about relative sizes of long / long long / size_t and about the number of bits per digit. I'd rather not have another place that would have to be carefully examined in the future if the number of bits per digit changed again. Overall, Objects/longobject.c is highly portable, and I'd like to keep it that way as much as possible.

    15. serhiy-storchaka commented on Mar 22, 2017

      @serhiy-storchaka
      MemberAuthor

      Updated the PR to divrem1-based version. The drawback is that divrem1 can fail with MemoryError while C long long arithmetic always works for integers of the size less than 1 exbibyte.

    16. serhiy-storchaka commented on Mar 22, 2017

      @serhiy-storchaka
      MemberAuthor

      The special case would be not needed if limit Python ints on 32-bit platforms to approximately 2**2**28. int.bit_length() could be simpler too.

    17. serhiy-storchaka commented on Mar 30, 2017

      @serhiy-storchaka
      MemberAuthor

      New changeset 918403c by Serhiy Storchaka in branch 'master':
      bpo-29816: Shift operation now has less opportunity to raise OverflowError. (#680)
      918403c

    18. serhiy-storchaka commented on Mar 30, 2017

      @serhiy-storchaka
      MemberAuthor

      Thank you for your review Mark.

    19. serhiy-storchaka commented on Apr 22, 2017

      @serhiy-storchaka
      MemberAuthor

      New changeset 997a4ad by Serhiy Storchaka in branch 'master':
      Remove outdated note about constraining of the bit shift right operand. (bpo-1258)
      997a4ad

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

      3.7 (EOL)end of lifeinterpreter-core(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancement

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions