ofLoadImage android crash on bad png files

I have a scenario where I download images and then load them using ofLoadImage. Sometimes when the image is bad (for example, half a download), this line will crash on Android.

I attached such a bad image that I recreated very simply: I took a 75k png file and truncated it to be only 35k. Two images. The original one (to prove that the source is not a bad image) and the truncated one.

Now, this error is uncatchable. I guess it just cannot recover from it.

I followed the code to this line in ofImage.cpp:

bmp = FreeImage_Load(fif, fileName.c_str(), 0);

It seems like it’s assuming the validity of the file and just can’t cope with a partial image. I searched the FreeImage reference for any way to validate images before attempting to load, but couldn’t find anything.

Any idea on what can be done in order to avoid crashing? Currently my best solution is to persist which file I am loading, so if I crash, I’ll re-download that file next time. But that’s just stupid.