From 63f5286086ff0eb2e5541d6e7c1741df54832d79 Mon Sep 17 00:00:00 2001
From: pw <pw>
Date: Tue, 6 Aug 2002 17:32:24 +0000
Subject: [PATCH] Fix read-sequence for specialized arrays to handle case where
 system:read-n-bytes reads short. Problem noted by Todd Sabin.

---
 code/stream.lisp | 50 ++++++++++++++++++++++++++++++------------------
 1 file changed, 31 insertions(+), 19 deletions(-)

diff --git a/code/stream.lisp b/code/stream.lisp
index d503547f6..efd823c9d 100644
--- a/code/stream.lisp
+++ b/code/stream.lisp
@@ -5,7 +5,7 @@
 ;;; Carnegie Mellon University, and has been placed in the public domain.
 ;;;
 (ext:file-comment
-  "$Header: /Volumes/share2/src/cmucl/cvs2git/cvsroot/src/code/stream.lisp,v 1.50 2002/06/10 22:41:24 toy Exp $")
+  "$Header: /Volumes/share2/src/cmucl/cvs2git/cvsroot/src/code/stream.lisp,v 1.51 2002/08/06 17:32:24 pw Exp $")
 ;;;
 ;;; **********************************************************************
 ;;;
@@ -1895,28 +1895,40 @@ POSITION: an INTEGER greater than or equal to zero, and less than or
 
 	  ((/= vm:byte-bits 8)
 	   ;; We must resort to the READ-BYTE based operation
-	   ;; also in this case.
+	   ;; also in this case. XXX Unreachable code note.
 	   (read-into-vector s stream start end))
 
 	  ;; Otherwise we can do something more interesting.
 	  (t
-	   (flet ((read-n-x8-bytes (stream data offset-start offset-end byte-size)
-		    (let* ((x8-mult (truncate byte-size 8))
-			   (numbytes (* (- offset-end offset-start) x8-mult))
-			   (bytes-read (system:read-n-bytes stream
-							    data
-							    offset-start
-							    numbytes
-							    nil))
-			   )
-		      ;; A check should probably be made here in order to
-		      ;; be sure that we actually read the right amount
-		      ;; of bytes. (I.e. (truncate bytes-read x8-mult)
-		      ;; should return a 0 second value.
-		      (if (< bytes-read numbytes)
-			  (+ offset-start (truncate bytes-read x8-mult))
-			  offset-end)))
-		  )
+	   (labels
+	       ((get-n-bytes (stream data offset numbytes)
+		  ;; Handle case of read-n-bytes reading short.
+		  (let ((need numbytes))
+		    (loop
+		      (let ((n (read-n-bytes stream data offset need nil)))
+			(decf need n)
+			(cond ((or (zerop need) ; Complete
+				   (zerop n))   ; EOF
+			       (return (- numbytes need)))
+			      (t (incf offset n)))))))
+		(read-n-x8-bytes (stream data offset-start offset-end byte-size)
+		  (let* ((x8-mult (truncate byte-size 8))
+			 (numbytes (* (- offset-end offset-start) x8-mult))
+			 (bytes-read (get-n-bytes
+				      stream
+				      data
+				      offset-start
+				      numbytes))
+			 )
+		    ;; A check should probably be made here in order to
+		    ;; be sure that we actually read the right amount
+		    ;; of bytes. (I.e. (truncate bytes-read x8-mult)
+		    ;; should return a 0 second value.
+		    (if (< bytes-read numbytes)
+			(+ offset-start (truncate bytes-read x8-mult))
+			offset-end)))
+		)
+	   
 	     ;; According to the definition of OPEN and READ-N-BYTES,
 	     ;; these are the only cases when we can use the multi-byte read
 	     ;; operation on a binary stream.
-- 
GitLab