Skip to content

Commit bd2ed06

Browse files
gh-83004: Harden msvcrt further (#103420)
1 parent da2273f commit bd2ed06

File tree

1 file changed

+53
-62
lines changed

1 file changed

+53
-62
lines changed

PC/msvcrtmodule.c

Lines changed: 53 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -564,88 +564,81 @@ static struct PyMethodDef msvcrt_functions[] = {
564564
{NULL, NULL}
565565
};
566566

567-
static void
568-
insertint(PyObject *d, char *name, int value)
569-
{
570-
PyObject *v = PyLong_FromLong((long) value);
571-
if (v == NULL) {
572-
/* Don't bother reporting this error */
573-
PyErr_Clear();
574-
}
575-
else {
576-
PyDict_SetItemString(d, name, v);
577-
Py_DECREF(v);
578-
}
579-
}
580-
581-
static void
582-
insertptr(PyObject *d, char *name, void *value)
567+
static int
568+
insertptr(PyObject *mod, char *name, void *value)
583569
{
584570
PyObject *v = PyLong_FromVoidPtr(value);
585571
if (v == NULL) {
586-
/* Don't bother reporting this error */
587-
PyErr_Clear();
588-
}
589-
else {
590-
PyDict_SetItemString(d, name, v);
591-
Py_DECREF(v);
572+
return -1;
592573
}
574+
int rc = PyModule_AddObjectRef(mod, name, v);
575+
Py_DECREF(v);
576+
return rc;
593577
}
594578

579+
#define INSERTINT(MOD, NAME, VAL) do { \
580+
if (PyModule_AddIntConstant(MOD, NAME, VAL) < 0) { \
581+
return -1; \
582+
} \
583+
} while (0)
584+
585+
#define INSERTPTR(MOD, NAME, PTR) do { \
586+
if (insertptr(MOD, NAME, PTR) < 0) { \
587+
return -1; \
588+
} \
589+
} while (0)
590+
591+
#define INSERTSTR(MOD, NAME, CONST) do { \
592+
if (PyModule_AddStringConstant(MOD, NAME, CONST) < 0) { \
593+
return -1; \
594+
} \
595+
} while (0)
596+
595597
static int
596598
exec_module(PyObject* m)
597599
{
598-
int st;
599-
PyObject *d = PyModule_GetDict(m); // Borrowed ref.
600-
601600
/* constants for the locking() function's mode argument */
602-
insertint(d, "LK_LOCK", _LK_LOCK);
603-
insertint(d, "LK_NBLCK", _LK_NBLCK);
604-
insertint(d, "LK_NBRLCK", _LK_NBRLCK);
605-
insertint(d, "LK_RLCK", _LK_RLCK);
606-
insertint(d, "LK_UNLCK", _LK_UNLCK);
601+
INSERTINT(m, "LK_LOCK", _LK_LOCK);
602+
INSERTINT(m, "LK_NBLCK", _LK_NBLCK);
603+
INSERTINT(m, "LK_NBRLCK", _LK_NBRLCK);
604+
INSERTINT(m, "LK_RLCK", _LK_RLCK);
605+
INSERTINT(m, "LK_UNLCK", _LK_UNLCK);
607606
#ifdef MS_WINDOWS_DESKTOP
608-
insertint(d, "SEM_FAILCRITICALERRORS", SEM_FAILCRITICALERRORS);
609-
insertint(d, "SEM_NOALIGNMENTFAULTEXCEPT", SEM_NOALIGNMENTFAULTEXCEPT);
610-
insertint(d, "SEM_NOGPFAULTERRORBOX", SEM_NOGPFAULTERRORBOX);
611-
insertint(d, "SEM_NOOPENFILEERRORBOX", SEM_NOOPENFILEERRORBOX);
607+
INSERTINT(m, "SEM_FAILCRITICALERRORS", SEM_FAILCRITICALERRORS);
608+
INSERTINT(m, "SEM_NOALIGNMENTFAULTEXCEPT", SEM_NOALIGNMENTFAULTEXCEPT);
609+
INSERTINT(m, "SEM_NOGPFAULTERRORBOX", SEM_NOGPFAULTERRORBOX);
610+
INSERTINT(m, "SEM_NOOPENFILEERRORBOX", SEM_NOOPENFILEERRORBOX);
612611
#endif
613612
#ifdef _DEBUG
614-
insertint(d, "CRT_WARN", _CRT_WARN);
615-
insertint(d, "CRT_ERROR", _CRT_ERROR);
616-
insertint(d, "CRT_ASSERT", _CRT_ASSERT);
617-
insertint(d, "CRTDBG_MODE_DEBUG", _CRTDBG_MODE_DEBUG);
618-
insertint(d, "CRTDBG_MODE_FILE", _CRTDBG_MODE_FILE);
619-
insertint(d, "CRTDBG_MODE_WNDW", _CRTDBG_MODE_WNDW);
620-
insertint(d, "CRTDBG_REPORT_MODE", _CRTDBG_REPORT_MODE);
621-
insertptr(d, "CRTDBG_FILE_STDERR", _CRTDBG_FILE_STDERR);
622-
insertptr(d, "CRTDBG_FILE_STDOUT", _CRTDBG_FILE_STDOUT);
623-
insertptr(d, "CRTDBG_REPORT_FILE", _CRTDBG_REPORT_FILE);
613+
INSERTINT(m, "CRT_WARN", _CRT_WARN);
614+
INSERTINT(m, "CRT_ERROR", _CRT_ERROR);
615+
INSERTINT(m, "CRT_ASSERT", _CRT_ASSERT);
616+
INSERTINT(m, "CRTDBG_MODE_DEBUG", _CRTDBG_MODE_DEBUG);
617+
INSERTINT(m, "CRTDBG_MODE_FILE", _CRTDBG_MODE_FILE);
618+
INSERTINT(m, "CRTDBG_MODE_WNDW", _CRTDBG_MODE_WNDW);
619+
INSERTINT(m, "CRTDBG_REPORT_MODE", _CRTDBG_REPORT_MODE);
620+
INSERTPTR(m, "CRTDBG_FILE_STDERR", _CRTDBG_FILE_STDERR);
621+
INSERTPTR(m, "CRTDBG_FILE_STDOUT", _CRTDBG_FILE_STDOUT);
622+
INSERTPTR(m, "CRTDBG_REPORT_FILE", _CRTDBG_REPORT_FILE);
624623
#endif
625624

625+
#undef INSERTINT
626+
#undef INSERTPTR
627+
626628
/* constants for the crt versions */
627629
#ifdef _VC_ASSEMBLY_PUBLICKEYTOKEN
628-
st = PyModule_AddStringConstant(m, "VC_ASSEMBLY_PUBLICKEYTOKEN",
629-
_VC_ASSEMBLY_PUBLICKEYTOKEN);
630-
if (st < 0) {
631-
return -1;
632-
}
630+
INSERTSTR(m, "VC_ASSEMBLY_PUBLICKEYTOKEN", _VC_ASSEMBLY_PUBLICKEYTOKEN);
633631
#endif
634632
#ifdef _CRT_ASSEMBLY_VERSION
635-
st = PyModule_AddStringConstant(m, "CRT_ASSEMBLY_VERSION",
636-
_CRT_ASSEMBLY_VERSION);
637-
if (st < 0) {
638-
return -1;
639-
}
633+
INSERTSTR(m, "CRT_ASSEMBLY_VERSION", _CRT_ASSEMBLY_VERSION);
640634
#endif
641635
#ifdef __LIBRARIES_ASSEMBLY_NAME_PREFIX
642-
st = PyModule_AddStringConstant(m, "LIBRARIES_ASSEMBLY_NAME_PREFIX",
643-
__LIBRARIES_ASSEMBLY_NAME_PREFIX);
644-
if (st < 0) {
645-
return -1;
646-
}
636+
INSERTSTR(m, "LIBRARIES_ASSEMBLY_NAME_PREFIX",
637+
__LIBRARIES_ASSEMBLY_NAME_PREFIX);
647638
#endif
648639

640+
#undef INSERTSTR
641+
649642
/* constants for the 2010 crt versions */
650643
#if defined(_VC_CRT_MAJOR_VERSION) && defined (_VC_CRT_MINOR_VERSION) && defined(_VC_CRT_BUILD_VERSION) && defined(_VC_CRT_RBUILD_VERSION)
651644
PyObject *version = PyUnicode_FromFormat("%d.%d.%d.%d",
@@ -656,14 +649,12 @@ exec_module(PyObject* m)
656649
if (version == NULL) {
657650
return -1;
658651
}
659-
st = PyModule_AddObjectRef(m, "CRT_ASSEMBLY_VERSION", version);
652+
int st = PyModule_AddObjectRef(m, "CRT_ASSEMBLY_VERSION", version);
660653
Py_DECREF(version);
661654
if (st < 0) {
662655
return -1;
663656
}
664657
#endif
665-
/* make compiler warning quiet if st is unused */
666-
(void)st;
667658

668659
return 0;
669660
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy