Skip to content

Commit b54c504

Browse files
committed
Fix a parser bug when parsing "num/denom" strings with numbers close to 'sys.maxsize'.
When the parser detected that the numerator is getting too large to fit safely into a C integer,, it continues parsing digits for the denominator without clearing the digit collection buffer, thus effectively contatenating numerator and denominator into the parsed denominator.
1 parent 85b0325 commit b54c504

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

CHANGES.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
ChangeLog
22
=========
33

4+
1.21 (2025-06-13)
5+
-----------------
6+
7+
* A serious parser bug could accidentally concatenate numerator and denominator
8+
as final denominator when parsing "x/y" where x or y are close to ``sys.maxsize``,
9+
thus returning a ``Fraction("x/xy")``.
10+
11+
412
1.20 (2025-06-13)
513
-----------------
614

src/quicktions.pyx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1790,6 +1790,8 @@ cdef tuple _parse_fraction(AnyString s, Py_ssize_t s_len, orig_str):
17901790
break
17911791
pos += 1
17921792

1793+
# Start fresh digits collection.
1794+
c_digits = c_digits_start
17931795
while pos < s_len:
17941796
c = _char_at(s_data, s_kind, pos) if AnyString is unicode else cdata[pos]
17951797
pos += 1

src/test_fractions.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1975,7 +1975,11 @@ def test_large_values(self):
19751975
(63, 64),
19761976
(64, 70),
19771977
(128, 511),
1978-
]]
1978+
]] + ["%s/%s" % (a, b) for a, b in itertools.permutations([
1979+
sys.maxsize // n + i
1980+
for i in range(-3, +3)
1981+
for n in (1, 2, 4, 10, 100)
1982+
], 2)]
19791983
values = [
19801984
v for value in values
19811985
for v in ([value, value.replace('/', '.'), value.replace('/', '.') + "E" + value[:4]]

0 commit comments

Comments
 (0)