Skip to content

Commit 1c748f3

Browse files
committed
Issue #27570: Merge null pointer fixes from 3.5
2 parents f3b5bca + be8da9c commit 1c748f3

5 files changed

Lines changed: 40 additions & 15 deletions

File tree

Lib/test/test_array.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,24 @@ def __init__(self, typecode, newarg=None):
3636
if have_long_long:
3737
typecodes += 'qQ'
3838

39-
class BadConstructorTest(unittest.TestCase):
39+
class MiscTest(unittest.TestCase):
4040

41-
def test_constructor(self):
41+
def test_bad_constructor(self):
4242
self.assertRaises(TypeError, array.array)
4343
self.assertRaises(TypeError, array.array, spam=42)
4444
self.assertRaises(TypeError, array.array, 'xx')
4545
self.assertRaises(ValueError, array.array, 'x')
4646

47+
def test_empty(self):
48+
# Exercise code for handling zero-length arrays
49+
a = array.array('B')
50+
a[:] = a
51+
self.assertEqual(len(a), 0)
52+
self.assertEqual(len(a + a), 0)
53+
self.assertEqual(len(a * 3), 0)
54+
a += a
55+
self.assertEqual(len(a), 0)
56+
4757

4858
# Machine format codes.
4959
#

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ Core and Builtins
9696
Library
9797
-------
9898

99+
- Issue #27570: Avoid zero-length memcpy() etc calls with null source
100+
pointers in the "ctypes" and "array" modules.
101+
99102
- Issue #22233: Break email header lines *only* on the RFC specified CR and LF
100103
characters, not on arbitrary unicode line breaks. This also fixes a bug in
101104
HTTP header parsing.

Modules/_ctypes/_ctypes.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1375,8 +1375,10 @@ PyCArrayType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
13751375
goto error;
13761376
}
13771377
stgdict->shape[0] = length;
1378-
memmove(&stgdict->shape[1], itemdict->shape,
1379-
sizeof(Py_ssize_t) * (stgdict->ndim - 1));
1378+
if (stgdict->ndim > 1) {
1379+
memmove(&stgdict->shape[1], itemdict->shape,
1380+
sizeof(Py_ssize_t) * (stgdict->ndim - 1));
1381+
}
13801382

13811383
itemsize = itemdict->size;
13821384
if (length * itemsize < 0) {

Modules/_ctypes/stgdict.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -391,9 +391,11 @@ PyCStructUnionType_update_stgdict(PyObject *type, PyObject *fields, int isStruct
391391
}
392392
memset(stgdict->ffi_type_pointer.elements, 0,
393393
sizeof(ffi_type *) * (basedict->length + len + 1));
394-
memcpy(stgdict->ffi_type_pointer.elements,
395-
basedict->ffi_type_pointer.elements,
396-
sizeof(ffi_type *) * (basedict->length));
394+
if (basedict->length > 0) {
395+
memcpy(stgdict->ffi_type_pointer.elements,
396+
basedict->ffi_type_pointer.elements,
397+
sizeof(ffi_type *) * (basedict->length));
398+
}
397399
ffi_ofs = basedict->length;
398400
} else {
399401
offset = 0;

Modules/arraymodule.c

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -740,8 +740,10 @@ array_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
740740
np = (arrayobject *) newarrayobject(&Arraytype, ihigh - ilow, a->ob_descr);
741741
if (np == NULL)
742742
return NULL;
743-
memcpy(np->ob_item, a->ob_item + ilow * a->ob_descr->itemsize,
744-
(ihigh-ilow) * a->ob_descr->itemsize);
743+
if (ihigh > ilow) {
744+
memcpy(np->ob_item, a->ob_item + ilow * a->ob_descr->itemsize,
745+
(ihigh-ilow) * a->ob_descr->itemsize);
746+
}
745747
return (PyObject *)np;
746748
}
747749

@@ -799,9 +801,13 @@ array_concat(arrayobject *a, PyObject *bb)
799801
if (np == NULL) {
800802
return NULL;
801803
}
802-
memcpy(np->ob_item, a->ob_item, Py_SIZE(a)*a->ob_descr->itemsize);
803-
memcpy(np->ob_item + Py_SIZE(a)*a->ob_descr->itemsize,
804-
b->ob_item, Py_SIZE(b)*b->ob_descr->itemsize);
804+
if (Py_SIZE(a) > 0) {
805+
memcpy(np->ob_item, a->ob_item, Py_SIZE(a)*a->ob_descr->itemsize);
806+
}
807+
if (Py_SIZE(b) > 0) {
808+
memcpy(np->ob_item + Py_SIZE(a)*a->ob_descr->itemsize,
809+
b->ob_item, Py_SIZE(b)*b->ob_descr->itemsize);
810+
}
805811
return (PyObject *)np;
806812
#undef b
807813
}
@@ -821,7 +827,7 @@ array_repeat(arrayobject *a, Py_ssize_t n)
821827
np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr);
822828
if (np == NULL)
823829
return NULL;
824-
if (n == 0)
830+
if (size == 0)
825831
return (PyObject *)np;
826832
oldbytes = Py_SIZE(a) * a->ob_descr->itemsize;
827833
newbytes = oldbytes * n;
@@ -942,8 +948,10 @@ array_do_extend(arrayobject *self, PyObject *bb)
942948
size = oldsize + Py_SIZE(b);
943949
if (array_resize(self, size) == -1)
944950
return -1;
945-
memcpy(self->ob_item + oldsize * self->ob_descr->itemsize,
946-
b->ob_item, bbsize * b->ob_descr->itemsize);
951+
if (bbsize > 0) {
952+
memcpy(self->ob_item + oldsize * self->ob_descr->itemsize,
953+
b->ob_item, bbsize * b->ob_descr->itemsize);
954+
}
947955

948956
return 0;
949957
#undef b

0 commit comments

Comments
 (0)