1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-06-14 23:50:19 +02:00

Avoid undefined behavior regarding signed integers and left shifts.

* libguile/numbers.c (scm_logbit_p): If the requested bit is the sign
  bit (or above), check the sign portably.  Otherwise, ensure that we're
  testing the bit in a two's complement representation.
  (left_shift_exact_integer): Avoid left-shifting negative integers.

* libguile/vm-i-scheme.c (ash): Avoid left-shifting negative integers.
This commit is contained in:
Mark H Weaver 2014-03-11 20:19:17 -04:00
parent 5fbf0e0f99
commit 03cce0ce5f
2 changed files with 17 additions and 8 deletions

View file

@ -1,4 +1,5 @@
/* Copyright (C) 2001, 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
/* Copyright (C) 2001, 2009, 2010, 2011, 2012, 2013,
* 2014 Free Software Foundation, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
@ -505,7 +506,9 @@ VM_DEFINE_FUNCTION (159, ash, "ash", 2)
&& ((scm_t_bits)
(SCM_SRS (nn, (SCM_I_FIXNUM_BIT-1 - bits_to_shift)) + 1)
<= 1))
RETURN (SCM_I_MAKINUM (nn << bits_to_shift));
RETURN (SCM_I_MAKINUM (nn < 0
? -(-nn << bits_to_shift)
: (nn << bits_to_shift)));
/* fall through */
}
/* fall through */