cpython: d465da1e5902 (original) (raw)
Mercurial > cpython
changeset 103290:d465da1e5902 2.7
Issue #27570: Avoid zero-length memcpy() calls with null source pointers [#27570]
Martin Panter vadmium+py@gmail.com | |
---|---|
date | Wed, 07 Sep 2016 11:04:41 +0000 |
parents | 731e5617cc8d |
children | 9a68ab141c33 |
files | Lib/test/test_array.py Misc/NEWS Modules/_ctypes/_ctypes.c Modules/_ctypes/stgdict.c Modules/arraymodule.c |
diffstat | 5 files changed, 42 insertions(+), 15 deletions(-)[+] [-] Lib/test/test_array.py 16 Misc/NEWS 3 Modules/_ctypes/_ctypes.c 6 Modules/_ctypes/stgdict.c 8 Modules/arraymodule.c 24 |
line wrap: on
line diff
--- a/Lib/test/test_array.py +++ b/Lib/test/test_array.py @@ -22,9 +22,9 @@ typecodes = "cbBhHiIlLfd" if test_support.have_unicode: typecodes += "u" -class BadConstructorTest(unittest.TestCase): +class MiscTest(unittest.TestCase):
- def test_bad_constructor(self): self.assertRaises(TypeError, array.array) self.assertRaises(TypeError, array.array, spam=42) self.assertRaises(TypeError, array.array, 'xx')
@@ -40,7 +40,17 @@ class BadConstructorTest(unittest.TestCa self.assertRaises(ValueError, array.array, u'x') self.assertRaises(ValueError, array.array, u'\x80') -tests.append(BadConstructorTest)
- def test_empty(self):
# Exercise code for handling zero-length arrays[](#l1.21)
a = array.array('B')[](#l1.22)
a[:] = a[](#l1.23)
self.assertEqual(len(a), 0)[](#l1.24)
self.assertEqual(len(a + a), 0)[](#l1.25)
self.assertEqual(len(a * 3), 0)[](#l1.26)
a += a[](#l1.27)
self.assertEqual(len(a), 0)[](#l1.28)
+ +tests.append(MiscTest) class BaseTest(unittest.TestCase): # Required class attributes (provided by subclasses
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -40,6 +40,9 @@ Core and Builtins Library ------- +- Issue #27570: Avoid zero-length memcpy() etc calls with null source
- lib2to3.pgen3.driver.load_grammar() now creates a stable cache file between runs given the same Grammar.txt input regardless of the hash randomization setting.
--- a/Modules/_ctypes/_ctypes.c +++ b/Modules/_ctypes/_ctypes.c @@ -1426,8 +1426,10 @@ PyCArrayType_new(PyTypeObject *type, PyO return NULL; } stgdict->shape[0] = length;
- if (stgdict->ndim > 1) {
memmove(&stgdict->shape[1], itemdict->shape,[](#l3.10)
sizeof(Py_ssize_t) * (stgdict->ndim - 1));[](#l3.11)
- }
itemsize = itemdict->size; if (length * itemsize < 0) {
--- a/Modules/_ctypes/stgdict.c +++ b/Modules/_ctypes/stgdict.c @@ -395,9 +395,11 @@ PyCStructUnionType_update_stgdict(PyObje } memset(stgdict->ffi_type_pointer.elements, 0, sizeof(ffi_type *) * (basedict->length + len + 1));
memcpy(stgdict->ffi_type_pointer.elements,[](#l4.7)
basedict->ffi_type_pointer.elements,[](#l4.8)
sizeof(ffi_type *) * (basedict->length));[](#l4.9)
if (basedict->length > 0) {[](#l4.10)
memcpy(stgdict->ffi_type_pointer.elements,[](#l4.11)
basedict->ffi_type_pointer.elements,[](#l4.12)
sizeof(ffi_type *) * (basedict->length));[](#l4.13)
} else { offset = 0;}[](#l4.14) ffi_ofs = basedict->length;[](#l4.15)
--- a/Modules/arraymodule.c +++ b/Modules/arraymodule.c @@ -620,8 +620,10 @@ array_slice(arrayobject *a, Py_ssize_t i np = (arrayobject *) newarrayobject(&Arraytype, ihigh - ilow, a->ob_descr); if (np == NULL) return NULL;
- memcpy(np->ob_item, a->ob_item + ilow * a->ob_descr->itemsize,
(ihigh-ilow) * a->ob_descr->itemsize);[](#l5.8)
- if (ihigh > ilow) {
memcpy(np->ob_item, a->ob_item + ilow * a->ob_descr->itemsize,[](#l5.10)
(ihigh-ilow) * a->ob_descr->itemsize);[](#l5.11)
- } return (PyObject *)np; }
@@ -660,9 +662,13 @@ array_concat(arrayobject *a, PyObject *b if (np == NULL) { return NULL; }
- memcpy(np->ob_item, a->ob_item, Py_SIZE(a)*a->ob_descr->itemsize);
- memcpy(np->ob_item + Py_SIZE(a)*a->ob_descr->itemsize,
b->ob_item, Py_SIZE(b)*b->ob_descr->itemsize);[](#l5.22)
- if (Py_SIZE(a) > 0) {
memcpy(np->ob_item, a->ob_item, Py_SIZE(a)*a->ob_descr->itemsize);[](#l5.24)
- }
- if (Py_SIZE(b) > 0) {
memcpy(np->ob_item + Py_SIZE(a)*a->ob_descr->itemsize,[](#l5.27)
b->ob_item, Py_SIZE(b)*b->ob_descr->itemsize);[](#l5.28)
- } return (PyObject *)np;
#undef b } @@ -684,6 +690,8 @@ array_repeat(arrayobject *a, Py_ssize_t np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr); if (np == NULL) return NULL;
- if (size == 0)
p = np->ob_item; nbytes = Py_SIZE(a) * a->ob_descr->itemsize; for (i = 0; i < n; i++) { @@ -838,8 +846,10 @@ array_do_extend(arrayobject *self, PyObj PyErr_NoMemory(); return -1; }return (PyObject *)np;[](#l5.38)