Skip to content

gh-111178: fix UBSan failures in Objects/floatobject.c #129776

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

Merged
merged 2 commits into from
Feb 8, 2025
Merged
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
43 changes: 24 additions & 19 deletions Objects/floatobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -369,8 +369,9 @@ _Py_convert_int_to_double(PyObject **v, double *dbl)
}

static PyObject *
float_repr(PyFloatObject *v)
float_repr(PyObject *op)
{
PyFloatObject *v = _PyFloat_CAST(op);
PyObject *result;
char *buf;

Expand Down Expand Up @@ -579,9 +580,10 @@ float_richcompare(PyObject *v, PyObject *w, int op)
}

static Py_hash_t
float_hash(PyFloatObject *v)
float_hash(PyObject *op)
{
return _Py_HashDouble((PyObject *)v, v->ob_fval);
PyFloatObject *v = _PyFloat_CAST(op);
return _Py_HashDouble(op, v->ob_fval);
}

static PyObject *
Expand Down Expand Up @@ -851,20 +853,23 @@ float_pow(PyObject *v, PyObject *w, PyObject *z)
#undef DOUBLE_IS_ODD_INTEGER

static PyObject *
float_neg(PyFloatObject *v)
float_neg(PyObject *op)
{
PyFloatObject *v = _PyFloat_CAST(op);
return PyFloat_FromDouble(-v->ob_fval);
}

static PyObject *
float_abs(PyFloatObject *v)
float_abs(PyObject *op)
{
PyFloatObject *v = _PyFloat_CAST(op);
return PyFloat_FromDouble(fabs(v->ob_fval));
}

static int
float_bool(PyFloatObject *v)
float_bool(PyObject *op)
{
PyFloatObject *v = _PyFloat_CAST(op);
return v->ob_fval != 0.0;
}

Expand Down Expand Up @@ -1206,7 +1211,7 @@ float_hex_impl(PyObject *self)
CONVERT_TO_DOUBLE(self, x);

if (isnan(x) || isinf(x))
return float_repr((PyFloatObject *)self);
return float_repr(self);

if (x == 0.0) {
if (copysign(1.0, x) == -1.0)
Expand Down Expand Up @@ -1651,7 +1656,7 @@ float_subtype_new(PyTypeObject *type, PyObject *x)
}

static PyObject *
float_vectorcall(PyObject *type, PyObject * const*args,
float_vectorcall(PyObject *type, PyObject *const *args,
size_t nargsf, PyObject *kwnames)
{
if (!_PyArg_NoKwnames("float", kwnames)) {
Expand Down Expand Up @@ -1771,13 +1776,13 @@ float___getformat___impl(PyTypeObject *type, const char *typestr)


static PyObject *
float_getreal(PyObject *v, void *closure)
float_getreal(PyObject *v, void *Py_UNUSED(closure))
{
return float_float(v);
}

static PyObject *
float_getimag(PyObject *v, void *closure)
float_getimag(PyObject *Py_UNUSED(v), void *Py_UNUSED(closure))
{
return PyFloat_FromDouble(0.0);
}
Expand Down Expand Up @@ -1829,11 +1834,11 @@ static PyMethodDef float_methods[] = {

static PyGetSetDef float_getset[] = {
{"real",
float_getreal, (setter)NULL,
float_getreal, NULL,
"the real part of a complex number",
NULL},
{"imag",
float_getimag, (setter)NULL,
float_getimag, NULL,
"the imaginary part of a complex number",
NULL},
{NULL} /* Sentinel */
Expand All @@ -1847,10 +1852,10 @@ static PyNumberMethods float_as_number = {
float_rem, /* nb_remainder */
float_divmod, /* nb_divmod */
float_pow, /* nb_power */
(unaryfunc)float_neg, /* nb_negative */
float_neg, /* nb_negative */
float_float, /* nb_positive */
(unaryfunc)float_abs, /* nb_absolute */
(inquiry)float_bool, /* nb_bool */
float_abs, /* nb_absolute */
float_bool, /* nb_bool */
0, /* nb_invert */
0, /* nb_lshift */
0, /* nb_rshift */
Expand Down Expand Up @@ -1881,16 +1886,16 @@ PyTypeObject PyFloat_Type = {
"float",
sizeof(PyFloatObject),
0,
(destructor)float_dealloc, /* tp_dealloc */
float_dealloc, /* tp_dealloc */
0, /* tp_vectorcall_offset */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_as_async */
(reprfunc)float_repr, /* tp_repr */
float_repr, /* tp_repr */
&float_as_number, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
(hashfunc)float_hash, /* tp_hash */
float_hash, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
PyObject_GenericGetAttr, /* tp_getattro */
Expand All @@ -1916,7 +1921,7 @@ PyTypeObject PyFloat_Type = {
0, /* tp_init */
0, /* tp_alloc */
float_new, /* tp_new */
.tp_vectorcall = (vectorcallfunc)float_vectorcall,
.tp_vectorcall = float_vectorcall,
.tp_version_tag = _Py_TYPE_VERSION_FLOAT,
};

Expand Down
Loading