[Python-checkins] cpython (3.2): Issue #9651: Fix a crash when ctypes.create_string_buffer(0) was passed to

georg.brandl python-checkins at python.org
Sun Sep 4 08:42:30 CEST 2011


http://hg.python.org/cpython/rev/f19d00b5220a
changeset:   72242:f19d00b5220a
branch:      3.2
user:        Amaury Forgeot d'Arc <amauryfa at gmail.com>
date:        Tue Aug 30 21:40:20 2011 +0200
summary:
  Issue #9651: Fix a crash when ctypes.create_string_buffer(0) was passed to
some functions like file.write().

files:
  Lib/ctypes/test/test_buffers.py |  4 ++++
  Misc/NEWS                       |  3 +++
  Modules/_ctypes/_ctypes.c       |  6 ++++--
  3 files changed, 11 insertions(+), 2 deletions(-)


diff --git a/Lib/ctypes/test/test_buffers.py b/Lib/ctypes/test/test_buffers.py
--- a/Lib/ctypes/test/test_buffers.py
+++ b/Lib/ctypes/test/test_buffers.py
@@ -20,6 +20,10 @@
         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
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -152,6 +152,9 @@
 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
--- a/Modules/_ctypes/_ctypes.c
+++ b/Modules/_ctypes/_ctypes.c
@@ -2488,8 +2488,10 @@
     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;

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list