On Fri, Mar 18, 2011 at 5:58 PM, Matthew Mondor <mm_lists@pulsar-zone.net> wrote:
Hello,

When looking at the HyperSpec on those functions, it seems undefined
what happens when a supplied index is not a "valid array index", thus,
from 0 below the array size.  The exception is ELT, which should signal
a condition of type TYPE-ERROR.

In practice however, it seems that implementations attempt to signal an
error condition for invalid index access.

In SBCL, AREF, SVREF, CHAR, SCHAR will signal a uniform condition of
type SB-INT:INVALID-ARRAY-INDEX-ERROR:

Which version is this? I get this one with the latest update from Fink

 (handler-case (let ((a (make-array '(2 3)))) (aref a 2 1)) (error (c) (print c)))
 => #<TYPE-ERROR expected-type: BIT datum: 2>
 
Index 4 out of bounds for <insert array type here>,
should be nonnegative and <<insert size here>.

Although a different behaviour for ELT:
The index 4 is too large. [SB-KERNEL:INDEX-TOO-LARGE-ERROR]


For ECL, the condition type and error message varies, and SVREF even
behaves differently than the others:

CHAR, SCHAR: [SIMPLE-ERROR]
AREF: [SIMPLE-TYPE-ERROR]
SVREF: (this differs in interpreted and compiled mode)
Interpreted mode: In function SVREF, the index into the object 4. takes a value #(1 2 3) out of the range (INTEGER 0 2). [SIMPLE-TYPE-ERROR]
Compiled mode: Returns the supplied vector instead of signaling an error.
ELT: [SIMPLE-TYPE-ERROR]

I have changed this. CHAR, SCHAR, SVREF and ELT now use the same error reporting function. I also fixed a typo in AREF.

SVREF in compiled mode, though, may be directly translated into a vector access, if optimization levels are large enough. In this case no error checking is performed and the value you may get is completely random, as Pascal explained. Compare

> (funcall (compile 'nil '(lambda (x) (svref x 3))) #(1 2 3))
[...]
Condition of type: SIMPLE-TYPE-ERROR
In function AREF, the index into the object
 #(1 2 3).
takes a value 3 out of the range (INTEGER 0 2).
[...]
> (funcall (compile 'nil '(lambda (x) (declare (optimize (safety 0))) (svref x 3))) #(1 2 3))
[...]
Condition of type: SEGMENTATION-VIOLATION
Detected access to an invalid or protected memory address.
Available restarts:

Juanjo

--
Instituto de Física Fundamental, CSIC
c/ Serrano, 113b, Madrid 28006 (Spain)
http://juanjose.garciaripoll.googlepages.com