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

Skip to content

Commit 99d76ac

Browse files
committed
Fix some very unlikely leaks in extensions.
The newly created module may leak if any following initialization fails. Since `import_array` is a macro that hides a `return`-on-failure, that needs to move earlier before the module creation.
1 parent 579475b commit 99d76ac

7 files changed

Lines changed: 29 additions & 14 deletions

File tree

src/_backend_agg_wrapper.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -656,19 +656,21 @@ PyMODINIT_FUNC PyInit__backend_agg(void)
656656
{
657657
PyObject *m;
658658

659+
import_array();
660+
659661
m = PyModule_Create(&moduledef);
660662

661663
if (m == NULL) {
662664
return NULL;
663665
}
664666

665-
import_array();
666-
667667
if (!PyRendererAgg_init_type(m, &PyRendererAggType)) {
668+
Py_DECREF(m);
668669
return NULL;
669670
}
670671

671672
if (!PyBufferRegion_init_type(m, &PyBufferRegionType)) {
673+
Py_DECREF(m);
672674
return NULL;
673675
}
674676

src/_contour_wrapper.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,18 +170,19 @@ PyMODINIT_FUNC PyInit__contour(void)
170170
{
171171
PyObject *m;
172172

173+
import_array();
174+
173175
m = PyModule_Create(&moduledef);
174176

175177
if (m == NULL) {
176178
return NULL;
177179
}
178180

179181
if (!PyQuadContourGenerator_init_type(m, &PyQuadContourGeneratorType)) {
182+
Py_DECREF(m);
180183
return NULL;
181184
}
182185

183-
import_array();
184-
185186
return m;
186187
}
187188

src/_image_wrapper.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,8 @@ PyMODINIT_FUNC PyInit__image(void)
312312
{
313313
PyObject *m;
314314

315+
import_array();
316+
315317
m = PyModule_Create(&moduledef);
316318

317319
if (m == NULL) {
@@ -336,11 +338,10 @@ PyMODINIT_FUNC PyInit__image(void)
336338
PyModule_AddIntConstant(m, "LANCZOS", LANCZOS) ||
337339
PyModule_AddIntConstant(m, "BLACKMAN", BLACKMAN) ||
338340
PyModule_AddIntConstant(m, "_n_interpolation", _n_interpolation)) {
341+
Py_DECREF(m);
339342
return NULL;
340343
}
341344

342-
import_array();
343-
344345
return m;
345346
}
346347

src/_path_wrapper.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -908,14 +908,15 @@ static struct PyModuleDef moduledef = {
908908
PyMODINIT_FUNC PyInit__path(void)
909909
{
910910
PyObject *m;
911+
912+
import_array();
913+
911914
m = PyModule_Create(&moduledef);
912915

913916
if (m == NULL) {
914917
return NULL;
915918
}
916919

917-
import_array();
918-
919920
return m;
920921
}
921922

src/ft2font_wrapper.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1551,21 +1551,26 @@ PyMODINIT_FUNC PyInit_ft2font(void)
15511551
{
15521552
PyObject *m;
15531553

1554+
import_array();
1555+
15541556
m = PyModule_Create(&moduledef);
15551557

15561558
if (m == NULL) {
15571559
return NULL;
15581560
}
15591561

15601562
if (!PyFT2Image_init_type(m, &PyFT2ImageType)) {
1563+
Py_DECREF(m);
15611564
return NULL;
15621565
}
15631566

15641567
if (!PyGlyph_init_type(m, &PyGlyphType)) {
1568+
Py_DECREF(m);
15651569
return NULL;
15661570
}
15671571

15681572
if (!PyFT2Font_init_type(m, &PyFT2FontType)) {
1573+
Py_DECREF(m);
15691574
return NULL;
15701575
}
15711576

@@ -1607,6 +1612,7 @@ PyMODINIT_FUNC PyInit_ft2font(void)
16071612
add_dict_int(d, "LOAD_TARGET_MONO", (unsigned long)FT_LOAD_TARGET_MONO) ||
16081613
add_dict_int(d, "LOAD_TARGET_LCD", (unsigned long)FT_LOAD_TARGET_LCD) ||
16091614
add_dict_int(d, "LOAD_TARGET_LCD_V", (unsigned long)FT_LOAD_TARGET_LCD_V)) {
1615+
Py_DECREF(m);
16101616
return NULL;
16111617
}
16121618

@@ -1615,6 +1621,7 @@ PyMODINIT_FUNC PyInit_ft2font(void)
16151621

16161622
if (error) {
16171623
PyErr_SetString(PyExc_RuntimeError, "Could not initialize the freetype2 library");
1624+
Py_DECREF(m);
16181625
return NULL;
16191626
}
16201627

@@ -1625,16 +1632,16 @@ PyMODINIT_FUNC PyInit_ft2font(void)
16251632
FT_Library_Version(_ft2Library, &major, &minor, &patch);
16261633
sprintf(version_string, "%d.%d.%d", major, minor, patch);
16271634
if (PyModule_AddStringConstant(m, "__freetype_version__", version_string)) {
1635+
Py_DECREF(m);
16281636
return NULL;
16291637
}
16301638
}
16311639

16321640
if (PyModule_AddStringConstant(m, "__freetype_build_type__", STRINGIFY(FREETYPE_BUILD_TYPE))) {
1641+
Py_DECREF(m);
16331642
return NULL;
16341643
}
16351644

1636-
import_array();
1637-
16381645
return m;
16391646
}
16401647

src/qhull_wrap.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,14 +322,14 @@ PyInit__qhull(void)
322322
{
323323
PyObject* m;
324324

325+
import_array();
326+
325327
m = PyModule_Create(&qhull_module);
326328

327329
if (m == NULL) {
328330
return NULL;
329331
}
330332

331-
import_array();
332-
333333
return m;
334334
}
335335

src/tri/_tri_wrapper.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -510,24 +510,27 @@ PyMODINIT_FUNC PyInit__tri(void)
510510
{
511511
PyObject *m;
512512

513+
import_array();
514+
513515
m = PyModule_Create(&moduledef);
514516

515517
if (m == NULL) {
516518
return NULL;
517519
}
518520

519521
if (!PyTriangulation_init_type(m, &PyTriangulationType)) {
522+
Py_DECREF(m);
520523
return NULL;
521524
}
522525
if (!PyTriContourGenerator_init_type(m, &PyTriContourGeneratorType)) {
526+
Py_DECREF(m);
523527
return NULL;
524528
}
525529
if (!PyTrapezoidMapTriFinder_init_type(m, &PyTrapezoidMapTriFinderType)) {
530+
Py_DECREF(m);
526531
return NULL;
527532
}
528533

529-
import_array();
530-
531534
return m;
532535
}
533536

0 commit comments

Comments
 (0)