Twinside / Juicy.Pixels

Haskell library to load & save pictures

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

jpeg causing hang on decode.

benclifford opened this issue · comments

Jpeg decoding hangs for one image in our library. I'd expect it to either work or return an error.

To recreate:
git clone https://github.com/benclifford/juicypixels-decode-bug
stack build
stack exec test-exe

The image is a.jpeg in that repository, and the test case calls readImage on it and then forces the evaluation a bit.

It looks like getNextIntJpg is looping forever, due to missing its termination condition. The patch below causes execution to crash when getNextIntJpg enters an illegal state, as a further demonstration of what I think is going wrong.

I have tested this against juicypixels from stack LTS-5.12, and from git repo commit 6ca1bd2.

diff --git a/src/Codec/Picture/BitWriter.hs b/src/Codec/Picture/BitWriter.hs
index 35dd7dd..2e355a7 100644
--- a/src/Codec/Picture/BitWriter.hs
+++ b/src/Codec/Picture/BitWriter.hs
@@ -112,7 +112,7 @@ getNextIntJpg :: Int -> BoolReader s Int32
 {-# INLINE getNextIntJpg #-}
 getNextIntJpg = go 0 where
   go !acc !0 = return acc
-  go !acc !n = do
+  go !acc !n | n>=0 = do
     BoolState idx v chain <- S.get
     let !leftBits = 1 + fromIntegral idx
     if n >= leftBits then do

I'll look after it, but not before June, I don't have access to a computer right now