Skip to content

Commit 7a6f3bc

Browse files
authored
bpo-1635741: Fix refleak in _locale init error handling (GH-19307)
1 parent 45f7008 commit 7a6f3bc

File tree

1 file changed

+26
-15
lines changed

1 file changed

+26
-15
lines changed

Modules/_localemodule.c

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -726,47 +726,58 @@ static struct PyMethodDef PyLocale_Methods[] = {
726726
};
727727

728728
static int
729-
_locale_exec(PyObject *m)
729+
_locale_exec(PyObject *module)
730730
{
731731
#ifdef HAVE_LANGINFO_H
732732
int i;
733733
#endif
734+
#define ADD_INT(module, value) \
735+
do { \
736+
if (PyModule_AddIntConstant(module, #value, value) < 0) { \
737+
return -1; \
738+
} \
739+
} while (0)
734740

735-
PyModule_AddIntMacro(m, LC_CTYPE);
736-
PyModule_AddIntMacro(m, LC_TIME);
737-
PyModule_AddIntMacro(m, LC_COLLATE);
738-
PyModule_AddIntMacro(m, LC_MONETARY);
741+
ADD_INT(module, LC_CTYPE);
742+
ADD_INT(module, LC_TIME);
743+
ADD_INT(module, LC_COLLATE);
744+
ADD_INT(module, LC_MONETARY);
739745

740746
#ifdef LC_MESSAGES
741-
PyModule_AddIntMacro(m, LC_MESSAGES);
747+
ADD_INT(module, LC_MESSAGES);
742748
#endif /* LC_MESSAGES */
743749

744-
PyModule_AddIntMacro(m, LC_NUMERIC);
745-
PyModule_AddIntMacro(m, LC_ALL);
746-
PyModule_AddIntMacro(m, CHAR_MAX);
750+
ADD_INT(module, LC_NUMERIC);
751+
ADD_INT(module, LC_ALL);
752+
ADD_INT(module, CHAR_MAX);
747753

748-
_locale_state *state = get_locale_state(m);
754+
_locale_state *state = get_locale_state(module);
749755
state->Error = PyErr_NewException("locale.Error", NULL, NULL);
750756
if (state->Error == NULL) {
751757
return -1;
752758
}
753-
Py_INCREF(get_locale_state(m)->Error);
754-
if (PyModule_AddObject(m, "Error", get_locale_state(m)->Error) < 0) {
755-
Py_DECREF(get_locale_state(m)->Error);
759+
Py_INCREF(get_locale_state(module)->Error);
760+
if (PyModule_AddObject(module, "Error", get_locale_state(module)->Error) < 0) {
761+
Py_DECREF(get_locale_state(module)->Error);
756762
return -1;
757763
}
758764

759765
#ifdef HAVE_LANGINFO_H
760766
for (i = 0; langinfo_constants[i].name; i++) {
761-
PyModule_AddIntConstant(m, langinfo_constants[i].name,
762-
langinfo_constants[i].value);
767+
if (PyModule_AddIntConstant(module,
768+
langinfo_constants[i].name,
769+
langinfo_constants[i].value) < 0) {
770+
return -1;
771+
}
763772
}
764773
#endif
765774

766775
if (PyErr_Occurred()) {
767776
return -1;
768777
}
769778
return 0;
779+
780+
#undef ADD_INT
770781
}
771782

772783
static struct PyModuleDef_Slot _locale_slots[] = {

0 commit comments

Comments
 (0)