1.0.36.24: FIND/POSITION bounds checking on lists
[sbcl.git] / tests / seq.pure.lisp
index a0897be..eb75013 100644 (file)
@@ -11,6 +11,8 @@
 ;;;; absolutely no warranty. See the COPYING and CREDITS files for
 ;;;; more information.
 
+(in-package :cl-user)
+
 ;;; As reported by Paul Dietz from his ansi-test suite for gcl, REMOVE
 ;;; malfunctioned when given :START, :END and :FROM-END arguments.
 ;;; Make sure it doesn't happen again.
   (assert s2)
   (assert (string= s2 "zzzzz")))
 
-;;; POSITION on dispaced arrays with non-zero offset has been broken
+;;; POSITION on displaced arrays with non-zero offset has been broken
 ;;; for quite a while...
 (let ((fn (compile nil '(lambda (x) (position x)))))
   (let* ((x #(1 2 3))
 (let ((a (make-sequence '(simple-string) 5))
       (b (concatenate '(simple-string) "a" "bdec"))
       (c (map '(simple-string) 'identity "abcde"))
-      (d (merge '(simple-string) "acd" "be" 'char>))
+      (d (merge '(simple-string) (copy-seq "acd") (copy-seq "be") 'char>))
       (e (coerce '(#\a #\b #\c #\e #\d) '(simple-string))))
   (assert (= (length a) 5))
   (assert (string= b "abdec"))
   (assert (string= c "abcde"))
   (assert (string= d "beacd"))
   (assert (string= e "abced")))
+
+;;; COPY-SEQ "should be prepared to signal an error if sequence is not
+;;; a proper sequence".
+(locally (declare (optimize safety))
+  (multiple-value-bind (seq err) (ignore-errors (copy-seq '(1 2 3 . 4)))
+    (assert (not seq))
+    (assert (typep err 'type-error))))
+
+;;; UBX-BASH-COPY transform had an inconsistent return type
+(let ((sb-c::*check-consistency* t))
+  (handler-bind ((warning #'error))
+    (compile nil
+             '(lambda (l)
+               (declare (type fixnum l))
+               (let* ((bsize 128)
+                      (b1 (make-array bsize :element-type '(unsigned-byte 8)))
+                      (b2 (make-array l :element-type '(unsigned-byte 8))))
+                 (replace b1 b2 :start2 0 :end2 l))))))
+
+(with-test (:name :bug-452008)
+  ;; FIND & POSITION on lists should check bounds and (in safe code) detect
+  ;; circular and dotted lists.
+  (macrolet ((test (type lambda)
+               `(let ((got (handler-case
+                               (funcall (compile nil ',lambda))
+                             (,type () :error)
+                             (:no-error (res)
+                               (list :no-error res)))))
+                  (let ((*print-circle* t))
+                    (format t "test: ~S~%" ',lambda))
+                  (unless (eq :error got)
+                    (error "wanted an error, got ~S for~%  ~S"
+                           (second got) ',lambda)))))
+    (test sb-kernel:bounding-indices-bad-error
+          (lambda ()
+            (find :foo '(1 2 3 :foo) :start 1 :end 5)))
+    (test sb-kernel:bounding-indices-bad-error
+          (lambda ()
+            (position :foo '(1 2 3 :foo) :start 1 :end 5)))
+    (test sb-kernel:bounding-indices-bad-error
+          (lambda ()
+            (find :foo '(1 2 3 :foo) :start 3 :end 0)))
+    (test sb-kernel:bounding-indices-bad-error
+          (lambda ()
+            (position :foo '(1 2 3 :foo) :start 3 :end 0)))
+    (test type-error
+          (lambda ()
+            (let ((list (list 1 2 3 :foo)))
+              (find :bar (nconc list list)))))
+    (test type-error
+          (lambda ()
+            (let ((list (list 1 2 3 :foo)))
+              (position :bar (nconc list list)))))))