Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b640491

Browse files
committed
Issue #18203: Replace malloc() with PyMem_Malloc() in Python modules
Replace malloc() with PyMem_Malloc() when the GIL is held, or with PyMem_RawMalloc() otherwise.
1 parent 1a7425f commit b640491

10 files changed

Lines changed: 52 additions & 55 deletions

File tree

Modules/_curses_panel.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ insert_lop(PyCursesPanelObject *po)
117117
{
118118
list_of_panels *new;
119119

120-
if ((new = (list_of_panels *)malloc(sizeof(list_of_panels))) == NULL) {
120+
if ((new = (list_of_panels *)PyMem_Malloc(sizeof(list_of_panels))) == NULL) {
121121
PyErr_NoMemory();
122122
return -1;
123123
}
@@ -136,7 +136,7 @@ remove_lop(PyCursesPanelObject *po)
136136
temp = lop;
137137
if (temp->po == po) {
138138
lop = temp->next;
139-
free(temp);
139+
PyMem_Free(temp);
140140
return;
141141
}
142142
while (temp->next == NULL || temp->next->po != po) {
@@ -148,7 +148,7 @@ remove_lop(PyCursesPanelObject *po)
148148
temp = temp->next;
149149
}
150150
n = temp->next->next;
151-
free(temp->next);
151+
PyMem_Free(temp->next);
152152
temp->next = n;
153153
return;
154154
}

Modules/_cursesmodule.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3406,7 +3406,7 @@ PyInit__curses(void)
34063406
continue;
34073407
if (strncmp(key_n,"KEY_F(",6)==0) {
34083408
char *p1, *p2;
3409-
key_n2 = malloc(strlen(key_n)+1);
3409+
key_n2 = PyMem_Malloc(strlen(key_n)+1);
34103410
if (!key_n2) {
34113411
PyErr_NoMemory();
34123412
break;
@@ -3425,7 +3425,7 @@ PyInit__curses(void)
34253425
key_n2 = key_n;
34263426
SetDictInt(key_n2,key);
34273427
if (key_n2 != key_n)
3428-
free(key_n2);
3428+
PyMem_Free(key_n2);
34293429
}
34303430
#endif
34313431
SetDictInt("KEY_MIN", KEY_MIN);

Modules/_lsprof.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -223,15 +223,15 @@ static ProfilerEntry*
223223
newProfilerEntry(ProfilerObject *pObj, void *key, PyObject *userObj)
224224
{
225225
ProfilerEntry *self;
226-
self = (ProfilerEntry*) malloc(sizeof(ProfilerEntry));
226+
self = (ProfilerEntry*) PyMem_Malloc(sizeof(ProfilerEntry));
227227
if (self == NULL) {
228228
pObj->flags |= POF_NOMEMORY;
229229
return NULL;
230230
}
231231
userObj = normalizeUserObj(userObj);
232232
if (userObj == NULL) {
233233
PyErr_Clear();
234-
free(self);
234+
PyMem_Free(self);
235235
pObj->flags |= POF_NOMEMORY;
236236
return NULL;
237237
}
@@ -264,7 +264,7 @@ static ProfilerSubEntry *
264264
newSubEntry(ProfilerObject *pObj, ProfilerEntry *caller, ProfilerEntry* entry)
265265
{
266266
ProfilerSubEntry *self;
267-
self = (ProfilerSubEntry*) malloc(sizeof(ProfilerSubEntry));
267+
self = (ProfilerSubEntry*) PyMem_Malloc(sizeof(ProfilerSubEntry));
268268
if (self == NULL) {
269269
pObj->flags |= POF_NOMEMORY;
270270
return NULL;
@@ -282,7 +282,7 @@ newSubEntry(ProfilerObject *pObj, ProfilerEntry *caller, ProfilerEntry* entry)
282282
static int freeSubEntry(rotating_node_t *header, void *arg)
283283
{
284284
ProfilerSubEntry *subentry = (ProfilerSubEntry*) header;
285-
free(subentry);
285+
PyMem_Free(subentry);
286286
return 0;
287287
}
288288

@@ -291,7 +291,7 @@ static int freeEntry(rotating_node_t *header, void *arg)
291291
ProfilerEntry *entry = (ProfilerEntry*) header;
292292
RotatingTree_Enum(entry->calls, freeSubEntry, NULL);
293293
Py_DECREF(entry->userObj);
294-
free(entry);
294+
PyMem_Free(entry);
295295
return 0;
296296
}
297297

@@ -301,13 +301,13 @@ static void clearEntries(ProfilerObject *pObj)
301301
pObj->profilerEntries = EMPTY_ROTATING_TREE;
302302
/* release the memory hold by the ProfilerContexts */
303303
if (pObj->currentProfilerContext) {
304-
free(pObj->currentProfilerContext);
304+
PyMem_Free(pObj->currentProfilerContext);
305305
pObj->currentProfilerContext = NULL;
306306
}
307307
while (pObj->freelistProfilerContext) {
308308
ProfilerContext *c = pObj->freelistProfilerContext;
309309
pObj->freelistProfilerContext = c->previous;
310-
free(c);
310+
PyMem_Free(c);
311311
}
312312
pObj->freelistProfilerContext = NULL;
313313
}
@@ -393,7 +393,7 @@ ptrace_enter_call(PyObject *self, void *key, PyObject *userObj)
393393
else {
394394
/* free list exhausted, allocate a new one */
395395
pContext = (ProfilerContext*)
396-
malloc(sizeof(ProfilerContext));
396+
PyMem_Malloc(sizeof(ProfilerContext));
397397
if (pContext == NULL) {
398398
pObj->flags |= POF_NOMEMORY;
399399
goto restorePyerr;
@@ -712,7 +712,7 @@ flush_unmatched(ProfilerObject *pObj)
712712
else
713713
pObj->currentProfilerContext = pContext->previous;
714714
if (pContext)
715-
free(pContext);
715+
PyMem_Free(pContext);
716716
}
717717

718718
}

Modules/_ssl.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3118,7 +3118,7 @@ static int _setup_ssl_threads(void) {
31183118
if (_ssl_locks == NULL) {
31193119
_ssl_locks_count = CRYPTO_num_locks();
31203120
_ssl_locks = (PyThread_type_lock *)
3121-
malloc(sizeof(PyThread_type_lock) * _ssl_locks_count);
3121+
PyMem_Malloc(sizeof(PyThread_type_lock) * _ssl_locks_count);
31223122
if (_ssl_locks == NULL)
31233123
return 0;
31243124
memset(_ssl_locks, 0,
@@ -3130,7 +3130,7 @@ static int _setup_ssl_threads(void) {
31303130
for (j = 0; j < i; j++) {
31313131
PyThread_free_lock(_ssl_locks[j]);
31323132
}
3133-
free(_ssl_locks);
3133+
PyMem_Free(_ssl_locks);
31343134
return 0;
31353135
}
31363136
}

Modules/audioop.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,8 +1137,8 @@ audioop_ratecv(PyObject *self, PyObject *args)
11371137
"not enough memory for output buffer");
11381138
return 0;
11391139
}
1140-
prev_i = (int *) malloc(nchannels * sizeof(int));
1141-
cur_i = (int *) malloc(nchannels * sizeof(int));
1140+
prev_i = (int *) PyMem_Malloc(nchannels * sizeof(int));
1141+
cur_i = (int *) PyMem_Malloc(nchannels * sizeof(int));
11421142
if (prev_i == NULL || cur_i == NULL) {
11431143
(void) PyErr_NoMemory();
11441144
goto exit;
@@ -1257,10 +1257,8 @@ audioop_ratecv(PyObject *self, PyObject *args)
12571257
}
12581258
}
12591259
exit:
1260-
if (prev_i != NULL)
1261-
free(prev_i);
1262-
if (cur_i != NULL)
1263-
free(cur_i);
1260+
PyMem_Free(prev_i);
1261+
PyMem_Free(cur_i);
12641262
return rv;
12651263
}
12661264

Modules/posixmodule.c

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1298,14 +1298,14 @@ win32_wchdir(LPCWSTR path)
12981298
if (!result)
12991299
return FALSE;
13001300
if (result > MAX_PATH+1) {
1301-
new_path = malloc(result * sizeof(wchar_t));
1301+
new_path = PyMem_RawMalloc(result * sizeof(wchar_t));
13021302
if (!new_path) {
13031303
SetLastError(ERROR_OUTOFMEMORY);
13041304
return FALSE;
13051305
}
13061306
result = GetCurrentDirectoryW(result, new_path);
13071307
if (!result) {
1308-
free(new_path);
1308+
PyMem_RawFree(new_path);
13091309
return FALSE;
13101310
}
13111311
}
@@ -1316,7 +1316,7 @@ win32_wchdir(LPCWSTR path)
13161316
env[1] = new_path[0];
13171317
result = SetEnvironmentVariableW(env, new_path);
13181318
if (new_path != _new_path)
1319-
free(new_path);
1319+
PyMem_RawFree(new_path);
13201320
return result;
13211321
}
13221322
#endif
@@ -1497,7 +1497,7 @@ get_target_path(HANDLE hdl, wchar_t **target_path)
14971497
if(!buf_size)
14981498
return FALSE;
14991499

