Issue #18203: Add _PyMem_RawStrdup() and _PyMem_Strdup()
Replace strdup() with _PyMem_RawStrdup() or _PyMem_Strdup(), depending if the
GIL is held or not.
diff --git a/Modules/_cursesmodule.c b/Modules/_cursesmodule.c
index fbe18f6..5d14898 100644
--- a/Modules/_cursesmodule.c
+++ b/Modules/_cursesmodule.c
@@ -529,7 +529,7 @@
wo = PyObject_NEW(PyCursesWindowObject, &PyCursesWindow_Type);
if (wo == NULL) return NULL;
wo->win = win;
- wo->encoding = strdup(encoding);
+ wo->encoding = _PyMem_Strdup(encoding);
if (wo->encoding == NULL) {
Py_DECREF(wo);
PyErr_NoMemory();
@@ -543,7 +543,7 @@
{
if (wo->win != stdscr) delwin(wo->win);
if (wo->encoding != NULL)
- free(wo->encoding);
+ PyMem_Free(wo->encoding);
PyObject_DEL(wo);
}
@@ -1938,13 +1938,13 @@
ascii = PyUnicode_AsASCIIString(value);
if (ascii == NULL)
return -1;
- encoding = strdup(PyBytes_AS_STRING(ascii));
+ encoding = _PyMem_Strdup(PyBytes_AS_STRING(ascii));
Py_DECREF(ascii);
if (encoding == NULL) {
PyErr_NoMemory();
return -1;
}
- free(self->encoding);
+ PyMem_Free(self->encoding);
self->encoding = encoding;
return 0;
}
diff --git a/Modules/_pickle.c b/Modules/_pickle.c
index 5b30931..72bd7fc 100644
--- a/Modules/_pickle.c
+++ b/Modules/_pickle.c
@@ -1213,8 +1213,8 @@
if (errors == NULL)
errors = "strict";
- self->encoding = strdup(encoding);
- self->errors = strdup(errors);
+ self->encoding = _PyMem_Strdup(encoding);
+ self->errors = _PyMem_Strdup(errors);
if (self->encoding == NULL || self->errors == NULL) {
PyErr_NoMemory();
return -1;
@@ -5590,8 +5590,8 @@
_Unpickler_MemoCleanup(self);
PyMem_Free(self->marks);
PyMem_Free(self->input_line);
- free(self->encoding);
- free(self->errors);
+ PyMem_Free(self->encoding);
+ PyMem_Free(self->errors);
Py_TYPE(self)->tp_free((PyObject *)self);
}
@@ -5627,9 +5627,9 @@
self->marks = NULL;
PyMem_Free(self->input_line);
self->input_line = NULL;
- free(self->encoding);
+ PyMem_Free(self->encoding);
self->encoding = NULL;
- free(self->errors);
+ PyMem_Free(self->errors);
self->errors = NULL;
return 0;
diff --git a/Modules/faulthandler.c b/Modules/faulthandler.c
index 686a45a..172945d 100644
--- a/Modules/faulthandler.c
+++ b/Modules/faulthandler.c
@@ -475,7 +475,7 @@
Py_CLEAR(thread.file);
if (thread.header) {
- free(thread.header);
+ PyMem_Free(thread.header);
thread.header = NULL;
}
}
@@ -504,7 +504,7 @@
"Timeout (%lu:%02lu:%02lu)!\n",
hour, min, sec);
- return strdup(buffer);
+ return _PyMem_Strdup(buffer);
}
static PyObject*
@@ -570,7 +570,7 @@
if (PyThread_start_new_thread(faulthandler_thread, NULL) == -1) {
PyThread_release_lock(thread.running);
Py_CLEAR(thread.file);
- free(header);
+ PyMem_Free(header);
thread.header = NULL;
PyErr_SetString(PyExc_RuntimeError,
"unable to start watchdog thread");
@@ -729,9 +729,10 @@
return NULL;
if (user_signals == NULL) {
- user_signals = calloc(NSIG, sizeof(user_signal_t));
+ user_signals = PyMem_Malloc(NSIG * sizeof(user_signal_t));
if (user_signals == NULL)
return PyErr_NoMemory();
+ memset(user_signals, 0, NSIG * sizeof(user_signal_t));
}
user = &user_signals[signum];
@@ -1136,7 +1137,7 @@
if (user_signals != NULL) {
for (signum=0; signum < NSIG; signum++)
faulthandler_unregister(&user_signals[signum], signum);
- free(user_signals);
+ PyMem_Free(user_signals);
user_signals = NULL;
}
#endif
diff --git a/Modules/main.c b/Modules/main.c
index 2bafbfd..af0d7f4 100644
--- a/Modules/main.c
+++ b/Modules/main.c
@@ -544,7 +544,7 @@
Py_FatalError(
"not enough memory to copy PYTHONWARNINGS");
strcpy(buf, p);
- oldloc = strdup(setlocale(LC_ALL, NULL));
+ oldloc = _PyMem_RawStrdup(setlocale(LC_ALL, NULL));
setlocale(LC_ALL, "");
for (p = strtok(buf, ","); p != NULL; p = strtok(NULL, ",")) {
#ifdef __APPLE__
@@ -562,7 +562,7 @@
Py_DECREF(unicode);
}
setlocale(LC_ALL, oldloc);
- free(oldloc);
+ PyMem_RawFree(oldloc);
PyMem_RawFree(buf);
}
#endif
diff --git a/Modules/python.c b/Modules/python.c
index aaa7fcf..0ad1130 100644
--- a/Modules/python.c
+++ b/Modules/python.c
@@ -43,12 +43,12 @@
fpsetmask(m & ~FP_X_OFL);
#endif
- oldloc = strdup(setlocale(LC_ALL, NULL));
+ oldloc = _PyMem_RawStrdup(setlocale(LC_ALL, NULL));
setlocale(LC_ALL, "");
for (i = 0; i < argc; i++) {
argv_copy[i] = _Py_char2wchar(argv[i], NULL);
if (!argv_copy[i]) {
- free(oldloc);
+ PyMem_RawFree(oldloc);
fprintf(stderr, "Fatal Python error: "
"unable to decode the command line argument #%i\n",
i + 1);
@@ -59,7 +59,7 @@
argv_copy2[argc] = argv_copy[argc] = NULL;
setlocale(LC_ALL, oldloc);
- free(oldloc);
+ PyMem_RawFree(oldloc);
res = Py_Main(argc, argv_copy);
for (i = 0; i < argc; i++) {
PyMem_RawFree(argv_copy2[i]);