Skip to content

Commit 02f8970

Browse files
gh-83004: Harden msvcrt further
1 parent c3cd3d1 commit 02f8970

File tree

1 file changed

+53
-67
lines changed

1 file changed

+53
-67
lines changed

PC/msvcrtmodule.c

Lines changed: 53 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -577,92 +577,80 @@ static struct PyModuleDef msvcrtmodule = {
577577
NULL
578578
};
579579

580-
static void
581-
insertint(PyObject *d, char *name, int value)
582-
{
583-
PyObject *v = PyLong_FromLong((long) value);
584-
if (v == NULL) {
585-
/* Don't bother reporting this error */
586-
PyErr_Clear();
587-
}
588-
else {
589-
PyDict_SetItemString(d, name, v);
590-
Py_DECREF(v);
591-
}
592-
}
593-
594-
static void
595-
insertptr(PyObject *d, char *name, void *value)
596-
{
597-
PyObject *v = PyLong_FromVoidPtr(value);
598-
if (v == NULL) {
599-
/* Don't bother reporting this error */
600-
PyErr_Clear();
601-
}
602-
else {
603-
PyDict_SetItemString(d, name, v);
604-
Py_DECREF(v);
605-
}
606-
}
580+
#define INSERTINT(MOD, NAME, VAL) do { \
581+
if (PyModule_AddIntConstant(MOD, NAME, VAL) < 0) { \
582+
return -1; \
583+
} \
584+
} while (0)
585+
586+
#define INSERTPTR(MOD, NAME, PTR) do { \
587+
PyObject *v = PyLong_FromVoidPtr(PTR); \
588+
if (v == NULL) { \
589+
return -1; \
590+
} \
591+
int rc = PyModule_AddObjectRef(MOD, NAME, v); \
592+
Py_DECREF(v); \
593+
if (rc < 0) { \
594+
return -1; \
595+
} \
596+
} while (0)
597+
598+
#define INSERTSTR(MOD, NAME, CONST) do { \
599+
if (PyModule_AddStringConstant(MOD, NAME, CONST) < 0) { \
600+
return -1; \
601+
} \
602+
} while (0)
607603

