Skip to content

Commit e0eec0f

Browse files
committed
gh-110572: Fix leaks in test_*_code in _testcapi/getargs.c
1 parent ea39c87 commit e0eec0f

File tree

1 file changed

+46
-31
lines changed

1 file changed

+46
-31
lines changed

Modules/_testcapi/getargs.c

Lines changed: 46 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -333,68 +333,74 @@ getargs_K(PyObject *self, PyObject *args)
333333
static PyObject *
334334
test_k_code(PyObject *self, PyObject *Py_UNUSED(ignored))
335335
{
336-
PyObject *tuple, *num;
337-
unsigned long value;
338-
339-
tuple = PyTuple_New(1);
336+
PyObject *tuple = PyTuple_New(1);
340337
if (tuple == NULL) {
341338
return NULL;
342339
}
343340

344341
/* a number larger than ULONG_MAX even on 64-bit platforms */
345-
num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16);
342+
PyObject *num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16);
346343
if (num == NULL) {
347-
return NULL;
344+
goto error;
348345
}
349346

350-
value = PyLong_AsUnsignedLongMask(num);
347+
unsigned long value = PyLong_AsUnsignedLongMask(num);
351348
if (value != ULONG_MAX) {
349+
Py_DECREF(num);
352350
PyErr_SetString(PyExc_AssertionError,
353351
"test_k_code: "
354352
"PyLong_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF");
355-
return NULL;
353+
goto error;
356354
}
357355

358356
PyTuple_SET_ITEM(tuple, 0, num);
359357

360358
value = 0;
361359
if (!PyArg_ParseTuple(tuple, "k:test_k_code", &value)) {
362-
return NULL;
360+
goto error;
363361
}
364362
if (value != ULONG_MAX) {
365363
PyErr_SetString(PyExc_AssertionError,
366364
"test_k_code: k code returned wrong value for long 0xFFF...FFF");
367-
return NULL;
365+
goto error;
368366
}
369367

370-
Py_DECREF(num);
368+
Py_DECREF(tuple); // also clears `num`
369+
tuple = PyTuple_New(1);
370+
if (tuple == NULL) {
371+
return NULL;
372+
}
371373
num = PyLong_FromString("-FFFFFFFF000000000000000042", NULL, 16);
372374
if (num == NULL) {
373-
return NULL;
375+
goto error;
374376
}
375377

376378
value = PyLong_AsUnsignedLongMask(num);
377379
if (value != (unsigned long)-0x42) {
378380
PyErr_SetString(PyExc_AssertionError,
379381
"test_k_code: "
380382
"PyLong_AsUnsignedLongMask() returned wrong value for long -0xFFF..000042");
381-
return NULL;
383+
goto error;
382384
}
383385

384386
PyTuple_SET_ITEM(tuple, 0, num);
385387

386388
value = 0;
387389
if (!PyArg_ParseTuple(tuple, "k:test_k_code", &value)) {
388-
return NULL;
390+
goto error;
389391
}
390392
if (value != (unsigned long)-0x42) {
391393
PyErr_SetString(PyExc_AssertionError,
392394
"test_k_code: k code returned wrong value for long -0xFFF..000042");
393-
return NULL;
395+
goto error;
394396
}
395397

396398
Py_DECREF(tuple);
397399
Py_RETURN_NONE;
400+
401+
error:
402+
Py_DECREF(tuple);
403+
return NULL;
398404
}
399405

400406
static PyObject *
@@ -684,51 +690,56 @@ getargs_et_hash(PyObject *self, PyObject *args)
684690
static PyObject *
685691
test_L_code(PyObject *self, PyObject *Py_UNUSED(ignored))
686692
{
687-
PyObject *tuple, *num;
688-
long long value;
689-
690-
tuple = PyTuple_New(1);
693+
PyObject *tuple = PyTuple_New(1);
691694
if (tuple == NULL) {
692695
return NULL;
693696
}
694697

695-
num = PyLong_FromLong(42);
698+
PyObject *num = PyLong_FromLong(42);
696699
if (num == NULL) {
697-
return NULL;
700+
goto error;
698701
}
699702

700703
PyTuple_SET_ITEM(tuple, 0, num);
701704

702-
value = -1;
705+
long long value = -1;
703706
if (!PyArg_ParseTuple(tuple, "L:test_L_code", &value)) {
704-
return NULL;
707+
goto error;
705708
}
706709
if (value != 42) {
707710
PyErr_SetString(PyExc_AssertionError,
708711
"test_L_code: L code returned wrong value for long 42");
709-
return NULL;
712+
goto error;
710713
}
711714

712-
Py_DECREF(num);
715+
Py_DECREF(tuple); // also clears `num`
716+
tuple = PyTuple_New(1);
717+
if (tuple == NULL) {
718+
return NULL;
719+
}
713720
num = PyLong_FromLong(42);
714721
if (num == NULL) {
715-
return NULL;
722+
goto error;
716723
}
717724

718725
PyTuple_SET_ITEM(tuple, 0, num);
719726

720727
value = -1;
721728
if (!PyArg_ParseTuple(tuple, "L:test_L_code", &value)) {
722-
return NULL;
729+
goto error;
723730
}
724731
if (value != 42) {
725732
PyErr_SetString(PyExc_AssertionError,
726733
"test_L_code: L code returned wrong value for int 42");
727-
return NULL;
734+
goto error;
728735
}
729736

730737
Py_DECREF(tuple);
731738
Py_RETURN_NONE;
739+
740+
error:
741+
Py_DECREF(tuple);
742+
return NULL;
732743
}
733744

734745
/* Test the s and z codes for PyArg_ParseTuple.
@@ -745,7 +756,7 @@ test_s_code(PyObject *self, PyObject *Py_UNUSED(ignored))
745756
PyObject *obj = PyUnicode_Decode("t\xeate", strlen("t\xeate"),
746757
"latin-1", NULL);
747758
if (obj == NULL) {
748-
return NULL;
759+
goto error;
749760
}
750761

751762
PyTuple_SET_ITEM(tuple, 0, obj);
@@ -755,15 +766,19 @@ test_s_code(PyObject *self, PyObject *Py_UNUSED(ignored))
755766
*/
756767
char *value;
757768
if (!PyArg_ParseTuple(tuple, "s:test_s_code1", &value)) {
758-
return NULL;
769+
goto error;
759770
}
760771

761772
if (!PyArg_ParseTuple(tuple, "z:test_s_code2", &value)) {
762-
return NULL;
773+
goto error;
763774
}
764775

765776
Py_DECREF(tuple);
766777
Py_RETURN_NONE;
778+
779+
error:
780+
Py_DECREF(tuple);
781+
return NULL;
767782
}
768783

769784
static PyObject *

0 commit comments

Comments
 (0)