From 6e8fe793a4f3e8a3c8b67755101ee15df85d73c4 Mon Sep 17 00:00:00 2001 From: Stas Boukarev Date: Sat, 7 Sep 2013 23:01:37 +0400 Subject: [PATCH] LDB/DPB do not check for negative indexes. Calling (lambda (x y) (ldb (byte x y) 100)) with -1 -2 didn't raise an error. Reported by Bart Botta. --- src/code/numbers.lisp | 4 ++++ tests/arith.pure.lisp | 26 ++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/src/code/numbers.lisp b/src/code/numbers.lisp index bf3a03a..5847707 100644 --- a/src/code/numbers.lisp +++ b/src/code/numbers.lisp @@ -1176,18 +1176,22 @@ the first." (deposit-field newbyte bytespec integer)) (defun %ldb (size posn integer) + (declare (type bit-index size posn)) (logand (ash integer (- posn)) (1- (ash 1 size)))) (defun %mask-field (size posn integer) + (declare (type bit-index size posn)) (logand integer (ash (1- (ash 1 size)) posn))) (defun %dpb (newbyte size posn integer) + (declare (type bit-index size posn)) (let ((mask (1- (ash 1 size)))) (logior (logand integer (lognot (ash mask posn))) (ash (logand newbyte mask) posn)))) (defun %deposit-field (newbyte size posn integer) + (declare (type bit-index size posn)) (let ((mask (ash (ldb (byte size 0) -1) posn))) (logior (logand newbyte mask) (logand integer (lognot mask))))) diff --git a/tests/arith.pure.lisp b/tests/arith.pure.lisp index 32cff23..d257edc 100644 --- a/tests/arith.pure.lisp +++ b/tests/arith.pure.lisp @@ -628,3 +628,29 @@ (logior x ,k))))) (loop for x from min upto max do (assert (eql (logior x k) (funcall f x))))))))) + +(with-test (:name :ldb-negative-index-no-error) + (assert + (raises-error? + (funcall (compile nil + `(lambda (x y) + (ldb (byte x y) 100))) + -1 -2))) + (assert + (raises-error? + (funcall (compile nil + `(lambda (x y) + (mask-field (byte x y) 100))) + -1 -2))) + (assert + (raises-error? + (funcall (compile nil + `(lambda (x y) + (dpb 0 (byte x y) 100))) + -1 -2))) + (assert + (raises-error? + (funcall (compile nil + `(lambda (x y) + (deposit-field 0 (byte x y) 100))) + -1 -2)))) -- 1.7.10.4