hedgehogqa / haskell-hedgehog-classes

Hedgehog will eat your typeclass bugs
BSD 3-Clause "New" or "Revised" License
56 stars 17 forks source link

Read from the actual byte offset in 'storablePeekByte' #37

Closed ag-eitilt closed 3 years ago

ag-eitilt commented 3 years ago

Fixes #36. The TestStruct instance did indeed crash as described in that issue before the fix was applied. The exact calculation used for off will still run into issues with Storable instances where sizeOf (or, even less commonly, alignment) changes depending on the data, but there's an argument to be made that any such instance is poorly-written to begin with, and uncommon enough that the cost in complexity outweighs the benefits. For the record, a complete fix would require something like the following (untested):

foldl' (\ptr a -> (ptr `alignPtr` alignment a) `plusPtr` sizeOf a) nullPtr (take (max 0 $ ix - 1) as) `alignPtr` (as !! ix) `minusPtr` nullPtr