8000 Use calloc-based functions, not malloc. (GH-19152) · python/cpython@7668a8b · GitHub
[go: up one dir, main page]

Skip to content

Commit 7668a8b

Browse files
authored
Use calloc-based functions, not malloc. (GH-19152)
1 parent 7dd549e commit 7668a8b

File tree

10 files changed

+17
-38
lines changed

10 files changed

+17
-38
lines changed

Modules/_ctypes/callproc.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,9 @@ _ctypes_get_errobj(int **pspace)
156156
Py_INCREF(errobj);
157157
}
158158
else if (!PyErr_Occurred()) {
159-
void *space = PyMem_Malloc(sizeof(int) * 2);
159+
void *space = PyMem_Calloc(2, sizeof(int));
160160
if (space == NULL)
161161
return NULL;
162-
memset(space, 0, sizeof(int) * 2);
163162
errobj = PyCapsule_New(space, CTYPES_CAPSULE_NAME_PYMEM, pymem_destructor);
164163
if (errobj == NULL) {
165164
PyMem_Free(space);
@@ -1712,10 +1711,9 @@ resize(PyObject *self, PyObject *args)
17121711
if (!_CDataObject_HasExternalBuffer(obj)) {
17131712
/* We are currently using the objects default buffer, but it
17141713
isn't large enough any more. */
1715-
void *ptr = PyMem_Malloc(size);
1714+
void *ptr = PyMem_Calloc(1, size);
17161715
if (ptr == NULL)
17171716
return PyErr_NoMemory();
1718-
memset(ptr, 0, size);
17191717
memmove(ptr, obj->b_ptr, obj->b_size);
17201718
obj->b_ptr = ptr;
17211719
obj->b_size = size;

Modules/_lzmamodule.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,9 @@ parse_filter_spec_lzma(PyObject *spec)
212212
return NULL;
213213
}
214214

215-
options = (lzma_options_lzma *)PyMem_Malloc(sizeof *options);
215+
options = (lzma_options_lzma *)PyMem_Calloc(1, sizeof *options);
216216
if (options == NULL)
217217
return PyErr_NoMemory();
218-
memset(options, 0, sizeof *options);
219218

220219
if (lzma_lzma_preset(options, preset)) {
221220
PyMem_Free(options);
@@ -257,10 +256,9 @@ parse_filter_spec_delta(PyObject *spec)
257256
return NULL;
258257
}
259258

260-
options = (lzma_options_delta *)PyMem_Malloc(sizeof *options);
259+
options = (lzma_options_delta *)PyMem_Calloc(1, sizeof *options);
261260
if (options == NULL)
262261
return PyErr_NoMemory();
263-
memset(options, 0, sizeof *options);
264262
options->type = LZMA_DELTA_TYPE_BYTE;
265263
options->dist = dist;
266264
return options;
@@ -281,10 +279,9 @@ parse_filter_spec_bcj(PyObject *spec)
281279
return NULL;
282280
}
283281

284-
options = (lzma_options_bcj *)PyMem_Malloc(sizeof *options);
282+
options = (lzma_options_bcj *)PyMem_Calloc(1, sizeof *options);
285283
if (options == NULL)
286284
return PyErr_NoMemory();
287-
memset(options, 0, sizeof *options);
288285
options->start_offset = start_offset;
289286
return options;
290287
}

