I am using giflib, and have discovered that if I open a new file before closing the previous one, all hell breaks loose.
- DGifOpen (file A)
- DGifSlurp (file A)
- DGifOpen (file B)
- DGifSlurp (file B) - fails with D_GIF_ERR_READ_FAILED
Which is remarkable, since I can verify with a custom read function that every read succeeds and returns the number of bytes requested.
But...
- DGifOpen (file A)
- DGifSlurp (file A)
- DGifClose (file A)
- DGifOpen (file B)
- DGifSlurp (file B) - works!
The documentation says giflib isre-entrant and thread-safe
.
So why does it matter if there are multiple open files? GifFileType should be encapsulating all state; and there is no overlap between user-side data structures for file A and file B.
Update & related question: Is giflib intended to safely handle malicious input files, or is it the wrong tool for that scenario? I do not see any automated use of valgrind in the test system, and I can't seem to find the CI server for the project.
Update: This has nothing to do with overlapped files, but rather with the fact that giflib 5.1.2 only works when the heap is zeroed - uninitialized reads are causing the failure. Overlapping reads were just a way to trigger that.
To reproduce, download giflib 5.1.2, and
- Run
./autogen.sh
- Run
make check
- Then
cd util && cat ../pic/porsche.gif | valgrind .libs/lt-gifsponge
- Watch valgrind report the uninitialized read.