Opened 19 years ago

Last modified 19 years ago

#806 closed defect (fixed)

GDAL TIFFOpen in tif_vsi.c doesn't close the file if TIFFClientOpen fails

Reported by: dem@… Owned by: warmerdam
Priority: high Milestone:
Component: GDAL_Raster Version: unspecified
Severity: normal Keywords:
Cc:

Description

In the libtiff tif_unix.c TIFFOpen close the file if TIFFFdOpen fails :
#ifdef _AM29K
	fd = open(name, m);
#else
	fd = open(name, m, 0666);
#endif
	if (fd < 0) {
		TIFFError(module, "%s: Cannot open", name);
		return ((TIFF *)0);
	}

	tif = TIFFFdOpen((int)fd, name, mode);
	if(!tif)
		close(fd);
	return tif;

but in GDAL tif_vsi.c TIFFOpen, this isn't the case :
        fp = VSIFOpenL( name, access );
	if (fp == NULL) {
		TIFFError(module, "%s: Cannot open", name);
		return ((TIFF *)0);
	}

	tif = TIFFClientOpen(name, mode,
	    (thandle_t) fp,
	    _tiffReadProc, _tiffWriteProc,
	    _tiffSeekProc, _tiffCloseProc, _tiffSizeProc,
	    _tiffMapProc, _tiffUnmapProc);

        if( tif != NULL )
            tif->tif_fd = 0;
        
	return tif;

The simple patch seems to be :
        if( tif != NULL )
            tif->tif_fd = 0;
        else
            VSIFCloseL( fp );

Why the tif->tif_fd = 0; line ?

I believe the reason of this bug is that in standard libtiff tif_unix.c the
close when error was moved from TIFFClientOpen to TIFFOpen, but this wasn't
reported in the GDAL tif_vsi.c ...
Note that for GDAL it's a minor bug because TIFFOpen shouldn't be directly
called (except by me ; I use the libtiff function in GDAL), and in the GeoTIFF
driver, GDAL test if it's a TIFF file with the file byte header before call
TIFFOpen.

Julien

Change History (1)

comment:1 by warmerdam, 19 years ago

Patch applied as suggested. 

I don't know why the tif_fd is set to 0. Likely just to avoid 
"reference uninitialized memory" errors from memory checkers. 

Note: See TracTickets for help on using tickets.