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

mmap.read() crashes when passed a negative argument #50593

Closed
amauryfa opened this issue Jun 25, 2009 · 8 comments
Closed

mmap.read() crashes when passed a negative argument #50593

amauryfa opened this issue Jun 25, 2009 · 8 comments
Labels
stdlib Python modules in the Lib dir type-crash A hard crash of the interpreter, possibly with a core dump

Comments

@amauryfa
Copy link
Member

BPO 6344
Nosy @amauryfa
Files
  • fix_mmap_read.patch
  • fix_mmap_read.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 2009-06-29.15:10:28.077>
    created_at = <Date 2009-06-25.21:22:27.717>
    labels = ['library', 'type-crash']
    title = 'mmap.read() crashes when passed a negative argument'
    updated_at = <Date 2009-06-29.15:10:28.075>
    user = 'https://github.com/amauryfa'

    bugs.python.org fields:

    activity = <Date 2009-06-29.15:10:28.075>
    actor = 'ocean-city'
    assignee = 'none'
    closed = True
    closed_date = <Date 2009-06-29.15:10:28.077>
    closer = 'ocean-city'
    components = ['Library (Lib)']
    creation = <Date 2009-06-25.21:22:27.717>
    creator = 'amaury.forgeotdarc'
    dependencies = []
    files = ['14372', '14382']
    hgrepos = []
    issue_num = 6344
    keywords = ['patch']
    message_count = 8.0
    messages = ['89714', '89717', '89724', '89757', '89771', '89824', '89830', '89849']
    nosy_count = 2.0
    nosy_names = ['amaury.forgeotdarc', 'ocean-city']
    pr_nums = []
    priority = 'high'
    resolution = 'fixed'
    stage = None
    status = 'closed'
    superseder = None
    type = 'crash'
    url = 'https://bugs.python.org/issue6344'
    versions = ['Python 2.7']

    @amauryfa
    Copy link
    Member Author

    mmap.read() crashes when passed a negative count:

        def test_read_negative(self):
            f = open(TESTFN, 'w+')
            f.write("ABCDE\0abcde")
            f.flush()
    
            mf = mmap.mmap(f.fileno(), 0)
        self.assertEqual(mf.read(2),  "AB")    # OK
        self.assertEqual(mf.read(-1), "CDE")   # ??
        self.assertEqual(mf.read(-1), "BCDE")  # ??
        self.assertEqual(mf.read(-1), "ABCDE") # ??
        mf.read(-1)                            # crash!!
    

    I don't know what mf.read(-1) should do: raise a ValueError, return the empty
    string, or return everything from the current pos to len(mf)?

    @amauryfa amauryfa added stdlib Python modules in the Lib dir type-crash A hard crash of the interpreter, possibly with a core dump labels Jun 25, 2009
    @ocean-city
    Copy link
    Mannequin

    ocean-city mannequin commented Jun 26, 2009

    I don't know what mf.read(-1) should do
    I'm not sure neither.

    I think the problem is that mmap uses size_t as length, but uses
    Py_ssize_t for PyArg_ParseTuple. (PyArg_ParseTuple doesn't support
    size_t) I think this discrepancy should be fixed.

    If mmap should use size_t because it should cover all possible memory
    region which size_t can represent but Py_ssize_t cannot, maybe should
    PyArg_ParseTuple support size_t?

    @amauryfa
    Copy link
    Member Author

    Well, I would not care that much: you can never read more than
    PY_SSIZE_T_MAX, because the mapped area and the string would not fit in
    the addressable space of the process.

    @ocean-city
    Copy link
    Mannequin

    ocean-city mannequin commented Jun 27, 2009

    Hmm, I cannot reproduce the crash. I created the patch experimentally,
    but I'm not confident with this patch. Especially here

    + if (n < 0)
    + n = PY_SSIZE_T_MAX;

    because I don't have so much memory.

    @amauryfa
    Copy link
    Member Author

    • if (n < 0)

    I suggest a comment like:

    /* The difference can overflow, only if self->size is greater than

    • PY_SSIZE_T_MAX. But then the operation cannot possibly succeed,
    • because the mapped area and the returned string each need more
    • than half of the addressable memory. So we clip the size, and let
    • the code below raise MemoryError.
      */

    @ocean-city
    Copy link
    Mannequin

    ocean-city mannequin commented Jun 29, 2009

    OK, how about this patch?

    @amauryfa
    Copy link
    Member Author

    OK for me.

    @ocean-city
    Copy link
    Mannequin

    ocean-city mannequin commented Jun 29, 2009

    Thanks, committed in r73677(trunk), r73682(release26-maint),
    r73684(py3k), r73685(release31-maint).

    @ocean-city ocean-city mannequin closed this as completed Jun 29, 2009
    @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
    stdlib Python modules in the Lib dir type-crash A hard crash of the interpreter, possibly with a core dump
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant