Quid2 / flat

Principled and efficient binary serialization
BSD 3-Clause "New" or "Revised" License
60 stars 17 forks source link

Failure to `unflat` a Sum type #30

Closed freckletonj closed 2 years ago

freckletonj commented 2 years ago

I've worked a ways to minimize this example, and while it's not perfectly minimal, I think it demonstrates what seems like a bug to me. Any guidance? Or am I just using it wrong?

{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE TypeApplications #-}

module Flat_Fails where

import Flat
import Flat.Instances.Vector
import Data.Vector.Storable (Vector)
import qualified Data.Vector.Storable as V

data Foo =
    A
  | B (Vector Double)
  | C (Vector Double) (Vector Double)
  deriving (Generic, Flat, Show)

test :: IO ()
test = do
  print $ unflat @Foo . flat $ A
  print $ unflat @Foo . flat $ B (V.fromList [7,8])
  print $ unflat @Foo . flat $ C (V.fromList [7,8]) (V.fromList [9, 10])

The Output:

λ> test
Right A
Left (TooMuchSpace (0x000000420cd4b3b3,S {currPtr = 0x000000420cd4b3a1, usedBits = 4}))
Left (NotEnoughSpace (0x000000420cd4b4a5,S {currPtr = 0x000000420cd4b4a2, usedBits = 2}))
tittoassini commented 2 years ago

This is due to an encoding bug in vector that could sometime occur when the vector did not start on a byte boundary.

This is fixed in github, and will be in the new 0.5 version that I hope to release in the few days.

Best