Ticket #9130: trac_9130-py_float_segfault.take2.patch

File trac_9130-py_float_segfault.take2.patch, 2.4 KB (added by burcin, 8 years ago)
  • sage/libs/ginac/decl.pxi

    # HG changeset patch
    # User Burcin Erocal <burcin@erocal.org>
    # Date 1306345394 -7200
    # Node ID eac4215c169efa580a04be3c82a72cbdce573de1
    # Parent  21fa9916737efeecab87a20910899d7302d30fd6
    trac 9130: fix segfault in sage.symbolic.pynac.py_float
    
    When the second argument of py_float() is declared as object, Cython assumes
    that this is initialized and it can do reference counting, etc. Calls to
    py_float() from pynac cannot provide a valid python object for the second
    argument. In this case pynac just passes, a NULL pointer to py_float(), which
    lead to crashes.
    
    Changing the declaration from object to PyObject* and checking for NULL fixes
    the problem.
    
    diff --git a/sage/libs/ginac/decl.pxi b/sage/libs/ginac/decl.pxi
    a b  
    1111# we do *not* have to use sig_on() and sig_off(). We do use it a little
    1212# in the actual pyx code to catch control-c for long running functions.
    1313
     14from cpython cimport PyObject
     15
    1416cdef extern from "ginac_wrap.h":
    1517    void ginac_pyinit_Integer(object)
    1618    void ginac_pyinit_Float(object)
     
    433435        object (*py_integer_from_long)(long int x) except +
    434436        object (*py_integer_from_python_obj)(object x) except +
    435437
    436         object (*py_float)(object a, object parent) except +
     438        object (*py_float)(object a, PyObject* parent) except +
    437439        object (*py_RDF_from_double)(double x)
    438440
    439441
  • sage/symbolic/pynac.pyx

    diff --git a/sage/symbolic/pynac.pyx b/sage/symbolic/pynac.pyx
    a b  
    987987    """
    988988    return py_is_cinteger(x)
    989989
    990 cdef public object py_float(object n, object parent) except +:
     990cdef public object py_float(object n, PyObject* parent) except +:
    991991    """
    992992    Evaluate pynac numeric objects numerically.
    993993
     
    10071007        sage: type(py_float(1/2, CC))
    10081008        <type 'sage.rings.complex_number.ComplexNumber'>
    10091009    """
    1010     return parent(n)
     1010    if parent is not NULL:
     1011        return (<object>parent)(n)
     1012    else:
     1013        try:
     1014            return RR(n)
     1015        except TypeError:
     1016            return CC(n)
    10111017
    10121018def py_float_for_doctests(n, prec):
    10131019    """
     
    10191025        sage: py_float_for_doctests(pi, RealField(80))
    10201026        3.1415926535897932384626
    10211027    """
    1022     return py_float(n, prec)
     1028    return py_float(n, <PyObject*>prec)
    10231029
    10241030# TODO: Optimize this
    10251031from sage.rings.real_double import RDF