1500-
buf = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t));
1500+
buf = (wchar_t *)PyMem_Malloc((buf_size+1)*sizeof(wchar_t));
15011501
if (!buf) {
15021502
SetLastError(ERROR_OUTOFMEMORY);
15031503
return FALSE;
@@ -1507,12 +1507,12 @@ get_target_path(HANDLE hdl, wchar_t **target_path)
15071507
buf, buf_size, VOLUME_NAME_DOS);
15081508

15091509
if(!result_length) {
1510-
free(buf);
1510+
PyMem_Free(buf);
15111511
return FALSE;
15121512
}
15131513

15141514
if(!CloseHandle(hdl)) {
1515-
free(buf);
1515+
PyMem_Free(buf);
15161516
return FALSE;
15171517
}
15181518

@@ -1603,7 +1603,7 @@ win32_xstat_impl(const char *path, struct win32_stat *result,
16031603
return -1;
16041604

16051605
code = win32_xstat_impl_w(target_path, result, FALSE);
1606-
free(target_path);
1606+
PyMem_Free(target_path);
16071607
return code;
16081608
}
16091609
} else
@@ -1699,7 +1699,7 @@ win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result,
16991699
return -1;
17001700

17011701
code = win32_xstat_impl_w(target_path, result, FALSE);
1702-
free(target_path);
1702+
PyMem_Free(target_path);
17031703
return code;
17041704
}
17051705
} else
@@ -3089,7 +3089,7 @@ posix_getcwd(int use_bytes)
30893089
terminating \0. If the buffer is too small, len includes
30903090
the space needed for the terminator. */
30913091
if (len >= sizeof wbuf/ sizeof wbuf[0]) {
3092-
wbuf2 = malloc(len * sizeof(wchar_t));
3092+
wbuf2 = PyMem_RawMalloc(len * sizeof(wchar_t));
30933093
if (wbuf2)
30943094
len = GetCurrentDirectoryW(len, wbuf2);
30953095
}
@@ -3100,12 +3100,12 @@ posix_getcwd(int use_bytes)
31003100
}
31013101
if (!len) {
31023102
if (wbuf2 != wbuf)
3103-
free(wbuf2);
3103+
PyMem_RawFree(wbuf2);
31043104
return PyErr_SetFromWindowsErr(0);
31053105
}
31063106
resobj = PyUnicode_FromWideChar(wbuf2, len);
31073107
if (wbuf2 != wbuf)
3108-
free(wbuf2);
3108+
PyMem_RawFree(wbuf2);
31093109
return resobj;
31103110
}
31113111

