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

Skip to content

gh-116502: Fix memory access violation on fatal error with Windows #116503

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 31 additions & 37 deletions Python/pylifecycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -3079,6 +3079,8 @@ _Py_FatalError_PrintExc(PyThreadState *tstate)
static void
fatal_output_debug(const char *msg)
{
assert(msg != NULL);

/* buffer of 256 bytes allocated on the stack */
WCHAR buffer[256 / sizeof(WCHAR)];
size_t buflen = Py_ARRAY_LENGTH(buffer) - 1;
Expand Down Expand Up @@ -3285,7 +3287,7 @@ _Py_DumpExtensionModules(int fd, PyInterpreterState *interp)


static void _Py_NO_RETURN
fatal_error(int fd, int header, const char *prefix, const char *msg,
fatal_error(int fd, const char *prefix, const char *msg,
int status)
{
static int reentrant = 0;
Expand All @@ -3297,20 +3299,18 @@ fatal_error(int fd, int header, const char *prefix, const char *msg,
}
reentrant = 1;

if (header) {
PUTS(fd, "Fatal Python error: ");
if (prefix) {
PUTS(fd, prefix);
PUTS(fd, ": ");
}
if (msg) {
PUTS(fd, msg);
}
else {
PUTS(fd, "<message not set>");
}
PUTS(fd, "\n");
PUTS(fd, "Fatal Python error: ");
if (prefix) {
PUTS(fd, prefix);
PUTS(fd, ": ");
}
if (msg) {
PUTS(fd, msg);
}
else {
PUTS(fd, "<message not set>");
}
PUTS(fd, "\n");

_PyRuntimeState *runtime = &_PyRuntime;
fatal_error_dump_runtime(fd, runtime);
Expand Down Expand Up @@ -3360,7 +3360,12 @@ fatal_error(int fd, int header, const char *prefix, const char *msg,
}

#ifdef MS_WINDOWS
fatal_output_debug(msg);
if (msg) {
fatal_output_debug(msg);
}
else {
fatal_output_debug("<message not set>");
}
#endif /* MS_WINDOWS */

fatal_error_exit(status);
Expand All @@ -3372,44 +3377,33 @@ fatal_error(int fd, int header, const char *prefix, const char *msg,
void _Py_NO_RETURN
Py_FatalError(const char *msg)
{
fatal_error(fileno(stderr), 1, NULL, msg, -1);
fatal_error(fileno(stderr), NULL, msg, -1);
}


void _Py_NO_RETURN
_Py_FatalErrorFunc(const char *func, const char *msg)
{
fatal_error(fileno(stderr), 1, func, msg, -1);
fatal_error(fileno(stderr), func, msg, -1);
}


void _Py_NO_RETURN
_Py_FatalErrorFormat(const char *func, const char *format, ...)
{
static int reentrant = 0;
if (reentrant) {
/* _Py_FatalErrorFormat() caused a second fatal error */
fatal_error_exit(-1);
}
reentrant = 1;

FILE *stream = stderr;
const int fd = fileno(stream);
PUTS(fd, "Fatal Python error: ");
if (func) {
PUTS(fd, func);
PUTS(fd, ": ");
}

va_list vargs;
va_start(vargs, format);
vfprintf(stream, format, vargs);
int length = vsnprintf(NULL, 0, format, vargs);
va_end(vargs);

fputs("\n", stream);
fflush(stream);
char* msg = malloc(length + 1);

va_start(vargs, format);
vsnprintf(msg, length + 1, format, vargs);
va_end(vargs);

fatal_error(fd, 0, NULL, NULL, -1);
fatal_error(fileno(stderr), func, msg, -1);
free(msg);
}


Expand All @@ -3430,7 +3424,7 @@ Py_ExitStatusException(PyStatus status)
exit(status.exitcode);
}
else if (_PyStatus_IS_ERROR(status)) {
fatal_error(fileno(stderr), 1, status.func, status.err_msg, 1);
fatal_error(fileno(stderr), status.func, status.err_msg, 1);
}
else {
Py_FatalError("Py_ExitStatusException() must not be called on success");
Expand Down
Loading