From: Amaury Forgeot d'Arc Date: Tue, 30 Aug 2011 19:40:20 +0000 (+0200) Subject: Issue #9651: Fix a crash when ctypes.create_string_buffer(0) was passed to X-Git-Tag: v3.3.0a1~1598^2~1 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=326e189410106de22c4af9d8449e0a8dc631c7dc;p=python Issue #9651: Fix a crash when ctypes.create_string_buffer(0) was passed to some functions like file.write(). --- diff --git a/Lib/ctypes/test/test_buffers.py b/Lib/ctypes/test/test_buffers.py index c19c05a300..2dc74841c4 100644 --- a/Lib/ctypes/test/test_buffers.py +++ b/Lib/ctypes/test/test_buffers.py @@ -20,6 +20,10 @@ class StringBufferTestCase(unittest.TestCase): self.assertEqual(b[::2], b"ac") self.assertEqual(b[::5], b"a") + def test_buffer_interface(self): + self.assertEqual(len(bytearray(create_string_buffer(0))), 0) + self.assertEqual(len(bytearray(create_string_buffer(1))), 1) + try: c_wchar except NameError: diff --git a/Misc/NEWS b/Misc/NEWS index 35343faaf8..a6db483235 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -185,6 +185,9 @@ Library Extension Modules ----------------- +- Issue #9651: Fix a crash when ctypes.create_string_buffer(0) was passed to + some functions like file.write(). + - Issue #10309: Define _GNU_SOURCE so that mremap() gets the proper signature. Without this, architectures where sizeof void* != sizeof int are broken. Patch given by Hallvard B Furuseth. diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c index 8e8598002d..277206c7de 100644 --- a/Modules/_ctypes/_ctypes.c +++ b/Modules/_ctypes/_ctypes.c @@ -2488,8 +2488,10 @@ static int PyCData_NewGetBuffer(PyObject *_self, Py_buffer *view, int flags) view->ndim = dict->ndim; view->shape = dict->shape; view->itemsize = self->b_size; - for (i = 0; i < view->ndim; ++i) { - view->itemsize /= dict->shape[i]; + if (view->itemsize) { + for (i = 0; i < view->ndim; ++i) { + view->itemsize /= dict->shape[i]; + } } view->strides = NULL; view->suboffsets = NULL;