Skip to content
Open
Show file tree
Hide file tree
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
12 changes: 12 additions & 0 deletions Lib/test/test_long.py
Original file line number Diff line number Diff line change
Expand Up @@ -1748,6 +1748,18 @@ def test_karatsuba_single_digit_parts(self):
self.assertEqual(a * b, (1 << (SHIFT * 139)) + (1 << (SHIFT * 209)))
self.assertEqual(b * a, a * b)

def test_karatsuba_zero_parts(self):
# gh-100687: k_lopsided_mul() passes slices of the larger operand
# to k_mul(), and such a slice can consist of zero digits only.
n = KARATSUBA_CUTOFF + 30
a = (1 << (SHIFT * n)) - 1
b = 1 << (SHIFT * n * 3)
self.assertEqual(a * b, a << (SHIFT * n * 3))
self.assertEqual(b * a, a * b)
# Only the high half of a slice is zero.
b += 1 << (SHIFT * (n + 1))
self.assertEqual(a * b, (a << (SHIFT * n * 3)) + (a << (SHIFT * (n + 1))))

def test_hash(self):
# gh-136599
self.assertEqual(hash(-1), -2)
Expand Down
6 changes: 6 additions & 0 deletions Objects/longobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -4205,6 +4205,12 @@ k_mul(PyLongObject *a, PyLongObject *b)
if (a == b) {
t2 = (PyLongObject*)Py_NewRef(t1);
}
else if (_PyLong_IsZero(bh)) {
/* b can be an unnormalized slice made by k_lopsided_mul(), so bh
* (and bl too) can be zero, and x_add() needs a nonzero operand.
*/
t2 = (PyLongObject*)Py_NewRef(bl);
}
else if ((t2 = x_add(bh, bl)) == NULL) {
Py_DECREF(t1);
goto fail;
Expand Down
Loading