Infinite loop at EOF in libpng

Björn Ganster <[email protected]> Tue, 25 May 2021 15:08:37 +0200
Newsgroups gmane.comp.graphics.png.devel
Message-ID <[email protected]>
Hi all,

I'm Björn Ganster, working for X-Ways Software Technology AG. We use 
libpng as part of our software to display images of all kinds via ResIL 
(https://sourceforge.net/projects/resil/), which requires us to also 
support image files in various states of decay (e.g., files that are cut 
off or where the ending has been overwritten by junk).

Normally, libpng works very well for us. However, I have been sent a 
file that leads to an infinite loop in libpng. This file is obviously 
not a normal png, it seems that the file end is missing. ResIL provides 
a callback function for reading data, and libpng's png_read_data funtion 
in pngrio.c calls this function. The problem here is that the callback 
function has no way of indicating that less than the required number of 
bytes is available, and so of course libpng cannot check whether the 
read request was fully handled. In the case at hand, the callback 
refuses to read any bytes beyond and of file (EOF), leading to an 
infinite loop between libpng and the callback. I was able to reach an 
error state with this png file by setting all bytes in the returned 
buffer to zero, which libpng recognized as an invalid marker, but this 
is no more than a workaround. I would prefer that the callback returns 
the number of bytes that were actually read, that libpng treats missing 
bytes as an error and stops reading further bytes.

However, there is an additional catch about this. We do not simply want 
libpng to treat incomplete pngs as errors and give up. Instead, we would 
like to display partial pngs as much as possible: if a file is 
incomplete, the parts of the file that can still be read should be 
displayed. Of course, libpng should mark the file as incomplete by 
setting error messages somewhere, but we need the file to be read to the 
extend that this is possible.

If I can help make this possible, please indicate how this should 
happen. I am not confident enough in my knowledge of libpng to judge 
whether there are solutions in place that I do not know about, or the 
best way of creating a patch to do this.

I hope we can find some common ground about this
With kind regards
Björn Ganster

---

X-Ways Software Technology AG • Contact details: 
http://www.x-ways.net/corporate/contact.html
Registered at Carl-Diem-Str. 32, 32257 Bünde, Germany • Amtsgericht Bad 
Oeynhausen HRB 7475



_______________________________________________
png-mng-implement mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/png-mng-implement