From dbd5ee00cddff48b3708fc59c646263dc8757b96 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 11 Jun 2026 15:42:39 +0200 Subject: [PATCH 1/2] Fix issues reported by cpython-review-toolkit in faulthandler * snprintf() is not async-signal-safe: replace it with _Py_DumpDecimal(). * Fix tid type from 'long' to 'unsigned long'. * Replace PyLong_AsLong() with PyLong_AsInt(). * Avoid unnecessary narrowing cast on _Py_write_noraise() call. --- Modules/faulthandler.c | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/Modules/faulthandler.c b/Modules/faulthandler.c index 7c727d8c2d4ff0e..53c179105d863e9 100644 --- a/Modules/faulthandler.c +++ b/Modules/faulthandler.c @@ -102,7 +102,6 @@ faulthandler_get_fileno(PyObject **file_ptr) { PyObject *result; long fd_long; - int fd; PyObject *file = *file_ptr; if (file == NULL || file == Py_None) { @@ -124,7 +123,7 @@ faulthandler_get_fileno(PyObject **file_ptr) return -1; } } - fd = PyLong_AsInt(file); + int fd = PyLong_AsInt(file); if (fd == -1 && PyErr_Occurred()) return -1; if (fd < 0) { @@ -145,15 +144,16 @@ faulthandler_get_fileno(PyObject **file_ptr) return -1; } - fd = -1; + int fd; if (PyLong_Check(result)) { - fd_long = PyLong_AsLong(result); - if (0 <= fd_long && fd_long < INT_MAX) - fd = (int)fd_long; + fd = PyLong_AsInt(result); + } + else { + fd = -1; } Py_DECREF(result); - if (fd == -1) { + if (fd < 0) { PyErr_SetString(PyExc_RuntimeError, "file.fileno() is not a valid file descriptor"); Py_DECREF(file); @@ -407,10 +407,8 @@ faulthandler_fatal_error(int signum) PUTS(fd, "\n\n"); } else { - char unknown_signum[23] = {0,}; - snprintf(unknown_signum, 23, "%d", signum); PUTS(fd, "Fatal Python error from unexpected signum: "); - PUTS(fd, unknown_signum); + _Py_DumpDecimal(fd, signum); PUTS(fd, "\n\n"); } @@ -713,7 +711,7 @@ faulthandler_thread(void *unused) /* Timeout => dump traceback */ assert(st == PY_LOCK_FAILURE); - (void)_Py_write_noraise(thread.fd, thread.header, (int)thread.header_len); + (void)_Py_write_noraise(thread.fd, thread.header, thread.header_len); errmsg = PyUnstable_DumpTracebackThreads(thread.fd, thread.interp, NULL, thread.max_threads); @@ -1224,7 +1222,7 @@ static PyObject * faulthandler__fatal_error_c_thread_impl(PyObject *module) /*[clinic end generated code: output=101bc8aaf4a5eec1 input=fbdca6fffd639a39]*/ { - long tid; + unsigned long tid; PyThread_type_lock lock; faulthandler_suppress_crash_report(); From 0872afc589d0d908c54b1d8ac6c9ac57fe2fa0de Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 11 Jun 2026 19:21:00 +0200 Subject: [PATCH 2/2] Fix compiler warnings --- Modules/faulthandler.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Modules/faulthandler.c b/Modules/faulthandler.c index 53c179105d863e9..3b0647152ceffed 100644 --- a/Modules/faulthandler.c +++ b/Modules/faulthandler.c @@ -101,7 +101,6 @@ static int faulthandler_get_fileno(PyObject **file_ptr) { PyObject *result; - long fd_long; PyObject *file = *file_ptr; if (file == NULL || file == Py_None) { @@ -1234,7 +1233,7 @@ faulthandler__fatal_error_c_thread_impl(PyObject *module) PyThread_acquire_lock(lock, WAIT_LOCK); tid = PyThread_start_new_thread(faulthandler_fatal_error_thread, lock); - if (tid == -1) { + if (tid == PYTHREAD_INVALID_THREAD_ID) { PyThread_free_lock(lock); PyErr_SetString(PyExc_RuntimeError, "unable to start the thread"); return NULL;