Skip to content

Calling "functions" used to implement generators/comps easily cause crash #81137

Closed

Description

@mr-nfamous
BPO 36956
Nosy @rhettinger, @mr-nfamous, @websurfer5, @iritkatriel

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 = None
created_at = <Date 2019-05-18.13:53:21.143>
labels = ['3.10', '3.9', 'type-crash', '3.11']
title = 'Calling "functions" used to implement generators/comps easily cause crash'
updated_at = <Date 2021-09-19.21:54:42.882>
user = 'https://github.com/mr-nfamous'

bugs.python.org fields:

activity = <Date 2021-09-19.21:54:42.882>
actor = 'iritkatriel'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = []
creation = <Date 2019-05-18.13:53:21.143>
creator = 'bup'
dependencies = []
files = []
hgrepos = []
issue_num = 36956
keywords = []
message_count = 5.0
messages = ['342797', '346807', '346808', '346809', '402171']
nosy_count = 4.0
nosy_names = ['rhettinger', 'bup', 'Jeffrey.Kintscher', 'iritkatriel']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'crash'
url = 'https://bugs.python.org/issue36956'
versions = ['Python 3.9', 'Python 3.10', 'Python 3.11']

Linked PRs

Activity

  1. mr-nfamous commented on May 18, 2019

    mr-nfamousmannequin
    MannequinAuthor

    As far as I know, generators, set comprehensions, list comprehensions, and dict comprehensions, (along with their asynchronous variants) are implemented by first calling the GET_(A)ITER opcode and then building and calling a function that acepts the resulting iterator as its sole argument.

    Assigning the code object used to make that function (or using it in the types.FunctionType constructor) and then calling it with a non-iterator argument will obviously cause a crash since the FOR_ITER opcode rightly expects that it will never have to deal with non-iterators and calls tp_iternext without checking if it exists.

    The 4-liner demonstrates the crash:

    if 1:
      fn = lambda: None
      gi = (i for i in ())
      fn.__code__ = gi.gi_code
      [*fn("abc")]
  2. added
    type-crashA hard crash of the interpreter, possibly with a core dump
    on May 18, 2019
  3. websurfer5 commented on Jun 28, 2019

    websurfer5mannequin
    Mannequin

    What is the preferred behavior? Raise an exception of some kind?

  4. websurfer5 commented on Jun 28, 2019

    websurfer5mannequin
    Mannequin

    Specifically, the crash occurs in Python/ceval.c function _PyEval_EvalFrameDefault() in the TARGET(FOR_ITER) case at line 3198 (master branch):

    PyObject *next = (*iter->ob_type->tp_iternext)(iter);

    It segfaults because tp_iternext is NULL.

  5. rhettinger commented on Jun 28, 2019

    @rhettinger
    Contributor

    ISTM this report is close to that covered by the traditional stance that word code hacks aren't protected from crashes because trying to do can be complicated, can be expensive, and isn't always possible.

    I'm not really sure where this falls on the grey scale with "normal, everyday python shouldn't crash" on one end and "with ctypes or code hacks, all bets are off" on the other end.

    Offhand, the posted snippet seems closer to "messing with the internals" but perhaps small cheap check can be added when the __code__ variable is updated.

  6. iritkatriel commented on Sep 19, 2021

    @iritkatriel
    Member

    Reproduced on 3.11.

  7. added
    3.11only security fixes
    and removed on Sep 19, 2021
  8. transferred this issue fromon Apr 10, 2022
  9. added
    3.13only security fixes
    and removed
    3.11only security fixes
    on Nov 7, 2023
  10. added a commit that references this issue on Nov 7, 2023
  11. added a commit that references this issue on Nov 7, 2023
  12. added a commit that references this issue on Nov 8, 2023
  13. added a commit that references this issue on Feb 11, 2024
  14. added a commit that references this issue on Sep 2, 2024
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.13only security fixestype-crashA hard crash of the interpreter, possibly with a core dump

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions