Changeset 12384

Show
Ignore:
Timestamp:
10/12/07 16:37:55 (9 months ago)
Author:
warmerdam
Message:

avoid crashing given corrupt exif info, better error checking (#1904)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • sandbox/warmerdam/1.4-esri/gdal/frmts/jpeg/jpgdataset.cpp

    r10990 r12384  
    397397/*      Read number of entry in directory                               */ 
    398398/* -------------------------------------------------------------------- */ 
    399     VSIFSeekL(fp, nOffset+nTIFFHEADER, SEEK_SET); 
    400  
    401     if(VSIFReadL(&nEntryCount,1,sizeof(GUInt16),fp) != sizeof(GUInt16))  
     399    if( VSIFSeekL(fp, nOffset+nTIFFHEADER, SEEK_SET) != 0  
     400        || VSIFReadL(&nEntryCount,1,sizeof(GUInt16),fp) != sizeof(GUInt16) ) 
     401    { 
    402402        CPLError( CE_Failure, CPLE_AppDefined, 
    403                   "Error directory count"); 
     403                  "Error reading EXIF Directory count at %d.", 
     404                  nOffset + nTIFFHEADER ); 
     405        return CE_Failure; 
     406    } 
    404407 
    405408    if (bSwabflag) 
     
    410413        return CE_None; 
    411414 
     415    // Some files are corrupt, a large entry count is a sign of this. 
     416    if( nEntryCount > 125 ) 
     417    { 
     418        CPLError( CE_Warning, CPLE_AppDefined, 
     419                  "Ignoring EXIF directory with unlikely entry count (%d).", 
     420                  nEntryCount ); 
     421        return CE_Warning; 
     422    } 
     423 
    412424    poTIFFDir = (TIFFDirEntry *)CPLMalloc(nEntryCount * sizeof(TIFFDirEntry)); 
    413425 
    414426    if (poTIFFDir == NULL)  
     427    { 
    415428        CPLError( CE_Failure, CPLE_AppDefined, 
    416429                  "No space for TIFF directory"); 
     430        return CE_Failure; 
     431    } 
    417432   
    418433/* -------------------------------------------------------------------- */ 
     
    421436    n = VSIFReadL(poTIFFDir, 1,nEntryCount*sizeof(TIFFDirEntry),fp); 
    422437    if (n != nEntryCount*sizeof(TIFFDirEntry))  
     438    { 
    423439        CPLError( CE_Failure, CPLE_AppDefined, 
    424440                  "Could not read all directories"); 
     441        return CE_Failure; 
     442    } 
    425443 
    426444/* -------------------------------------------------------------------- */ 
     
    439457/* -------------------------------------------------------------------- */ 
    440458        pszName[0] = '\0'; 
     459        pszTemp[0] = '\0'; 
    441460 
    442461        for (poExifTags = tagnames; poExifTags->tag; poExifTags++) 
     
    559578/*      The data is being read where tdir_offset point to in the file   */ 
    560579/* -------------------------------------------------------------------- */ 
    561         else { 
    562  
     580        else if (space > 0 && space < MAXSTRINGLENGTH)  
     581        { 
    563582            unsigned char *data = (unsigned char *)CPLMalloc(space); 
    564583 
     
    601620            } 
    602621        } 
     622        else 
     623        { 
     624            CPLError( CE_Warning, CPLE_AppDefined, 
     625                      "Invalid EXIF header size: %ld, ignoring tag.", space ); 
     626        } 
     627 
    603628        papszMetadata = CSLSetNameValue(papszMetadata, pszName, pszTemp); 
    604629    }