Skip to content

Commit a0b2967

Browse files
committed
fix UBSan failures in faulthandler.c
- reading from NULL is no more an undefined behavior for C11 - instead of using 1/0 arithmetic, we explicitly raise SIGFPE
1 parent 13cb8ca commit a0b2967

File tree

1 file changed

+5
-16
lines changed

1 file changed

+5
-16
lines changed

Modules/faulthandler.c

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,8 +1069,8 @@ faulthandler_suppress_crash_report(void)
10691069
#endif
10701070
}
10711071

1072-
static PyObject* _Py_NO_SANITIZE_UNDEFINED
1073-
faulthandler_read_null(PyObject *self, PyObject *args)
1072+
static PyObject*
1073+
faulthandler_read_null(PyObject *self, PyObject *Py_UNUSED(args))
10741074
{
10751075
volatile int *x;
10761076
volatile int y;
@@ -1079,7 +1079,6 @@ faulthandler_read_null(PyObject *self, PyObject *args)
10791079
x = NULL;
10801080
y = *x;
10811081
return PyLong_FromLong(y);
1082-
10831082
}
10841083

10851084
static void
@@ -1158,23 +1157,13 @@ faulthandler_fatal_error_c_thread(PyObject *self, PyObject *args)
11581157
Py_RETURN_NONE;
11591158
}
11601159

1161-
static PyObject* _Py_NO_SANITIZE_UNDEFINED
1160+
static PyObject*
11621161
faulthandler_sigfpe(PyObject *self, PyObject *Py_UNUSED(dummy))
11631162
{
11641163
faulthandler_suppress_crash_report();
1165-
1166-
/* Do an integer division by zero: raise a SIGFPE on Intel CPU, but not on
1167-
PowerPC. Use volatile to disable compile-time optimizations. */
1168-
volatile int x = 1, y = 0, z;
1169-
z = x / y;
1170-
1171-
/* If the division by zero didn't raise a SIGFPE (e.g. on PowerPC),
1172-
raise it manually. */
1164+
/* raise SIGFPE manually to prevent crafted undefined behaviors */
11731165
raise(SIGFPE);
1174-
1175-
/* This line is never reached, but we pretend to make something with z
1176-
to silence a compiler warning. */
1177-
return PyLong_FromLong(z);
1166+
Py_UNREACHABLE();
11781167
}
11791168

11801169
static PyObject *

0 commit comments

Comments
 (0)