summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAmaury Forgeot d'Arc <amauryfa@gmail.com>2011-08-30 19:40:20 (GMT)
committerAmaury Forgeot d'Arc <amauryfa@gmail.com>2011-08-30 19:40:20 (GMT)
commit4cfb42dd2dc804600e9c1b33425f4a6095c46752 (patch)
treeb4934c1c9eda63803aea04fa255dadeb6669c080
parent97d67924e3a3ac1bbe07d6abc02c233a330013f1 (diff)
downloadcpython-4cfb42dd2dc804600e9c1b33425f4a6095c46752.zip
cpython-4cfb42dd2dc804600e9c1b33425f4a6095c46752.tar.gz
cpython-4cfb42dd2dc804600e9c1b33425f4a6095c46752.tar.bz2
Issue #9651: Fix a crash when ctypes.create_string_buffer(0) was passed to
some functions like file.write().
-rw-r--r--Lib/ctypes/test/test_buffers.py4
-rw-r--r--Misc/NEWS3
-rw-r--r--Modules/_ctypes/_ctypes.c6
3 files changed, 11 insertions, 2 deletions
diff --git a/Lib/ctypes/test/test_buffers.py b/Lib/ctypes/test/test_buffers.py
index c19c05a..2dc7484 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 86eea69..af26b8d 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -152,6 +152,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 8e85980..277206c 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;