Skip to content

Commit

Permalink
FIXED: Enabled pre-check for left-shift of large integers
Browse files Browse the repository at this point in the history
This both avoids possible GMP exceptions and allocating huge amounts
of memory.
  • Loading branch information
JanWielemaker committed May 15, 2024
1 parent 0dc7d4a commit e90bc9c
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/pl-arith.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Author: Jan Wielemaker
E-mail: J.Wielemaker@vu.nl
WWW: http://www.swi-prolog.org
Copyright (c) 1985-2023, University of Amsterdam
Copyright (c) 1985-2024, University of Amsterdam
VU University Amsterdam
CWI, Amsterdam
SWI-Prolog Solutions b.v.
Expand Down Expand Up @@ -81,6 +81,8 @@ in this array.
#endif
#include <fenv.h>

#define O_BIGNUM_PRECHECK_ALLOCATIONS 1

#ifndef DBL_MAX
#define DBL_MAX 1.7976931348623157e+308
#endif
Expand Down Expand Up @@ -1777,12 +1779,14 @@ ar_shift(Number n1, Number n2, Number r, int dir)
if ( dir < 0 ) /* shift left (<<) */
{
#ifdef O_BIGNUM_PRECHECK_ALLOCATIONS
GET_LD
uint64_t msb = mpz_sizeinbase(n1->value.mpz, 2)+shift;

if ( (msb/sizeof(char)) > (uint64_t)globalStackLimit() )
{ mpz_clear(r->value.mpz);
return int_too_big();
if ( msb > 10000 )
{ GET_LD
if ( (msb/sizeof(char)) > (uint64_t)globalStackLimit() )
{ mpz_clear(r->value.mpz);
return int_too_big();
}
}
#endif /*O_BIGNUM_PRECHECK_ALLOCATIONS*/
mpz_mul_2exp(r->value.mpz, n1->value.mpz, shift);
Expand Down

0 comments on commit e90bc9c

Please sign in to comment.