Skip to content

Commit 9b16f9a

Browse files
committed
Fix segmentation fault
1 parent e3d5feb commit 9b16f9a

File tree

1 file changed

+8
-15
lines changed

1 file changed

+8
-15
lines changed

src/sage/rings/integer.pyx

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,8 @@ cdef class Integer(sage.structure.element.EuclideanDomainElement):
483483
"""
484484

485485
def __cinit__(self):
486+
# this function is only called to create global_dummy_Integer,
487+
# after that it will be replaced by fast_tp_new
486488
global the_integer_ring
487489
mpz_init(self.value)
488490
self._parent = the_integer_ring
@@ -7542,8 +7544,6 @@ cdef int sizeof_Integer
75427544
# from. DO NOT INITIALIZE IT AGAIN and DO NOT REFERENCE IT!
75437545
cdef Integer global_dummy_Integer
75447546
global_dummy_Integer = Integer()
7545-
# Reallocate to one limb to fix :issue:`31340` and :issue:`33081`
7546-
_mpz_realloc(global_dummy_Integer.value, 1)
75477547

75487548

75497549
def _check_global_dummy_Integer():
@@ -7559,7 +7559,7 @@ def _check_global_dummy_Integer():
75597559
# Check that it has exactly one limb allocated
75607560
# This is assumed later in fast_tp_new() :issue:`33081`
75617561
cdef mpz_ptr dummy = global_dummy_Integer.value
7562-
if dummy._mp_alloc == 1 and dummy._mp_size == 0:
7562+
if dummy._mp_alloc == 0 and dummy._mp_size == 0:
75637563
return True
75647564

75657565
raise AssertionError(
@@ -7589,7 +7589,6 @@ cdef PyObject* fast_tp_new(type t, args, kwds) except NULL:
75897589
global integer_pool, integer_pool_count, total_alloc, use_pool
75907590

75917591
cdef PyObject* new
7592-
cdef mpz_ptr new_mpz
75937592

75947593
# for profiling pool usage
75957594
# total_alloc += 1
@@ -7622,12 +7621,10 @@ cdef PyObject* fast_tp_new(type t, args, kwds) except NULL:
76227621
# created before this tp_new started to operate.
76237622
memcpy(new, (<void*>global_dummy_Integer), sizeof_Integer)
76247623

7625-
# We allocate memory for the _mp_d element of the value of this
7626-
# new Integer. We allocate one limb. Normally, one would use
7627-
# mpz_init() for this, but we allocate the memory directly.
7628-
# This saves time both by avoiding extra function calls and
7629-
# because the rest of the mpz struct was already initialized
7630-
# fully using the memcpy above.
7624+
# In sufficiently new versions of GMP, mpz_init() does not allocate
7625+
# any memory. We assume that memcpy a newly-initialized mpz results
7626+
# in a valid new mpz. Normally, one would use mpz_init() for this.
7627+
# This saves time by avoiding extra function calls.
76317628
#
76327629
# What is done here is potentially very dangerous as it reaches
76337630
# deeply into the internal structure of GMP. Consequently things
@@ -7639,10 +7636,6 @@ cdef PyObject* fast_tp_new(type t, args, kwds) except NULL:
76397636
# various internals described here may change in future GMP releases.
76407637
# Applications expecting to be compatible with future releases should use
76417638
# only the documented interfaces described in previous chapters."
7642-
#
7643-
# NOTE: This assumes global_dummy_Integer.value._mp_alloc == 1
7644-
new_mpz = <mpz_ptr>((<Integer>new).value)
7645-
new_mpz._mp_d = <mp_ptr>check_malloc(GMP_LIMB_BITS >> 3)
76467639

76477640
# This line is only needed if Python is compiled in debugging mode
76487641
# './configure --with-pydebug' or SAGE_DEBUG=yes. If that is the
@@ -7692,7 +7685,7 @@ cdef void fast_tp_dealloc(PyObject* o) noexcept:
76927685
return
76937686

76947687
# No space in the pool, so just free the mpz_t.
7695-
sig_free(o_mpz._mp_d)
7688+
mpz_clear(o_mpz)
76967689

76977690
# Free the object. This assumes that Py_TPFLAGS_HAVE_GC is not
76987691
# set. If it was set another free function would need to be

0 commit comments

Comments
 (0)