Skip to content

obj2ast's error handling can lead to python crashing with a C-level assertion failure #54600

Closed

Description

@davidmalcolm
BPO 10391
Nosy @brettcannon, @birkenfeld, @amauryfa, @ncoghlan, @benjaminp, @davidmalcolm
Files
  • py3k-issue-10391-fix-ast-error-handling.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 2010-11-20.01:39:03.489>
    created_at = <Date 2010-11-11.20:53:42.187>
    labels = ['interpreter-core', 'type-crash']
    title = "obj2ast's error handling can lead to python crashing with a C-level assertion failure"
    updated_at = <Date 2010-11-20.01:39:03.487>
    user = 'https://github.com/davidmalcolm'

    bugs.python.org fields:

    activity = <Date 2010-11-20.01:39:03.487>
    actor = 'benjamin.peterson'
    assignee = 'none'
    closed = True
    closed_date = <Date 2010-11-20.01:39:03.489>
    closer = 'benjamin.peterson'
    components = ['Interpreter Core']
    creation = <Date 2010-11-11.20:53:42.187>
    creator = 'dmalcolm'
    dependencies = []
    files = ['19572']
    hgrepos = []
    issue_num = 10391
    keywords = ['patch']
    message_count = 4.0
    messages = ['120967', '120968', '120971', '121588']
    nosy_count = 6.0
    nosy_names = ['brett.cannon', 'georg.brandl', 'amaury.forgeotdarc', 'ncoghlan', 'benjamin.peterson', 'dmalcolm']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'patch review'
    status = 'closed'
    superseder = None
    type = 'crash'
    url = 'https://bugs.python.org/issue10391'
    versions = ['Python 3.1', 'Python 3.2', 'Python 3.3']

    Activity

    1. davidmalcolm commented on Nov 11, 2010

      @davidmalcolm
      MemberAuthor

      In various places within the generated Python/Python-ast.c, error handling generates a repr() and raises exceptions accordingly.

      Currently in py3k the generated code uses PyBytes_AS_STRING() on the repr. My understanding is that repr() should be a PyUnicodeObject, not a PyBytesObject. This seems to be unchanged from r63682, which was a mass-change of PyString to PyBytes from 2 years ago.

      This leads to a python crashing with an assertion failure:
      test_compile_ast (main.TestSpecifics) ... python: Python/Python-ast.c:5835: obj2ast_expr: Assertion `((((((PyObject*)(tmp))->ob_type))->tp_flags & ((1L<<27))) != 0)' failed.

      when invoking compile() on certain (malformed) trees of ast objects.

    2. added
      interpreter-core(Objects, Python, Grammar, and Parser dirs)
      type-crashA hard crash of the interpreter, possibly with a core dump
      on Nov 11, 2010
    3. davidmalcolm commented on Nov 11, 2010

      @davidmalcolm
      MemberAuthor

      The attached patch:

      • extends the ast error-handling selftest with code that triggers this crash (on unpatched code)
      • fixes Parser/asdl_c.py to generate code using _PyUnicode_AS_STRING instead
      • contains the generated changes to Python/Python-ast.c

      FWIW, it's not clear to what extent _PyUnicode_AS_STRING is deprecated, Include/unicodeobject.h currently has:
      *** This API is for interpreter INTERNAL USE ONLY and will likely
      *** be removed or changed for Python 3.1.
      but I hope it won't be, it's far too useful.

    4. amauryfa commented on Nov 11, 2010

      @amauryfa
      Contributor
      IMO a better patch would be to use the %R format specification:
        PyErr_Format(
            PyExc_TypeError, "expected some sort of mod, but got %R", obj);
      And no need to use PyObject_Repr and the temporary variable!
    5. benjaminp commented on Nov 20, 2010

      @benjaminp
      Contributor

      r86538

    6. 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(Objects, Python, Grammar, and Parser dirs)type-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