Skip to content

Commit 43657bf

Browse files
committed
gh-143006: add type check in float_richcompare()
1 parent 1391ee6 commit 43657bf

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

Lib/test/test_float.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,14 @@ class F(float, H):
651651
value = F('nan')
652652
self.assertEqual(hash(value), object.__hash__(value))
653653

654+
def test_issue_gh143006(self):
655+
class EvilInt(int):
656+
def __neg__(self):
657+
return
658+
659+
i = -1<<50
660+
self.assertRaises(TypeError, operator.ge, float(i), EvilInt(i))
661+
654662

655663
@unittest.skipUnless(hasattr(float, "__getformat__"), "requires __getformat__")
656664
class FormatFunctionsTestCase(unittest.TestCase):
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Add missing type check for rich comparison methods of the :class:`float` to
2+
avoid possible crash with integer operand, having broken implementation of
3+
the :meth:`object.__neg__` method.

Objects/floatobject.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,12 @@ float_richcompare(PyObject *v, PyObject *w, int op)
473473
ww = PyNumber_Negative(w);
474474
if (ww == NULL)
475475
goto Error;
476+
else if (!PyLong_Check(ww)) {
477+
PyErr_SetString(PyExc_TypeError,
478+
"unexpected type from negation "
479+
"of integer operand");
480+
goto Error;
481+
}
476482
}
477483
else
478484
Py_INCREF(ww);

0 commit comments

Comments
 (0)