Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@MasterDuke17
Created October 10, 2020 21:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MasterDuke17/734c8fde3fa9f200e9a7735f495d70c7 to your computer and use it in GitHub Desktop.
Save MasterDuke17/734c8fde3fa9f200e9a7735f495d70c7 to your computer and use it in GitHub Desktop.
diff --git src/math/bigintops.c src/math/bigintops.c
index a683d70b2..f5724f589 100644
--- src/math/bigintops.c
+++ src/math/bigintops.c
@@ -36,6 +36,12 @@
#undef mpz_mod
#define mpz_mod mpz_fdiv_r
+static jmp_buf buf;
+
+void handler(int signal) {
+ longjmp(buf, 1);
+}
+
MVM_STATIC_INLINE void adjust_nursery(MVMThreadContext *tc, MVMP6bigintBody *body) {
if (MVM_BIGINT_IS_BIG(body)) {
int used = mpz_size(*body->u.bigint) * sizeof(mp_limb_t);
@@ -420,12 +411,21 @@ MVMObject * MVM_bigint_pow(MVMThreadContext *tc, MVMObject *a, MVMObject *b,
MVMP6bigintBody *resbody;
mpz_init(*ic);
MVM_gc_mark_thread_blocked(tc);
- mpz_pow_ui(*ic, *base, exponent_d);
- MVM_gc_mark_thread_unblocked(tc);
- r = MVM_repr_alloc_init(tc, int_type);
- resbody = get_bigint_body(tc, r);
- store_bigint_result(resbody, ic);
- adjust_nursery(tc, resbody);
+ signal(SIGABRT, handler);
+ if (setjmp(buf)) {
+ MVM_gc_mark_thread_unblocked(tc);
+ signal(SIGABRT, NULL);
+ MVM_exception_throw_adhoc(tc, "Can't allocate a bigint big enough!");
+ }
+ else {
+ mpz_pow_ui(*ic, *base, exponent_d);
+ signal(SIGABRT, NULL);
+ MVM_gc_mark_thread_unblocked(tc);
+ r = MVM_repr_alloc_init(tc, int_type);
+ resbody = get_bigint_body(tc, r);
+ store_bigint_result(resbody, ic);
+ adjust_nursery(tc, resbody);
+ }
}
}
else {
[dan@alexandria perl6]$ raku --ll-exception -e 'say 2⁴⁵⁵³⁵³⁵³⁴⁵³⁶⁴⁵³⁵³⁴⁵'
gmp: overflow in mpz type
gmp: overflow in mpz type
Can't allocate a bigint big enough!
at SETTING::src/core.c/Int.pm6:422 (/home/dan/Source/perl6/install/share/perl6/runtime/CORE.c.setting.moarvm:infix:<**>)
from SETTING::src/core.c/Numeric.pm6:264 (/home/dan/Source/perl6/install/share/perl6/runtime/CORE.c.setting.moarvm:postfix:<ⁿ>)
from SETTING::src/core.c/Numeric.pm6:263 (/home/dan/Source/perl6/install/share/perl6/runtime/CORE.c.setting.moarvm:postfix:<ⁿ>)
from -e:1 (<ephemeral file>:<unit>)
from -e:1 (<ephemeral file>:<unit-outer>)
from gen/moar/stage2/NQPHLL.nqp:1945 (/home/dan/Source/perl6/install/share/nqp/lib/NQPHLL.moarvm:eval)
from gen/moar/stage2/NQPHLL.nqp:2055 (/home/dan/Source/perl6/install/share/nqp/lib/NQPHLL.moarvm:)
from gen/moar/stage2/NQPHLL.nqp:2110 (/home/dan/Source/perl6/install/share/nqp/lib/NQPHLL.moarvm:command_eval)
from gen/moar/Compiler.nqp:64 (/home/dan/Source/perl6/install/share/perl6/lib/Perl6/Compiler.moarvm:command_eval)
from gen/moar/stage2/NQPHLL.nqp:2035 (/home/dan/Source/perl6/install/share/nqp/lib/NQPHLL.moarvm:command_line)
from gen/moar/rakudo.nqp:127 (/home/dan/Source/perl6/install/share/perl6/runtime/perl6.moarvm:MAIN)
from gen/moar/rakudo.nqp:1 (/home/dan/Source/perl6/install/share/perl6/runtime/perl6.moarvm:<mainline>)
from <unknown>:1 (/home/dan/Source/perl6/install/share/perl6/runtime/perl6.moarvm:<main>)
from <unknown>:1 (/home/dan/Source/perl6/install/share/perl6/runtime/perl6.moarvm:<entry>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment