From ea73fa22dbcd77ebfd2a47cc4d3b68b8678bf222 Mon Sep 17 00:00:00 2001
From: ram <ram>
Date: Tue, 9 Oct 1990 14:16:41 +0000
Subject: [PATCH] Changed ELT and %SETELT to check that the index is less than
 the LENGTH, which might differ from the array dimension when the array is
 non-simple.

---
 code/seq.lisp | 51 ++++++++++++++++++++++++++++++---------------------
 1 file changed, 30 insertions(+), 21 deletions(-)

diff --git a/code/seq.lisp b/code/seq.lisp
index 07a3895b1..9ad4d9cb7 100644
--- a/code/seq.lisp
+++ b/code/seq.lisp
@@ -75,30 +75,39 @@
 
 (defun elt (sequence index)
   "Returns the element of SEQUENCE specified by INDEX."
-  (if (listp sequence)
-      (if (< index 0)
-	  (error "~S: index too small." index)
-	  (do ((count index (1- count)))
-	      ((= count 0) (car sequence))
-	    (declare (fixnum count))
-	    (if (atom sequence)
-		(error "~S: index too large." index)
-		(setq sequence (cdr sequence)))))
-      (aref sequence index)))
+  (etypecase sequence
+    (list
+     (if (< index 0)
+	 (error "~S: index too small." index)
+	 (do ((count index (1- count)))
+	     ((= count 0) (car sequence))
+	   (declare (fixnum count))
+	   (if (atom sequence)
+	       (error "~S: index too large." index)
+	       (setq sequence (cdr sequence))))))
+    (vector
+     (when (>= index (length sequence))
+       (error "~S: index too large." index))
+     (aref sequence index))))
 
 (defun %setelt (sequence index newval)
   "Store NEWVAL as the component of SEQUENCE specified by INDEX."
-  (if (listp sequence)
-      (if (< index 0)
-	  (error "~S: index too small." index)
-	  (do ((count index (1- count))
-	       (seq sequence))
-	      ((= count 0) (rplaca seq newval) sequence)
-	    (declare (fixnum count))
-	    (if (atom (cdr seq))
-		(error "~S: index too large." index)
-		(setq seq (cdr seq)))))
-      (setf (aref sequence index) newval)))
+  (etypecase sequence
+    (list
+     (if (< index 0)
+	 (error "~S: index too small." index)
+	 (do ((count index (1- count))
+	      (seq sequence))
+	     ((= count 0) (rplaca seq newval) sequence)
+	   (declare (fixnum count))
+	   (if (atom (cdr seq))
+	       (error "~S: index too large." index)
+	       (setq seq (cdr seq))))))
+    (vector
+     (when (>= index (length sequence))
+       (error "~S: index too large." index))
+     (setf (aref sequence index) newval))))
+
 
 (defun length (sequence)
   "Returns an integer that is the length of SEQUENCE."
-- 
GitLab