Skip to content

Commit dbd5ee0

Browse files
committed
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.
1 parent 72e7edd commit dbd5ee0

1 file changed

Lines changed: 10 additions & 12 deletions

File tree

Modules/faulthandler.c

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ faulthandler_get_fileno(PyObject **file_ptr)
102102
{
103103
PyObject *result;
104104
long fd_long;
105-
int fd;
106105
PyObject *file = *file_ptr;
107106

108107
if (file == NULL || file == Py_None) {
@@ -124,7 +123,7 @@ faulthandler_get_fileno(PyObject **file_ptr)
124123
return -1;
125124
}
126125
}
127-
fd = PyLong_AsInt(file);
126+
int fd = PyLong_AsInt(file);
128127
if (fd == -1 && PyErr_Occurred())
129128
return -1;
130129
if (fd < 0) {
@@ -145,15 +144,16 @@ faulthandler_get_fileno(PyObject **file_ptr)
145144
return -1;
146145
}
147146

148-
fd = -1;
147+
int fd;
149148
if (PyLong_Check(result)) {
150-
fd_long = PyLong_AsLong(result);
151-
if (0 <= fd_long && fd_long < INT_MAX)
152-
fd = (int)fd_long;
149+
fd = PyLong_AsInt(result);
150+
}
151+
else {
152+
fd = -1;
153153
}
154154
Py_DECREF(result);
155155

156-
if (fd == -1) {
156+
if (fd < 0) {
157157
PyErr_SetString(PyExc_RuntimeError,
158158
"file.fileno() is not a valid file descriptor");
159159
Py_DECREF(file);
@@ -407,10 +407,8 @@ faulthandler_fatal_error(int signum)
407407
PUTS(fd, "\n\n");
408408
}
409409
else {
410-
char unknown_signum[23] = {0,};
411-
snprintf(unknown_signum, 23, "%d", signum);
412410
PUTS(fd, "Fatal Python error from unexpected signum: ");
413-
PUTS(fd, unknown_signum);
411+
_Py_DumpDecimal(fd, signum);
414412
PUTS(fd, "\n\n");
415413
}
416414

@@ -713,7 +711,7 @@ faulthandler_thread(void *unused)
713711
/* Timeout => dump traceback */
714712
assert(st == PY_LOCK_FAILURE);
715713

716-
(void)_Py_write_noraise(thread.fd, thread.header, (int)thread.header_len);
714+
(void)_Py_write_noraise(thread.fd, thread.header, thread.header_len);
717715

718716
errmsg = PyUnstable_DumpTracebackThreads(thread.fd, thread.interp, NULL,
719717
thread.max_threads);
@@ -1224,7 +1222,7 @@ static PyObject *
12241222
faulthandler__fatal_error_c_thread_impl(PyObject *module)
12251223
/*[clinic end generated code: output=101bc8aaf4a5eec1 input=fbdca6fffd639a39]*/
12261224
{
1227-
long tid;
1225+
unsigned long tid;
12281226
PyThread_type_lock lock;
12291227

12301228
faulthandler_suppress_crash_report();

0 commit comments

Comments
 (0)