From 91f8516a890d41e68ce88dff3fcebee096e117f7 Mon Sep 17 00:00:00 2001 From: Alex Shinn Date: Wed, 26 Feb 2014 07:16:01 +0900 Subject: [PATCH] Fixing right shift of a fixnum by > word size. Fixes bug report from Miroslav Urbanek. --- lib/srfi/33/bit.c | 2 +- tests/srfi-33-tests.scm | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/srfi/33/bit.c b/lib/srfi/33/bit.c index 046d779e..0546cc33 100644 --- a/lib/srfi/33/bit.c +++ b/lib/srfi/33/bit.c @@ -157,7 +157,7 @@ static sexp sexp_arithmetic_shift (sexp ctx, sexp self, sexp_sint_t n, sexp i, s if (c == 0) return i; if (sexp_fixnump(i)) { if (c < 0) { - res = sexp_make_fixnum(sexp_unbox_fixnum(i) >> -c); + res = sexp_make_fixnum(c > -sizeof(sexp_sint_t)*CHAR_BIT ? sexp_unbox_fixnum(i) >> -c : 0); } else { #if SEXP_USE_BIGNUMS if ((log2i(sexp_unbox_fixnum(i)) + c + 1) diff --git a/tests/srfi-33-tests.scm b/tests/srfi-33-tests.scm index 74eb74d8..d314bd4a 100644 --- a/tests/srfi-33-tests.scm +++ b/tests/srfi-33-tests.scm @@ -34,4 +34,8 @@ (test (- (expt 2 128)) (arithmetic-shift -1 128)) (test (- (expt 2 129)) (arithmetic-shift -1 129)) +(test 0 (arithmetic-shift 1 -63)) +(test 0 (arithmetic-shift 1 -64)) +(test 0 (arithmetic-shift 1 -65)) + (test-end)