Skip to content
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

Fix crash when specify invalid base for RR and RIF construction #39001

Merged
Merged
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
29 changes: 29 additions & 0 deletions src/sage/rings/convert/mpfi.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,42 @@ cdef int mpfi_set_sage(mpfi_ptr re, mpfi_ptr im, x, field, int base) except -1:
imaginary component is 0.

- in all other cases: raise an exception.

TESTS::

sage: RIF('0xabc')
Traceback (most recent call last):
...
TypeError: unable to convert '0xabc' to real interval
sage: RIF("0x123.e1", base=0) # rel tol 1e-12
291.87890625000000?
sage: RIF("0x123.@1", base=0) # rel tol 1e-12
4656
sage: RIF("1Xx", base=36) # rel tol 1e-12
2517
sage: RIF("-1Xx@-10", base=62) # rel tol 1e-12
-7.088054920481391?e-15
sage: RIF("1", base=1)
Traceback (most recent call last):
...
ValueError: base (=1) must be 0 or between 2 and 62
sage: RIF("1", base=-1)
Traceback (most recent call last):
...
ValueError: base (=-1) must be 0 or between 2 and 62
sage: RIF("1", base=63)
Traceback (most recent call last):
...
ValueError: base (=63) must be 0 or between 2 and 62
"""
cdef RealIntervalFieldElement ri
cdef ComplexIntervalFieldElement zi
cdef ComplexNumber zn
cdef ComplexDoubleElement zd
cdef bytes s

if base != 0 and (base < 2 or base > 62):
raise ValueError(f"base (={base}) must be 0 or between 2 and 62")
if im is not NULL and isinstance(x, tuple):
# For complex numbers, interpret tuples as real/imag parts
if len(x) != 2:
Expand Down
29 changes: 29 additions & 0 deletions src/sage/rings/real_mpfr.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1437,6 +1437,33 @@ cdef class RealNumber(sage.structure.element.RingElement):

sage: RealNumber('1_3.1e-32_45')
1.31000000000000e-3244

Test conversion from base different from `10`::

sage: RR('0xabc')
Traceback (most recent call last):
...
TypeError: unable to convert '0xabc' to a real number
sage: RR("0x123.e1", base=0) # rel tol 1e-12
291.878906250000
sage: RR("0x123.@1", base=0) # rel tol 1e-12
4656.00000000000
sage: RR("1Xx", base=36) # rel tol 1e-12
2517.00000000000
sage: RR("-1Xx@-10", base=62) # rel tol 1e-12
-7.08805492048139e-15
sage: RR("1", base=1)
Traceback (most recent call last):
...
ValueError: base (=1) must be 0 or between 2 and 62
sage: RR("1", base=-1)
Traceback (most recent call last):
...
ValueError: base (=-1) must be 0 or between 2 and 62
sage: RR("1", base=63)
Traceback (most recent call last):
...
ValueError: base (=63) must be 0 or between 2 and 62
"""
if x is not None:
self._set(x, base)
Expand Down Expand Up @@ -1485,6 +1512,8 @@ cdef class RealNumber(sage.structure.element.RingElement):
# Real Numbers are supposed to be immutable.
cdef RealField_class parent
parent = self._parent
if base != 0 and (base < 2 or base > 62):
raise ValueError(f"base (={base}) must be 0 or between 2 and 62")
if isinstance(x, RealNumber):
if isinstance(x, RealLiteral):
s = (<RealLiteral>x).literal
Expand Down
Loading