]> granicus.if.org Git - python/commitdiff
Issue #9651: Fix a crash when ctypes.create_string_buffer(0) was passed to
authorAmaury Forgeot d'Arc <amauryfa@gmail.com>
Tue, 30 Aug 2011 19:40:20 +0000 (21:40 +0200)
committerAmaury Forgeot d'Arc <amauryfa@gmail.com>
Tue, 30 Aug 2011 19:40:20 +0000 (21:40 +0200)
some functions like file.write().

Lib/ctypes/test/test_buffers.py
Misc/NEWS
Modules/_ctypes/_ctypes.c

index c19c05a300ac9babaf2685d4f449320935f4a15a..2dc74841c496e47a86be10864f6f135b02fd9265 100644 (file)
@@ -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:
index 86eea69a3f6144300771afa03c8d79e18c5efb8e..af26b8dcfa426998029e7d891b23841fb1c5a348 100644 (file)
--- 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.
index 8e8598002d9949d1e944eeac48ecf1d732d742c2..277206c7de0e8c4ae3bc1e8bf9ec9fe4bfc78ed9 100644 (file)
@@ -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;