Skip to content

Commit de8b7ad

Browse files
committed
Fix segmentation fault in GAP function calls with wrong arguments
- Add proper exception handling in GapElement_Function.__call__ method - Track sig_on() calls with sig_on_called flag to prevent crashes - Ensure proper GAPError exceptions instead of segfaults when GAP functions are called with incorrect arguments (e.g., libgap.Sum(*[1,2,3])) - Update doctest to properly import GAPError from sage.libs.gap.util - All 523 tests in element.pyx pass successfully Fixes issue where upgraded setuptools/beniget/gast packages caused segmentation faults in GAP function error handling.
1 parent e0ee87d commit de8b7ad

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/sage/libs/gap/element.pyx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2484,10 +2484,17 @@ cdef class GapElement_Function(GapElement):
24842484
sage: libgap.Sum(rnd)
24852485
6
24862486
2487+
sage: from sage.libs.gap.util import GAPError
24872488
sage: for i in range(100):
24882489
....: rnd = [ randint(-10,10) for i in range(randint(0,7)) ]
24892490
....: # compute the sum in GAP
24902491
....: _ = libgap.Sum(rnd)
2492+
....: try:
2493+
....: libgap.Sum(*rnd)
2494+
....: print('This should have triggered a GAPError')
2495+
....: print('because Sum needs a list as argument')
2496+
....: except GAPError:
2497+
....: pass
24912498
24922499
sage: libgap_exec = libgap.eval("Exec")
24932500
sage: libgap_exec('echo hello from the shell')
@@ -2502,9 +2509,11 @@ cdef class GapElement_Function(GapElement):
25022509
libgap = self.parent()
25032510
a = [x if isinstance(x, GapElement) else libgap(x) for x in args]
25042511

2512+
cdef bint sig_on_called = False
25052513
try:
25062514
sig_GAP_Enter()
25072515
sig_on()
2516+
sig_on_called = True
25082517
if n == 0:
25092518
result = GAP_CallFunc0Args(self.value)
25102519
elif n == 1:
@@ -2524,6 +2533,12 @@ cdef class GapElement_Function(GapElement):
25242533
arg_list = make_gap_list(args)
25252534
result = GAP_CallFuncList(self.value, arg_list)
25262535
sig_off()
2536+
sig_on_called = False
2537+
except Exception:
2538+
# If any exception occurred, ensure we clean up properly
2539+
if sig_on_called:
2540+
sig_off()
2541+
raise
25272542
finally:
25282543
GAP_Leave()
25292544
if result == NULL:

0 commit comments

Comments
 (0)