Skip to content

Commit

Permalink
Fix generic bignums overflow (bootstrap)
Browse files Browse the repository at this point in the history
  • Loading branch information
manuel-serrano committed Dec 12, 2024
1 parent 3b22d7f commit 8c9b57f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
7 changes: 4 additions & 3 deletions runtime/Clib/cbignum.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/* ------------------------------------------------------------- */
/* Author : José Romildo Malaquias */
/* Creation : Fri Nov 10 11:51:17 2006 */
/* Last change : Fri Nov 15 07:36:49 2024 (serrano) */
/* Last change : Thu Dec 12 09:52:45 2024 (serrano) */
/* Copyright : 2003-24 Manuel Serrano */
/* ------------------------------------------------------------- */
/* C implementation of bignum */
Expand Down Expand Up @@ -846,10 +846,11 @@ obj_t
bgl_safe_bignum_to_fixnum(obj_t bx) {
size_t bs = mpz_sizeinbase(&(BIGNUM(bx).mpz), 2);

if (bs < BGL_INT_BIT_SIZE)
if (bs < BGL_INT_BIT_SIZE) {
return BINT(bgl_bignum_to_long(bx));
else
} else {
return bx;
}
}

/*---------------------------------------------------------------------*/
Expand Down
8 changes: 6 additions & 2 deletions runtime/Unsafe/bignumber-generic.sch
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
;* ------------------------------------------------------------- */
;* Author : Manuel Serrano */
;* Creation : Thu Aug 29 07:41:07 2024 */
;* Last change : Thu Dec 12 08:48:41 2024 (serrano) */
;* Last change : Thu Dec 12 10:10:18 2024 (serrano) */
;* Copyright : 2024 Manuel Serrano */
;* ------------------------------------------------------------- */
;* Portable implementation of bignums. This is used only when no */
Expand Down Expand Up @@ -454,7 +454,11 @@
;*---------------------------------------------------------------------*/
;* Overflow operations ... */
;*---------------------------------------------------------------------*/
(define ($bignum->fixnum-safe x) (or ($bignum->fixnum x) x))
(define ($bignum->fixnum-safe x)
(let ((n ($bignum->fixnum x)))
(if (and n (=bx (fixnum->bignum n) x))
n
x)))
(define ($+fx-safe x y) (+fx/ov x y))
(define ($-fx-safe x y) (-fx/ov x y))
(define ($*fx-safe x y) (*fx/ov x y))
Expand Down

0 comments on commit 8c9b57f

Please sign in to comment.