@@ -3289,7 +3289,7 @@ _listdir_windows_no_opendir(path_t *path, PyObject *list)
32893289
len = wcslen(path->wide);
32903290
}
32913291
/* The +5 is so we can append "\\*.*\0" */
3292-
wnamebuf = malloc((len + 5) * sizeof(wchar_t));
3292+
wnamebuf = PyMem_Malloc((len + 5) * sizeof(wchar_t));
32933293
if (!wnamebuf) {
32943294
PyErr_NoMemory();
32953295
goto exit;
@@ -3410,8 +3410,7 @@ _listdir_windows_no_opendir(path_t *path, PyObject *list)
34103410
}
34113411
}
34123412
}
3413-
if (wnamebuf)
3414-
free(wnamebuf);
3413+
PyMem_Free(wnamebuf);
34153414

34163415
return list;
34173416
} /* end of _listdir_windows_no_opendir */
@@ -3575,7 +3574,7 @@ posix__getfullpathname(PyObject *self, PyObject *args)
35753574
Py_ARRAY_LENGTH(woutbuf),
35763575
woutbuf, &wtemp);
35773576
if (result > Py_ARRAY_LENGTH(woutbuf)) {
3578-
woutbufp = malloc(result * sizeof(wchar_t));
3577+
woutbufp = PyMem_Malloc(result * sizeof(wchar_t));
35793578
if (!woutbufp)
35803579
return PyErr_NoMemory();
35813580
result = GetFullPathNameW(wpath, result, woutbufp, &wtemp);
@@ -3585,7 +3584,7 @@ posix__getfullpathname(PyObject *self, PyObject *args)
35853584
else
35863585
v = win32_error_object("GetFullPathNameW", po);
35873586
if (woutbufp != woutbuf)
3588-
free(woutbufp);
3587+
PyMem_Free(woutbufp);
35893588
return v;
35903589
}
35913590
/* Drop the argument parsing error as narrow strings
@@ -3655,7 +3654,7 @@ posix__getfinalpathname(PyObject *self, PyObject *args)
36553654
if(!buf_size)
36563655
return win32_error_object("GetFinalPathNameByHandle", po);
36573656

3658-
target_path = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t));
3657+
target_path = (wchar_t *)PyMem_Malloc((buf_size+1)*sizeof(wchar_t));
36593658
if(!target_path)
36603659
return PyErr_NoMemory();
36613660

@@ -3669,7 +3668,7 @@ posix__getfinalpathname(PyObject *self, PyObject *args)
36693668

36703669
target_path[result_length] = 0;
36713670
result = PyUnicode_FromWideChar(target_path, result_length);
3672-
free(target_path);
3671+
PyMem_Free(target_path);
36733672
return result;
36743673

36753674
} /* end of posix__getfinalpathname */

