From: Amaury Forgeot d'Arc Date: Tue, 30 Aug 2011 19:04:35 +0000 (+0200) Subject: Issue #9651: Fix a crash when ctypes.create_string_buffer(0) was passed to X-Git-Tag: v2.7.3rc1~477 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=3d7f2363299f8dbfae93936d606bceb3c49939b2;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 bf931abc6a..12945ed980 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], "ac") self.assertEqual(b[::5], "a") + def test_buffer_interface(self): + self.assertEqual(len(bytearray(create_string_buffer(0))), 0) + self.assertEqual(len(bytearray(create_string_buffer(1))), 1) + def test_string_conversion(self): b = create_string_buffer(u"abc") self.assertEqual(len(b), 4) # trailing nul char diff --git a/Misc/NEWS b/Misc/NEWS index a35c186719..8c77c9f91f 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -180,6 +180,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 c6c6dbda18..c4d08901cd 100644 --- a/Modules/_ctypes/_ctypes.c +++ b/Modules/_ctypes/_ctypes.c @@ -2576,8 +2576,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;