Skip to content

Commit 1fbcaef

Browse files
author
Victor Stinner
committed
Fix array.array('u') constructor
1 parent eb5657a commit 1fbcaef

1 file changed

Lines changed: 12 additions & 6 deletions

File tree

‎Modules/arraymodule.c‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2529,19 +2529,25 @@ array_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
25292529
Py_DECREF(v);
25302530
}
25312531
else if (initial != NULL && PyUnicode_Check(initial)) {
2532-
Py_ssize_t n = PyUnicode_GET_DATA_SIZE(initial);
2532+
Py_ssize_t n;
2533+
if (PyUnicode_READY(initial)) {
2534+
Py_DECREF(a);
2535+
return NULL;
2536+
}
2537+
n = PyUnicode_GET_LENGTH(initial);
25332538
if (n > 0) {
25342539
arrayobject *self = (arrayobject *)a;
2535-
char *item = self->ob_item;
2536-
item = (char *)PyMem_Realloc(item, n);
2540+
Py_UCS4 *item = (Py_UCS4 *)self->ob_item;
2541+
item = (char *)PyMem_Realloc(item, n * sizeof(Py_UCS4));
25372542
if (item == NULL) {
25382543
PyErr_NoMemory();
25392544
Py_DECREF(a);
25402545
return NULL;
25412546
}
2542-
self->ob_item = item;
2543-
Py_SIZE(self) = n / sizeof(Py_UCS4);
2544-
memcpy(item, PyUnicode_AS_DATA(initial), n);
2547+
self->ob_item = (char*)item;
2548+
Py_SIZE(self) = n;
2549+
if (!PyUnicode_AsUCS4(initial, item, n, 0))
2550+
return NULL;
25452551
self->allocated = Py_SIZE(self);
25462552
}
25472553
}

0 commit comments

Comments
 (0)