Modules/pyexpat.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -997,7 +997,7 @@ xmlparse_ExternalEntityParserCreate(xmlparseobject *self, PyObject *args)
997997
PyObject_GC_Track(new_parser);
998998

999999
if (self->buffer != NULL) {
1000-
new_parser->buffer = malloc(new_parser->buffer_size);
1000+
new_parser->buffer = PyMem_Malloc(new_parser->buffer_size);
10011001
if (new_parser->buffer == NULL) {
10021002
Py_DECREF(new_parser);
10031003
return PyErr_NoMemory();
@@ -1014,7 +1014,7 @@ xmlparse_ExternalEntityParserCreate(xmlparseobject *self, PyObject *args)
10141014
for (i = 0; handler_info[i].name != NULL; i++)
10151015
/* do nothing */;
10161016

1017-
new_parser->handlers = malloc(sizeof(PyObject *) * i);
1017+
new_parser->handlers = PyMem_Malloc(sizeof(PyObject *) * i);
10181018
if (!new_parser->handlers) {
10191019
Py_DECREF(new_parser);
10201020
return PyErr_NoMemory();
@@ -1206,7 +1206,7 @@ newxmlparseobject(char *encoding, char *namespace_separator, PyObject *intern)
12061206
for (i = 0; handler_info[i].name != NULL; i++)
12071207
/* do nothing */;
12081208

1209-
self->handlers = malloc(sizeof(PyObject *) * i);
1209+
self->handlers = PyMem_Malloc(sizeof(PyObject *) * i);
12101210
if (!self->handlers) {
12111211
Py_DECREF(self);
12121212
return PyErr_NoMemory();
@@ -1233,11 +1233,11 @@ xmlparse_dealloc(xmlparseobject *self)
12331233
self->handlers[i] = NULL;
12341234
Py_XDECREF(temp);
12351235
}
1236-
free(self->handlers);
1236+
PyMem_Free(self->handlers);
12371237
self->handlers = NULL;
12381238
}
12391239
if (self->buffer != NULL) {
1240-
free(self->buffer);
1240+
PyMem_Free(self->buffer);
12411241
self->buffer = NULL;
12421242
}
12431243
Py_XDECREF(self->intern);
@@ -1437,7 +1437,7 @@ xmlparse_setattro(xmlparseobject *self, PyObject *name, PyObject *v)
14371437
return -1;
14381438
if (b) {
14391439
if (self->buffer == NULL) {
1440-
self->buffer = malloc(self->buffer_size);
1440+
self->buffer = PyMem_Malloc(self->buffer_size);
14411441
if (self->buffer == NULL) {
14421442
PyErr_NoMemory();
14431443
return -1;
@@ -1448,7 +1448,7 @@ xmlparse_setattro(xmlparseobject *self, PyObject *name, PyObject *v)
14481448
else if (self->buffer != NULL) {
14491449
if (flush_character_buffer(self) < 0)
14501450
return -1;
1451-
free(self->buffer);
1451+
PyMem_Free(self->buffer);
14521452
self->buffer = NULL;
14531453
}
14541454
return 0;
@@ -1508,9 +1508,9 @@ xmlparse_setattro(xmlparseobject *self, PyObject *name, PyObject *v)
15081508
flush_character_buffer(self);
15091509
}
15101510
/* free existing buffer */
1511-
free(self->buffer);
1511+
PyMem_Free(self->buffer);
15121512
}
1513-
self->buffer = malloc(new_buffer_size);
1513+
self->buffer = PyMem_Malloc(new_buffer_size);
15141514
if (self->buffer == NULL) {
15151515
PyErr_NoMemory();
15161516
return -1;

Modules/readline.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,12 @@ parse_and_bind(PyObject *self, PyObject *args)
8484
return NULL;
8585
/* Make a copy -- rl_parse_and_bind() modifies its argument */
8686
/* Bernard Herzog */
87-
copy = malloc(1 + strlen(s));
87+
copy = PyMem_Malloc(1 + strlen(s));
8888
if (copy == NULL)
8989
return PyErr_NoMemory();
9090
strcpy(copy, s);
9191
rl_parse_and_bind(copy);
92-
free(copy); /* Free the copy */
92+
PyMem_Free(copy); /* Free the copy */
9393
Py_RETURN_NONE;
9494
}
9595

0 commit comments

Comments
 (0)