608604
PyMODINIT_FUNC
609605
PyInit_msvcrt(void)
610606
{
611-
int st;
612607
PyObject *m = PyModule_Create(&msvcrtmodule);
613608
if (m == NULL) {
614609
return NULL;
615610
}
616-
PyObject *d = PyModule_GetDict(m); // Borrowed ref.
617611

618612
/* constants for the locking() function's mode argument */
619-
insertint(d, "LK_LOCK", _LK_LOCK);
620-
insertint(d, "LK_NBLCK", _LK_NBLCK);
621-
insertint(d, "LK_NBRLCK", _LK_NBRLCK);
622-
insertint(d, "LK_RLCK", _LK_RLCK);
623-
insertint(d, "LK_UNLCK", _LK_UNLCK);
613+
INSERTINT(m, "LK_LOCK", _LK_LOCK);
614+
INSERTINT(m, "LK_NBLCK", _LK_NBLCK);
615+
INSERTINT(m, "LK_NBRLCK", _LK_NBRLCK);
616+
INSERTINT(m, "LK_RLCK", _LK_RLCK);
617+
INSERTINT(m, "LK_UNLCK", _LK_UNLCK);
624618
#ifdef MS_WINDOWS_DESKTOP
625-
insertint(d, "SEM_FAILCRITICALERRORS", SEM_FAILCRITICALERRORS);
626-
insertint(d, "SEM_NOALIGNMENTFAULTEXCEPT", SEM_NOALIGNMENTFAULTEXCEPT);
627-
insertint(d, "SEM_NOGPFAULTERRORBOX", SEM_NOGPFAULTERRORBOX);
628-
insertint(d, "SEM_NOOPENFILEERRORBOX", SEM_NOOPENFILEERRORBOX);
619+
INSERTINT(m, "SEM_FAILCRITICALERRORS", SEM_FAILCRITICALERRORS);
620+
INSERTINT(m, "SEM_NOALIGNMENTFAULTEXCEPT", SEM_NOALIGNMENTFAULTEXCEPT);
621+
INSERTINT(m, "SEM_NOGPFAULTERRORBOX", SEM_NOGPFAULTERRORBOX);
622+
INSERTINT(m, "SEM_NOOPENFILEERRORBOX", SEM_NOOPENFILEERRORBOX);
629623
#endif
630624
#ifdef _DEBUG
631-
insertint(d, "CRT_WARN", _CRT_WARN);
632-
insertint(d, "CRT_ERROR", _CRT_ERROR);
633-
insertint(d, "CRT_ASSERT", _CRT_ASSERT);
634-
insertint(d, "CRTDBG_MODE_DEBUG", _CRTDBG_MODE_DEBUG);
635-
insertint(d, "CRTDBG_MODE_FILE", _CRTDBG_MODE_FILE);
636-
insertint(d, "CRTDBG_MODE_WNDW", _CRTDBG_MODE_WNDW);
637-
insertint(d, "CRTDBG_REPORT_MODE", _CRTDBG_REPORT_MODE);
638-
insertptr(d, "CRTDBG_FILE_STDERR", _CRTDBG_FILE_STDERR);
639-
insertptr(d, "CRTDBG_FILE_STDOUT", _CRTDBG_FILE_STDOUT);
640-
insertptr(d, "CRTDBG_REPORT_FILE", _CRTDBG_REPORT_FILE);
625+
INSERTINT(m, "CRT_WARN", _CRT_WARN);
626+
INSERTINT(m, "CRT_ERROR", _CRT_ERROR);
627+
INSERTINT(m, "CRT_ASSERT", _CRT_ASSERT);
628+
INSERTINT(m, "CRTDBG_MODE_DEBUG", _CRTDBG_MODE_DEBUG);
629+
INSERTINT(m, "CRTDBG_MODE_FILE", _CRTDBG_MODE_FILE);
630+
INSERTINT(m, "CRTDBG_MODE_WNDW", _CRTDBG_MODE_WNDW);
631+
INSERTINT(m, "CRTDBG_REPORT_MODE", _CRTDBG_REPORT_MODE);
632+
INSERTPTR(m, "CRTDBG_FILE_STDERR", _CRTDBG_FILE_STDERR);
633+
INSERTPTR(m, "CRTDBG_FILE_STDOUT", _CRTDBG_FILE_STDOUT);
634+
INSERTPTR(m, "CRTDBG_REPORT_FILE", _CRTDBG_REPORT_FILE);
641635
#endif
642636

637+
#undef INSERTINT
638+
#undef INSERTPTR
639+
643640
/* constants for the crt versions */
644641
#ifdef _VC_ASSEMBLY_PUBLICKEYTOKEN
645-
st = PyModule_AddStringConstant(m, "VC_ASSEMBLY_PUBLICKEYTOKEN",
646-
_VC_ASSEMBLY_PUBLICKEYTOKEN);
647-
if (st < 0) {
648-
goto error;
649-
}
642+
INSERTSTR(m, "VC_ASSEMBLY_PUBLICKEYTOKEN", _VC_ASSEMBLY_PUBLICKEYTOKEN);
650643
#endif
651644
#ifdef _CRT_ASSEMBLY_VERSION
652-
st = PyModule_AddStringConstant(m, "CRT_ASSEMBLY_VERSION",
653-
_CRT_ASSEMBLY_VERSION);
654-
if (st < 0) {
655-
goto error;
656-
}
645+
INSERTSTR(m, "CRT_ASSEMBLY_VERSION", _CRT_ASSEMBLY_VERSION);
657646
#endif
658647
#ifdef __LIBRARIES_ASSEMBLY_NAME_PREFIX
659-
st = PyModule_AddStringConstant(m, "LIBRARIES_ASSEMBLY_NAME_PREFIX",
660-
__LIBRARIES_ASSEMBLY_NAME_PREFIX);
661-
if (st < 0) {
662-
goto error;
663-
}
648+
INSERTSTR(m, "LIBRARIES_ASSEMBLY_NAME_PREFIX",
649+
__LIBRARIES_ASSEMBLY_NAME_PREFIX);
664650
#endif
665651

652+
#undef INSERTSTR
653+
666654
/* constants for the 2010 crt versions */
667655
#if defined(_VC_CRT_MAJOR_VERSION) && defined (_VC_CRT_MINOR_VERSION) && defined(_VC_CRT_BUILD_VERSION) && defined(_VC_CRT_RBUILD_VERSION)
668656
PyObject *version = PyUnicode_FromFormat("%d.%d.%d.%d",
@@ -673,14 +661,12 @@ PyInit_msvcrt(void)
673661
if (version == NULL) {
674662
goto error;
675663
}
676-
st = PyModule_AddObjectRef(m, "CRT_ASSEMBLY_VERSION", version);
664+
int rc = PyModule_AddObjectRef(m, "CRT_ASSEMBLY_VERSION", version);
677665
Py_DECREF(version);
678-
if (st < 0) {
666+
if (rc < 0) {
679667
goto error;
680668
}
681669
#endif
682-
/* make compiler warning quiet if st is unused */
683-
(void)st;
684670

685671
return m;
686672

0 commit comments

Comments
 (0)