Modules/_winapi.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -603,11 +603,10 @@ _winapi_CreateJunction_impl(PyObject *module, LPWSTR src_path,
603603
sizeof(rdb->MountPointReparseBuffer.PathBuffer) +
604604
/* Two +1's for NUL terminators. */
605605
(prefix_len + print_len + 1 + print_len + 1) * sizeof(WCHAR);
606-
rdb = (_Py_PREPARSE_DATA_BUFFER)PyMem_RawMalloc(rdb_size);
606+
rdb = (_Py_PREPARSE_DATA_BUFFER)PyMem_RawCalloc(1, rdb_size);
607607
if (rdb == NULL)
608608
goto cleanup;
609609

610-
memset(rdb, 0, rdb_size);
611610
rdb->ReparseTag = IO_REPARSE_TAG_MOUNT_POINT;
612611
rdb->ReparseDataLength = rdb_size - _Py_REPARSE_DATA_BUFFER_HEADER_SIZE;
613612
rdb->MountPointReparseBuffer.SubstituteNameOffset = 0;

Modules/binascii.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1311,15 +1311,12 @@ binascii_a2b_qp_impl(PyObject *module, Py_buffer *data, int header)
13111311
datalen = data->len;
13121312

13131313
/* We allocate the output same size as input, this is overkill.
1314-
* The previous implementation used calloc() so we'll zero out the
1315-
* memory here too, since PyMem_Malloc() does not guarantee that.
13161314
*/
1317-
odata = (unsigned char *) PyMem_Malloc(datalen);
1315+
odata = (unsigned char *) PyMem_Calloc(1, datalen);
13181316
if (odata == NULL) {
13191317
PyErr_NoMemory();
13201318
return NULL;
13211319
}
1322-
memset(odata, 0, datalen);
13231320

13241321
in = out = 0;
13251322
while (in < datalen) {
@@ -1499,15 +1496,12 @@ binascii_b2a_qp_impl(PyObject *module, Py_buffer *data, int quotetabs,
14991496
}
15001497

15011498
/* We allocate the output same size as input, this is overkill.
1502-
* The previous implementation used calloc() so we'll zero out the
1503-
* memory here too, since PyMem_Malloc() does not guarantee that.
15041499
*/
1505-
odata = (unsigned char *) PyMem_Malloc(odatalen);
1500+
odata = (unsigned char *) PyMem_Calloc(1, odatalen);
15061501
if (odata == NULL) {
15071502
PyErr_NoMemory();
15081503
return NULL;
15091504
}
1510-
memset(odata, 0, odatalen);
15111505

15121506
in = out = linelen = 0;
15131507
while (in < datalen) {

Modules/faulthandler.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -911,10 +911,9 @@ faulthandler_register_py(PyObject *self,
911911
return NULL;
912912

913913
if (user_signals == NULL) {
914-
user_signals = PyMem_Malloc(NSIG * sizeof(user_signal_t));
914+
user_signals = PyMem_Calloc(NSIG, sizeof(user_signal_t));
915915
if (user_signals == NULL)
916916
return PyErr_NoMemory();
917-
memset(user_signals, 0, NSIG * sizeof(user_signal_t));
918917
}
919918
user = &user_signals[signum];
920919

PC/_testconsole.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,9 @@ _testconsole_write_input_impl(PyObject *module, PyObject *file,
5555
const wchar_t *p = (const wchar_t *)PyBytes_AS_STRING(s);
5656
DWORD size = (DWORD)PyBytes_GET_SIZE(s) / sizeof(wchar_t);
5757

58-
rec = (INPUT_RECORD*)PyMem_Malloc(sizeof(INPUT_RECORD) * size);
58+
rec = (INPUT_RECORD*)PyMem_Calloc(size, sizeof(INPUT_RECORD));
5959
if (!rec)
6060
goto error;
61-
memset(rec, 0, sizeof(INPUT_RECORD) * size);
6261

6362
INPUT_RECORD *prec = rec;
6463
for (DWORD i = 0; i < size; ++i, ++p, ++prec) {

PC/getpathp.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,11 +370,10 @@ getpythonregpath(HKEY keyBase, int skipcore)
370370
/* Allocate a temp array of char buffers, so we only need to loop
371371
reading the registry once
372372
*/
373-
ppPaths = PyMem_RawMalloc( sizeof(WCHAR *) * numKeys );
373+
ppPaths = PyMem_RawCalloc(numKeys, sizeof(WCHAR *));
374374
if (ppPaths==NULL) {
375375
goto done;
376376
}
377-
memset(ppPaths, 0, sizeof(WCHAR *) * numKeys);
378377
/* Loop over all subkeys, allocating a temp sub-buffer. */
379378
for(index=0;index<numKeys;index++) {
380379
WCHAR keyBuf[MAX_PATH+1];

Python/compile.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -561,13 +561,12 @@ compiler_enter_scope(struct compiler *c, identifier name,
561561
struct compiler_unit *u;
562562
basicblock *block;
563563

564-
u = (struct compiler_unit *)PyObject_Malloc(sizeof(
564+
u = (struct compiler_unit *)PyObject_Calloc(1, sizeof(
565565
struct compiler_unit));
566566
if (!u) {
567567
PyErr_NoMemory();
568568
return 0;
569569
}
570-
memset(u, 0, sizeof(struct compiler_unit));
571570
u->u_scope_type = scope_type;
572571
u->u_argcount = 0;
573572
u->u_posonlyargcount = 0;
@@ -770,12 +769,11 @@ compiler_new_block(struct compiler *c)
770769
struct compiler_unit *u;
771770

772771
u = c->u;
773-
b = (basicblock *)PyObject_Malloc(sizeof(basicblock));
772+
b = (basicblock *)PyObject_Calloc(1, sizeof(basicblock));
774773
if (b == NULL) {
775774
PyErr_NoMemory();
776775
return NULL;
777776
}
778-
memset((void *)b, 0, sizeof(basicblock));
779777
/* Extend the singly linked list of blocks with new block. */
780778
b->b_list = u->u_blocks;
781779
u->u_blocks = b;
@@ -812,15 +810,13 @@ compiler_next_instr(basicblock *b)
812810
{
813811
assert(b != NULL);
814812
if (b->b_instr == NULL) {
815-
b->b_instr = (struct instr *)PyObject_Malloc(
816-
sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
813+
b->b_instr = (struct instr *)PyObject_Calloc(
814+
DEFAULT_BLOCK_SIZE, sizeof(struct instr));
817815
if (b->b_instr == NULL) {
818816
PyErr_NoMemory();
819817
return -1;
820818
}
821819
b->b_ialloc = DEFAULT_BLOCK_SIZE;
822-
memset((char *)b->b_instr, 0,
823-
sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
824820
}
825821
else if (b->b_iused == b->b_ialloc) {
826822
struct instr *tmp;

Python/pystate.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,12 +206,11 @@ PyInterpreterState_New(void)
206206
return NULL;
207207
}
208208

209-
PyInterpreterState *interp = PyMem_RawMalloc(sizeof(PyInterpreterState));
209+
PyInterpreterState *interp = PyMem_RawCalloc(1, sizeof(PyInterpreterState));
210210
if (interp == NULL) {
211211
return NULL;
212212
}
213213

214-
memset(interp, 0, sizeof(*interp));
215214
interp->id_refcount = -1;
216215

217216
_PyRuntimeState *runtime = &_PyRuntime;

Python/thread_pthread.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -547,9 +547,8 @@ PyThread_allocate_lock(void)
547547
if (!initialized)
548548
PyThread_init_thread();
549549

550-
lock = (pthread_lock *) PyMem_RawMalloc(sizeof(pthread_lock));
550+
lock = (pthread_lock *) PyMem_RawCalloc(1, sizeof(pthread_lock));
551551
if (lock) {
552-
memset((void *)lock, '\0', sizeof(pthread_lock));
553552
lock->locked = 0;
554553

555554
status = pthread_mutex_init(&lock->mut, NULL);

0 commit comments

Comments
 (0)
0