[Gdal-dev] Use of assert() on troublesome files

Frank Warmerdam warmerdam at pobox.com
Fri Mar 5 01:14:59 EST 2004


Ben Discoe wrote:
> It was fixable by changing the assert:
> 
> fmts/gtiff/libtiff.c, line 606
> < 	assert(sp->cinfo.comm.is_decompressor);
> ----
> 
>>	if (sp->cinfo.comm.is_decompressor == 0) {
>>		TIFFError("JPEGSetupDecode", "JPEG is not decompressor");
>>		return (0);
>>	}

Ben,

I assume you are referring to tif_jpeg.c?  My understanding is that
control should never get into JPEGSetupDecode() with the object not setup
in decompressor mode.  What I mean is, I think this is indicative of a logical
flaw in the code.  The sort of thing that might be suitably handled via an
assert().  There have been changes in this area.  Is it possible you are
using libtiff code that is a few months out of date?

> 1. Perhaps there is some way to trap these assert() failures in an app
> calling GDAL?

There isn't really much that can be done about asserts() at runtime.
In GDAL level code I normally use CPLAssert() which could in theory be
hooked to escape death via a setjmp()/longjmp() call or a C++ exception
but things wouldn't be properly cleaned up so it would still be very dangerous.

> 2. Perhaps assert() should be used only to detect cases of a logical flaw in
> the code, rather than unexpected input data?

I completely agree.  Assert() is only appropriate for identifying error
conditions that a user or bad data should not be able to cause.  But rather
to verify that preconditions expected at a particular location are being met.
In fact, in production builds the assert() is normally compiled out completely
so it is useless as a general error handler.

Assert should be used to help identify code problems close to the source
but are not a real "error handling" construct.

Best regards,
-- 
---------------------------------------+--------------------------------------
I set the clouds in motion - turn up   | Frank Warmerdam, warmerdam at pobox.com
light and sound - activate the windows | http://pobox.com/~warmerdam
and watch the world go round - Rush    | Geospatial Programmer for Rent




More information about the Gdal-dev mailing list