Skip to content

Commit 474eedf

Browse files
eduardo-elizondoencukou
authored andcommitted
bpo-34784: Fix PyStructSequence_NewType with heap-allocated StructSequence (GH-9665)
1 parent 1a6be91 commit 474eedf

File tree

5 files changed

+239
-137
lines changed

5 files changed

+239
-137
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix the implementation of PyStructSequence_NewType in order to create heap
2+
allocated StructSequences.

Modules/_testcapimodule.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3313,6 +3313,31 @@ test_decref_doesnt_leak(PyObject *ob, PyObject *Py_UNUSED(ignored))
33133313
Py_RETURN_NONE;
33143314
}
33153315

3316+
static PyObject *
3317+
test_structseq_newtype_doesnt_leak(PyObject *Py_UNUSED(self),
3318+
PyObject *Py_UNUSED(args))
3319+
{
3320+
PyStructSequence_Desc descr;
3321+
PyStructSequence_Field descr_fields[3];
3322+
3323+
descr_fields[0] = (PyStructSequence_Field){"foo", "foo value"};
3324+
descr_fields[1] = (PyStructSequence_Field){NULL, "some hidden value"};
3325+
descr_fields[2] = (PyStructSequence_Field){0, NULL};
3326+
3327+
descr.name = "_testcapi.test_descr";
3328+
descr.doc = "This is used to test for memory leaks in NewType";
3329+
descr.fields = descr_fields;
3330+
descr.n_in_sequence = 1;
3331+
3332+
PyTypeObject* structseq_type = PyStructSequence_NewType(&descr);
3333+
assert(structseq_type != NULL);
3334+
assert(PyType_Check(structseq_type));
3335+
assert(PyType_FastSubclass(structseq_type, Py_TPFLAGS_TUPLE_SUBCLASS));
3336+
Py_DECREF(structseq_type);
3337+
3338+
Py_RETURN_NONE;
3339+
}
3340+
33163341
static PyObject *
33173342
test_incref_decref_API(PyObject *ob, PyObject *Py_UNUSED(ignored))
33183343
{
@@ -4721,6 +4746,8 @@ static PyMethodDef TestMethods[] = {
47214746
{"test_incref_doesnt_leak", test_incref_doesnt_leak, METH_NOARGS},
47224747
{"test_xdecref_doesnt_leak",test_xdecref_doesnt_leak, METH_NOARGS},
47234748
{"test_decref_doesnt_leak", test_decref_doesnt_leak, METH_NOARGS},
4749+
{"test_structseq_newtype_doesnt_leak",
4750+
test_structseq_newtype_doesnt_leak, METH_NOARGS},
47244751
{"test_incref_decref_API", test_incref_decref_API, METH_NOARGS},
47254752
{"test_long_and_overflow", test_long_and_overflow, METH_NOARGS},
47264753
{"test_long_as_double", test_long_as_double, METH_NOARGS},

Modules/posixmodule.c

Lines changed: 53 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1948,14 +1948,14 @@ static PyStructSequence_Desc waitid_result_desc = {
19481948
waitid_result_fields,
19491949
5
19501950
};
1951-
static PyTypeObject WaitidResultType;
1951+
static PyTypeObject* WaitidResultType;
19521952
#endif
19531953

19541954
static int initialized;
1955-
static PyTypeObject StatResultType;
1956-
static PyTypeObject StatVFSResultType;
1955+
static PyTypeObject* StatResultType;
1956+
static PyTypeObject* StatVFSResultType;
19571957
#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
1958-
static PyTypeObject SchedParamType;
1958+
static PyTypeObject* SchedParamType;
19591959
#endif
19601960
static newfunc structseq_new;
19611961

@@ -2029,7 +2029,7 @@ static PyObject*
20292029
_pystat_fromstructstat(STRUCT_STAT *st)
20302030
{
20312031
unsigned long ansec, mnsec, cnsec;
2032-
PyObject *v = PyStructSequence_New(&StatResultType);
2032+
PyObject *v = PyStructSequence_New(StatResultType);
20332033
if (v == NULL)
20342034
return NULL;
20352035

@@ -4407,7 +4407,7 @@ static PyStructSequence_Desc uname_result_desc = {
44074407
5
44084408
};
44094409

4410-
static PyTypeObject UnameResultType;
4410+
static PyTypeObject* UnameResultType;
44114411

44124412

44134413
#ifdef HAVE_UNAME
@@ -4435,7 +4435,7 @@ os_uname_impl(PyObject *module)
44354435
if (res < 0)
44364436
return posix_error();
44374437

4438-
value = PyStructSequence_New(&UnameResultType);
4438+
value = PyStructSequence_New(UnameResultType);
44394439
if (value == NULL)
44404440
return NULL;
44414441

@@ -5941,7 +5941,7 @@ os_sched_getscheduler_impl(PyObject *module, pid_t pid)
59415941

59425942
#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
59435943
/*[clinic input]
5944-
class os.sched_param "PyObject *" "&SchedParamType"
5944+
class os.sched_param "PyObject *" "SchedParamType"
59455945
59465946
@classmethod
59475947
os.sched_param.__new__
@@ -5954,7 +5954,7 @@ Current has only one field: sched_priority");
59545954

59555955
static PyObject *
59565956
os_sched_param_impl(PyTypeObject *type, PyObject *sched_priority)
5957-
/*[clinic end generated code: output=48f4067d60f48c13 input=73a4c22f7071fc62]*/
5957+
/*[clinic end generated code: output=48f4067d60f48c13 input=ab4de35a9a7811f2]*/
59585958
{
59595959
PyObject *res;
59605960

@@ -5986,7 +5986,7 @@ convert_sched_param(PyObject *param, struct sched_param *res)
59865986
{
59875987
long priority;
59885988

5989-
if (Py_TYPE(param) != &SchedParamType) {
5989+
if (Py_TYPE(param) != SchedParamType) {
59905990
PyErr_SetString(PyExc_TypeError, "must have a sched_param object");
59915991
return 0;
59925992
}
@@ -6057,7 +6057,7 @@ os_sched_getparam_impl(PyObject *module, pid_t pid)
60576057

60586058
if (sched_getparam(pid, &param))
60596059
return posix_error();
6060-
result = PyStructSequence_New(&SchedParamType);
6060+
result = PyStructSequence_New(SchedParamType);
60616061
if (!result)
60626062
return NULL;
60636063
priority = PyLong_FromLong(param.sched_priority);
@@ -7422,7 +7422,7 @@ os_waitid_impl(PyObject *module, idtype_t idtype, id_t id, int options)
74227422
if (si.si_pid == 0)
74237423
Py_RETURN_NONE;
74247424

7425-
result = PyStructSequence_New(&WaitidResultType);
7425+
result = PyStructSequence_New(WaitidResultType);
74267426
if (!result)
74277427
return NULL;
74287428

@@ -7857,7 +7857,7 @@ static PyStructSequence_Desc times_result_desc = {
78577857
5
78587858
};
78597859

7860-
static PyTypeObject TimesResultType;
7860+
static PyTypeObject* TimesResultType;
78617861

78627862
#ifdef MS_WINDOWS
78637863
#define HAVE_TIMES /* mandatory, for the method table */
@@ -7870,7 +7870,7 @@ build_times_result(double user, double system,
78707870
double children_user, double children_system,
78717871
double elapsed)
78727872
{
7873-
PyObject *value = PyStructSequence_New(&TimesResultType);
7873+
PyObject *value = PyStructSequence_New(TimesResultType);
78747874
if (value == NULL)
78757875
return NULL;
78767876

@@ -9950,7 +9950,7 @@ os_WSTOPSIG_impl(PyObject *module, int status)
99509950

99519951
static PyObject*
99529952
_pystatvfs_fromstructstatvfs(struct statvfs st) {
9953-
PyObject *v = PyStructSequence_New(&StatVFSResultType);
9953+
PyObject *v = PyStructSequence_New(StatVFSResultType);
99549954
if (v == NULL)
99559955
return NULL;
99569956

@@ -11703,7 +11703,7 @@ os_urandom_impl(PyObject *module, Py_ssize_t size)
1170311703

1170411704
/* Terminal size querying */
1170511705

11706-
static PyTypeObject TerminalSizeType;
11706+
static PyTypeObject* TerminalSizeType;
1170711707

1170811708
PyDoc_STRVAR(TerminalSize_docstring,
1170911709
"A tuple of (columns, lines) for holding terminal window size");
@@ -11795,7 +11795,7 @@ get_terminal_size(PyObject *self, PyObject *args)
1179511795
}
1179611796
#endif /* TERMSIZE_USE_CONIO */
1179711797

11798-
termsize = PyStructSequence_New(&TerminalSizeType);
11798+
termsize = PyStructSequence_New(TerminalSizeType);
1179911799
if (termsize == NULL)
1180011800
return NULL;
1180111801
PyStructSequence_SET_ITEM(termsize, 0, PyLong_FromLong(columns));
@@ -13912,23 +13912,28 @@ INITFUNC(void)
1391213912
if (!initialized) {
1391313913
#if defined(HAVE_WAITID) && !defined(__APPLE__)
1391413914
waitid_result_desc.name = MODNAME ".waitid_result";
13915-
if (PyStructSequence_InitType2(&WaitidResultType, &waitid_result_desc) < 0)
13915+
WaitidResultType = PyStructSequence_NewType(&waitid_result_desc);
13916+
if (WaitidResultType == NULL) {
1391613917
return NULL;
13918+
}
1391713919
#endif
1391813920

1391913921
stat_result_desc.name = "os.stat_result"; /* see issue #19209 */
1392013922
stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
1392113923
stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
1392213924
stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
13923-
if (PyStructSequence_InitType2(&StatResultType, &stat_result_desc) < 0)
13925+
StatResultType = PyStructSequence_NewType(&stat_result_desc);
13926+
if (StatResultType == NULL) {
1392413927
return NULL;
13925-
structseq_new = StatResultType.tp_new;
13926-
StatResultType.tp_new = statresult_new;
13928+
}
13929+
structseq_new = StatResultType->tp_new;
13930+
StatResultType->tp_new = statresult_new;
1392713931

1392813932
statvfs_result_desc.name = "os.statvfs_result"; /* see issue #19209 */
13929-
if (PyStructSequence_InitType2(&StatVFSResultType,
13930-
&statvfs_result_desc) < 0)
13933+
StatVFSResultType = PyStructSequence_NewType(&statvfs_result_desc);
13934+
if (StatVFSResultType == NULL) {
1393113935
return NULL;
13936+
}
1393213937
#ifdef NEED_TICKS_PER_SECOND
1393313938
# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
1393413939
ticks_per_second = sysconf(_SC_CLK_TCK);
@@ -13941,15 +13946,18 @@ INITFUNC(void)
1394113946

1394213947
#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
1394313948
sched_param_desc.name = MODNAME ".sched_param";
13944-
if (PyStructSequence_InitType2(&SchedParamType, &sched_param_desc) < 0)
13949+
SchedParamType = PyStructSequence_NewType(&sched_param_desc);
13950+
if (SchedParamType == NULL) {
1394513951
return NULL;
13946-
SchedParamType.tp_new = os_sched_param;
13952+
}
13953+
SchedParamType->tp_new = os_sched_param;
1394713954
#endif
1394813955

1394913956
/* initialize TerminalSize_info */
13950-
if (PyStructSequence_InitType2(&TerminalSizeType,
13951-
&TerminalSize_desc) < 0)
13957+
TerminalSizeType = PyStructSequence_NewType(&TerminalSize_desc);
13958+
if (TerminalSizeType == NULL) {
1395213959
return NULL;
13960+
}
1395313961

1395413962
/* initialize scandir types */
1395513963
if (PyType_Ready(&ScandirIteratorType) < 0)
@@ -13958,29 +13966,33 @@ INITFUNC(void)
1395813966
return NULL;
1395913967
}
1396013968
#if defined(HAVE_WAITID) && !defined(__APPLE__)
13961-
Py_INCREF((PyObject*) &WaitidResultType);
13962-
PyModule_AddObject(m, "waitid_result", (PyObject*) &WaitidResultType);
13969+
Py_INCREF((PyObject*) WaitidResultType);
13970+
PyModule_AddObject(m, "waitid_result", (PyObject*) WaitidResultType);
1396313971
#endif
13964-
Py_INCREF((PyObject*) &StatResultType);
13965-
PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
13966-
Py_INCREF((PyObject*) &StatVFSResultType);
13972+
Py_INCREF((PyObject*) StatResultType);
13973+
PyModule_AddObject(m, "stat_result", (PyObject*) StatResultType);
13974+
Py_INCREF((PyObject*) StatVFSResultType);
1396713975
PyModule_AddObject(m, "statvfs_result",
13968-
(PyObject*) &StatVFSResultType);
13976+
(PyObject*) StatVFSResultType);
1396913977

1397013978
#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
13971-
Py_INCREF(&SchedParamType);
13972-
PyModule_AddObject(m, "sched_param", (PyObject *)&SchedParamType);
13979+
Py_INCREF(SchedParamType);
13980+
PyModule_AddObject(m, "sched_param", (PyObject *)SchedParamType);
1397313981
#endif
1397413982

1397513983
times_result_desc.name = MODNAME ".times_result";
13976-
if (PyStructSequence_InitType2(&TimesResultType, &times_result_desc) < 0)
13984+
TimesResultType = PyStructSequence_NewType(&times_result_desc);
13985+
if (TimesResultType == NULL) {
1397713986
return NULL;
13978-
PyModule_AddObject(m, "times_result", (PyObject *)&TimesResultType);
13987+
}
13988+
PyModule_AddObject(m, "times_result", (PyObject *)TimesResultType);
1397913989

1398013990
uname_result_desc.name = MODNAME ".uname_result";
13981-
if (PyStructSequence_InitType2(&UnameResultType, &uname_result_desc) < 0)
13991+
UnameResultType = PyStructSequence_NewType(&uname_result_desc);
13992+
if (UnameResultType == NULL) {
1398213993
return NULL;
13983-
PyModule_AddObject(m, "uname_result", (PyObject *)&UnameResultType);
13994+
}
13995+
PyModule_AddObject(m, "uname_result", (PyObject *)UnameResultType);
1398413996

1398513997
#ifdef __APPLE__
1398613998
/*
@@ -14020,8 +14032,8 @@ INITFUNC(void)
1402014032

1402114033
#endif /* __APPLE__ */
1402214034

14023-
Py_INCREF(&TerminalSizeType);
14024-
PyModule_AddObject(m, "terminal_size", (PyObject*) &TerminalSizeType);
14035+
Py_INCREF(TerminalSizeType);
14036+
PyModule_AddObject(m, "terminal_size", (PyObject*)TerminalSizeType);
1402514037

1402614038
billion = PyLong_FromLong(1000000000);
1402714039
if (!billion)

0 commit comments

Comments
 (0)