Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect use of PyObject_IsInstance #68445

Closed
serhiy-storchaka opened this issue May 21, 2015 · 6 comments
Closed

Incorrect use of PyObject_IsInstance #68445

serhiy-storchaka opened this issue May 21, 2015 · 6 comments
Assignees
Labels
extension-modules C modules in the Modules dir interpreter-core (Objects, Python, Grammar, and Parser dirs) type-crash A hard crash of the interpreter, possibly with a core dump

Comments

@serhiy-storchaka
Copy link
Member

BPO 24257
Nosy @rhettinger, @bitdancer, @serhiy-storchaka
Files
  • typecheck.patch
  • typecheck_2.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 = 'https://github.com/serhiy-storchaka'
    closed_at = <Date 2015-05-22.08:24:25.805>
    created_at = <Date 2015-05-21.08:40:46.663>
    labels = ['extension-modules', 'interpreter-core', 'type-crash']
    title = 'Incorrect use of PyObject_IsInstance'
    updated_at = <Date 2015-05-22.08:24:25.804>
    user = 'https://github.com/serhiy-storchaka'

    bugs.python.org fields:

    activity = <Date 2015-05-22.08:24:25.804>
    actor = 'serhiy.storchaka'
    assignee = 'serhiy.storchaka'
    closed = True
    closed_date = <Date 2015-05-22.08:24:25.805>
    closer = 'serhiy.storchaka'
    components = ['Extension Modules', 'Interpreter Core']
    creation = <Date 2015-05-21.08:40:46.663>
    creator = 'serhiy.storchaka'
    dependencies = []
    files = ['39450', '39453']
    hgrepos = []
    issue_num = 24257
    keywords = ['patch']
    message_count = 6.0
    messages = ['243739', '243752', '243754', '243756', '243797', '243820']
    nosy_count = 4.0
    nosy_names = ['rhettinger', 'r.david.murray', 'python-dev', 'serhiy.storchaka']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'crash'
    url = 'https://bugs.python.org/issue24257'
    versions = ['Python 2.7', 'Python 3.4', 'Python 3.5']

    @serhiy-storchaka
    Copy link
    Member Author

    PyObject_IsInstance() is used incorrectly for testing if Python object is an instance of specified builtin type before direct access to internals of object. This is not correct, because PyObject_IsInstance() checks the __class__ attribute that can be modified and even can be dynamic property. Correct way is to check static type. Proposed patch replaces PyObject_IsInstance() with PyObject_TypeCheck() if this is appropriate.

    See also similar issues bpo-24102 and bpo-24091.

    @serhiy-storchaka serhiy-storchaka added extension-modules C modules in the Modules dir interpreter-core (Objects, Python, Grammar, and Parser dirs) type-crash A hard crash of the interpreter, possibly with a core dump labels May 21, 2015
    @bitdancer
    Copy link
    Member

    Is there any chance that these changes will break working code, or is it the case that if the current check passes incorrectly one will always get a segfauilt or other error?

    @rhettinger
    Copy link
    Contributor

    is it the case that if the current check passes incorrectly
    one will always get a segfauilt or other error?

    Yes, that is the case. All four of these checks precede a reference to an structure member that depends on being an exact type or subtype. So, yes they are all necessary to prevent segfaults or other undefined behavior.

    @serhiy-storchaka
    Copy link
    Member Author

    Yes, it is the case that if the current check passes incorrectly one will always get a segfauilt or other error.

    Added tests for types.SimpleNamespace and sqlite3.Cursor. It is not easy to reproduce a bug for StopIterator (not sure it is reproducible), but the code looks definitely erroneous in any case.

    @rhettinger
    Copy link
    Contributor

    Serhiy, go ahead and apply your patch. The existing code is clearly wrong.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented May 22, 2015

    New changeset bccaba8a5482 by Serhiy Storchaka in branch '2.7':
    Issue bpo-24257: Fixed segmentation fault in sqlite3.Row constructor with faked
    https://hg.python.org/cpython/rev/bccaba8a5482

    New changeset c7b9645a6f35 by Serhiy Storchaka in branch '3.4':
    Issue bpo-24257: Fixed incorrect uses of PyObject_IsInstance().
    https://hg.python.org/cpython/rev/c7b9645a6f35

    New changeset a5101529a8a9 by Serhiy Storchaka in branch 'default':
    Issue bpo-24257: Fixed incorrect uses of PyObject_IsInstance().
    https://hg.python.org/cpython/rev/a5101529a8a9

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    extension-modules C modules in the Modules dir interpreter-core (Objects, Python, Grammar, and Parser dirs) type-crash A hard crash of the interpreter, possibly with a core dump
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants