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

Avoid memcpy(. . ., NULL, 0) etc calls #71757

Closed
vadmium opened this issue Jul 19, 2016 · 7 comments
Closed

Avoid memcpy(. . ., NULL, 0) etc calls #71757

vadmium opened this issue Jul 19, 2016 · 7 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) topic-ctypes type-bug An unexpected behavior, bug, or error

Comments

@vadmium
Copy link
Member

vadmium commented Jul 19, 2016

BPO 27570
Nosy @benjaminp, @vadmium
Files
  • memcpy-null.patch
  • memcpy-null.v2.patch
  • memcpy-null.v3.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 2016-09-08.06:11:34.573>
    created_at = <Date 2016-07-19.02:57:10.714>
    labels = ['interpreter-core', 'ctypes', 'type-bug']
    title = 'Avoid memcpy(. . ., NULL, 0) etc calls'
    updated_at = <Date 2016-09-08.06:11:34.572>
    user = 'https://github.com/vadmium'

    bugs.python.org fields:

    activity = <Date 2016-09-08.06:11:34.572>
    actor = 'martin.panter'
    assignee = 'none'
    closed = True
    closed_date = <Date 2016-09-08.06:11:34.573>
    closer = 'martin.panter'
    components = ['Interpreter Core', 'ctypes']
    creation = <Date 2016-07-19.02:57:10.714>
    creator = 'martin.panter'
    dependencies = []
    files = ['43784', '43852', '43987']
    hgrepos = []
    issue_num = 27570
    keywords = ['patch']
    message_count = 7.0
    messages = ['270806', '271135', '271874', '274701', '274708', '274917', '274969']
    nosy_count = 3.0
    nosy_names = ['benjamin.peterson', 'python-dev', 'martin.panter']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue27570'
    versions = ['Python 2.7', 'Python 3.5', 'Python 3.6']

    @vadmium
    Copy link
    Member Author

    vadmium commented Jul 19, 2016

    This patch fixes errors reported by GCC’s undefined behaviour sanitizer about calling functions with a null pointer:

    ./configure CC="gcc -fsanitize=undefined"

    Using bpo-22605 as a precedent, I propose to avoid calling memcpy() and memmove() for zero-length copies when there may be a null pointer.

    @vadmium vadmium added interpreter-core (Objects, Python, Grammar, and Parser dirs) topic-ctypes type-bug An unexpected behavior, bug, or error labels Jul 19, 2016
    @vadmium
    Copy link
    Member Author

    vadmium commented Jul 24, 2016

    V2 patch adds another fix, uncovered by recent datetime tests:

    >>> a = array("B")
    >>> a[:] = a
    /media/disk/home/proj/python/cpython/Modules/arraymodule.c:748:5: runtime error: null pointer passed as argument 1, which is declared to never be null
    /media/disk/home/proj/python/cpython/Modules/arraymodule.c:748:5: runtime error: null pointer passed as argument 2, which is declared to never be null

    @vadmium
    Copy link
    Member Author

    vadmium commented Aug 3, 2016

    Patch 3 fixes some more cases I found in the array module:

    >>> a + a
    Modules/arraymodule.c:809:5: runtime error: null pointer passed as argument 1, which is declared to never be null
    Modules/arraymodule.c:809:5: runtime error: null pointer passed as argument 2, which is declared to never be null
    Modules/arraymodule.c:810:5: runtime error: null pointer passed as argument 1, which is declared to never be null
    Modules/arraymodule.c:810:5: runtime error: null pointer passed as argument 2, which is declared to never be null
    array('B')
    >>> a * 3
    Modules/arraymodule.c:840:9: runtime error: null pointer passed as argument 1, which is declared to never be null
    Modules/arraymodule.c:840:9: runtime error: null pointer passed as argument 2, which is declared to never be null
    array('B')
    >>> a += a
    Modules/arraymodule.c:952:5: runtime error: null pointer passed as argument 1, which is declared to never be null
    Modules/arraymodule.c:952:5: runtime error: null pointer passed as argument 2, which is declared to never be null

    I wondered if there is a good argument for fixing these, or if it is only a theoretical problem. Apparently GCC can do optimizations about null pointer tests: <https://gcc.gnu.org/gcc-4.9/porting_to.html\>. I don’t think any of the cases I found are instances of this problem, but I think fixing them helps keep the UB sanitizer output clean, so any errors causing practical behaviour problems will be easier to find.

    @vadmium
    Copy link
    Member Author

    vadmium commented Sep 7, 2016

    Looks like revisions 5f3f6f1fb73a and ec537f9f468f may have fixed the listobject cases.

    Also 66feda02f2a5 looks relevant. Benjamin, maybe you are interested in other bits of my patches :)

    @benjaminp
    Copy link
    Contributor

    Sorry I missed this. The changes which I didn't already make look good. :)

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Sep 7, 2016

    New changeset e231dcad3a9b by Martin Panter in branch '3.5':
    Issue bpo-27570: Avoid zero-length memcpy() calls with null source pointers
    https://hg.python.org/cpython/rev/e231dcad3a9b

    New changeset 2d0fb659372c by Martin Panter in branch 'default':
    Issue bpo-27570: Merge null pointer fixes from 3.5
    https://hg.python.org/cpython/rev/2d0fb659372c

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Sep 8, 2016

    New changeset d465da1e5902 by Martin Panter in branch '2.7':
    Issue bpo-27570: Avoid zero-length memcpy() calls with null source pointers
    https://hg.python.org/cpython/rev/d465da1e5902

    @vadmium vadmium closed this as completed Sep 8, 2016
    @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
    interpreter-core (Objects, Python, Grammar, and Parser dirs) topic-ctypes type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants