-
Notifications
You must be signed in to change notification settings - Fork 31
fromList causes segfault if list doesn't contain rows*columns elements #64
Comments
Hi! Thanks for reporting this issue, and sorry for my late reply. I was a bit confused as to why the segfault was happening, but when I tried some stuff in GHCi I was left even more confused:
Wat? Where does that empty string even come from? And why does the segfault occur only after |
Hi @Daniel-Diaz, thank you for answering!
First off, I found out where the segfault is coming from. Relevant here is that -- | Variant of 'getElem' that returns Maybe instead of an error.
safeGet :: Int -> Int -> Matrix a -> Maybe a
safeGet i j a@(M n m _ _ _ _)
| i > n || j > m || i < 1 || j < 1 = Nothing
| otherwise = Just $ unsafeGet i j a So the function only considers whether the index is too large or too little for the matrix. But with
(The above doesn't actually work for me in GHCi, but in a compiled file it does.) -- | /O(1)/ Unsafe indexing without bounds checking
unsafeIndex :: Vector v a => v a -> Int -> a
{-# INLINE_FUSED unsafeIndex #-}
unsafeIndex v i = UNSAFE_CHECK(checkIndex) "unsafeIndex" i (length v)
$ unId (basicUnsafeIndexM v i) So this However, as of vector-0.12.3.1, these changes are not yet in the Cabal package.
I don't really know, but the same thing can be expressed using only Data.Vector:
So as to where it's coming from: it's gotta be the mixture of
Not sure but the
I have no idea why it's spitting out that number first and only segfaults the second time. I've gotten a ton of weird results while testing this, but if I do the same thing in a compiled Haskell file I get the segfault everytime. So it might have something to do with the implementation of Other SolutionsInstead of just adding an error function to
which not only makes no sense when reading it, but also does not help in understanding that in fact the matrix is not 2x2. Another solution could be to just not use | otherwise = v V.! (encode w (i+ro,j+co)) (Or maybe use (BTW, this is my first GitHub contribution so please bear with me (and/or tell me) if any of this is unncessary or something.) |
Segmentation Fault
The above causes a segfault and crashes the program if
rows*cols > (1+length list)
. This is bad because the compiler does not catch the exception and so the program crashes while running and leaves no trace of where or why the error occured. I understand that the documentation says thatfromList
has to have a list with rows*cols amount of elements but there should still be an error message to determine where this error occured and why. This should preferrably happen at compile time, not while the program is running.Reproduce
In ghci:
Like I said, this becomes a huge problem once you define multiple matrices in your program and perhaps even change their shape etc. In ghci you can easily see where the problem occured but in a normal, compiled .hs file, you'll need tons of print statements just to see which matrix caused the error.
The text was updated successfully, but these errors were encountered: