Ticket #2120 (closed bug: fixed)
Arrays allow out-of-bounds indexes
|Reported by:||amthrax||Owned by:|
|Type of failure:||Difficulty:||Unknown|
|Test Case:||Blocked By:|
The array changes proposed and recently implemented for ticket #1610 (Make arrays safer) strengthened the requirements on Ix instances, but weakened the requirements on array users. Specifically, array referencing now permits indexes that are not inRange.
import Data.Array.IArray b :: Array (Int,Int) Int b = listArray ((0,0), (3,3)) (repeat 0) main = do print (b ! (0,5)) -- SHOULD fail, but doesn't print (index (bounds b) (0,5)) -- DOES fail
The first line in main should fail because the specified index is not inRange, but doesn't because the (!) operator (specifically Data.Array.Base.safeIndex) now uses unsafeIndex and simply checks that the resulting index is within the linearized bounds of the array. In this case, the unsafe index of (0,5) wraps aroung to the index of (1,1).
A simple fix would be to use index instead of unsafeIndex in the implementation of Data.Array.Base.safeIndex. This would both require the user to use in-bounds references and would require Ix instances to return in-bounds indexes.