Skip to content

Commit b6a2eef

Browse files
authored
Fix issues reported by cpython-review-toolkit in faulthandler (#151341)
* 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.
1 parent 552ce57 commit b6a2eef

1 file changed

Lines changed: 11 additions & 14 deletions

File tree

Modules/faulthandler.c

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,6 @@ static int
101101
faulthandler_get_fileno(PyObject **file_ptr)
102102
{
103103
PyObject *result;
104-
long fd_long;
105-
int fd;
106104
PyObject *file = *file_ptr;
107105

108106
if (file == NULL || file == Py_None) {
@@ -124,7 +122,7 @@ faulthandler_get_fileno(PyObject **file_ptr)
124122
return -1;
125123
}
126124
}
127-
fd = PyLong_AsInt(file);
125+
int fd = PyLong_AsInt(file);
128126
if (fd == -1 && PyErr_Occurred())
129127
return -1;
130128
if (fd < 0) {
@@ -145,15 +143,16 @@ faulthandler_get_fileno(PyObject **file_ptr)
145143
return -1;
146144
}
147145

148-
fd = -1;
146+
int fd;
149147
if (PyLong_Check(result)) {
150-
fd_long = PyLong_AsLong(result);
151-
if (0 <= fd_long && fd_long < INT_MAX)
152-
fd = (int)fd_long;
148+
fd = PyLong_AsInt(result);
149+
}
150+
else {
151+
fd = -1;
153152
}
154153
Py_DECREF(result);
155154

156-
if (fd == -1) {
155+
if (fd < 0) {
157156
PyErr_SetString(PyExc_RuntimeError,
158157
"file.fileno() is not a valid file descriptor");
159158
Py_DECREF(file);
@@ -407,10 +406,8 @@ faulthandler_fatal_error(int signum)
407406
PUTS(fd, "\n\n");
408407
}
409408
else {
410-
char unknown_signum[23] = {0,};
411-
snprintf(unknown_signum, 23, "%d", signum);
412409
PUTS(fd, "Fatal Python error from unexpected signum: ");
413-
PUTS(fd, unknown_signum);
410+
_Py_DumpDecimal(fd, signum);
414411
PUTS(fd, "\n\n");
415412
}
416413

@@ -713,7 +710,7 @@ faulthandler_thread(void *unused)
713710
/* Timeout => dump traceback */
714711
assert(st == PY_LOCK_FAILURE);
715712

716-
(void)_Py_write_noraise(thread.fd, thread.header, (int)thread.header_len);
713+
(void)_Py_write_noraise(thread.fd, thread.header, thread.header_len);
717714

718715
errmsg = PyUnstable_DumpTracebackThreads(thread.fd, thread.interp, NULL,
719716
thread.max_threads);
@@ -1224,7 +1221,7 @@ static PyObject *
12241221
faulthandler__fatal_error_c_thread_impl(PyObject *module)
12251222
/*[clinic end generated code: output=101bc8aaf4a5eec1 input=fbdca6fffd639a39]*/
12261223
{
1227-
long tid;
1224+
unsigned long tid;
12281225
PyThread_type_lock lock;
12291226

12301227
faulthandler_suppress_crash_report();
@@ -1236,7 +1233,7 @@ faulthandler__fatal_error_c_thread_impl(PyObject *module)
12361233
PyThread_acquire_lock(lock, WAIT_LOCK);
12371234

12381235
tid = PyThread_start_new_thread(faulthandler_fatal_error_thread, lock);
1239-
if (tid == -1) {
1236+
if (tid == PYTHREAD_INVALID_THREAD_ID) {
12401237
PyThread_free_lock(lock);
12411238
PyErr_SetString(PyExc_RuntimeError, "unable to start the thread");
12421239
return NULL;

0 commit comments

Comments
 (0)