Commit e28706c8 authored by Victor Stinner's avatar Victor Stinner

Issue #18203: Replace malloc() with PyMem_Malloc() in _PySequence_BytesToCharpArray()

parent bc05520b
...@@ -2721,8 +2721,8 @@ PyIter_Next(PyObject *iter) ...@@ -2721,8 +2721,8 @@ PyIter_Next(PyObject *iter)
* NULL terminated string pointers with a NULL char* terminating the array. * NULL terminated string pointers with a NULL char* terminating the array.
* (ie: an argv or env list) * (ie: an argv or env list)
* *
* Memory allocated for the returned list is allocated using malloc() and MUST * Memory allocated for the returned list is allocated using PyMem_Malloc()
* be freed by the caller using a free() loop or _Py_FreeCharPArray(). * and MUST be freed by _Py_FreeCharPArray().
*/ */
char *const * char *const *
_PySequence_BytesToCharpArray(PyObject* self) _PySequence_BytesToCharpArray(PyObject* self)
...@@ -2730,6 +2730,7 @@ _PySequence_BytesToCharpArray(PyObject* self) ...@@ -2730,6 +2730,7 @@ _PySequence_BytesToCharpArray(PyObject* self)
char **array; char **array;
Py_ssize_t i, argc; Py_ssize_t i, argc;
PyObject *item = NULL; PyObject *item = NULL;
Py_ssize_t size;
argc = PySequence_Size(self); argc = PySequence_Size(self);
if (argc == -1) if (argc == -1)
...@@ -2742,7 +2743,7 @@ _PySequence_BytesToCharpArray(PyObject* self) ...@@ -2742,7 +2743,7 @@ _PySequence_BytesToCharpArray(PyObject* self)
return NULL; return NULL;
} }
array = malloc((argc + 1) * sizeof(char *)); array = PyMem_Malloc((argc + 1) * sizeof(char *));
if (array == NULL) { if (array == NULL) {
PyErr_NoMemory(); PyErr_NoMemory();
return NULL; return NULL;
...@@ -2761,11 +2762,13 @@ _PySequence_BytesToCharpArray(PyObject* self) ...@@ -2761,11 +2762,13 @@ _PySequence_BytesToCharpArray(PyObject* self)
array[i] = NULL; array[i] = NULL;
goto fail; goto fail;
} }
array[i] = strdup(data); size = PyBytes_GET_SIZE(item) + 1;
array[i] = PyMem_Malloc(size);
if (!array[i]) { if (!array[i]) {
PyErr_NoMemory(); PyErr_NoMemory();
goto fail; goto fail;
} }
memcpy(array[i], data, size);
Py_DECREF(item); Py_DECREF(item);
} }
array[argc] = NULL; array[argc] = NULL;
...@@ -2785,7 +2788,7 @@ _Py_FreeCharPArray(char *const array[]) ...@@ -2785,7 +2788,7 @@ _Py_FreeCharPArray(char *const array[])
{ {
Py_ssize_t i; Py_ssize_t i;
for (i = 0; array[i] != NULL; ++i) { for (i = 0; array[i] != NULL; ++i) {
free(array[i]); PyMem_Free(array[i]);
} }
free((void*)array); PyMem_Free((void*)array);
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment