diff --git a/src/osgPlugins/shp/ESRIShape.cpp b/src/osgPlugins/shp/ESRIShape.cpp index c2872ec5d..32c87d6bf 100644 --- a/src/osgPlugins/shp/ESRIShape.cpp +++ b/src/osgPlugins/shp/ESRIShape.cpp @@ -62,6 +62,26 @@ inline bool readVal( int fd, T &val, ByteOrder bo = LittleEndian ) return true; } + +template +inline bool readPositiveVal( int fd, T &val, ByteOrder bo = LittleEndian ) +{ + int nbytes = 0; + if( (nbytes = esri::read( fd, &val, sizeof(T))) <= 0 ) + return false; + + if( getByteOrder() != bo ) + swapBytes(val); + + if (val<0) + { + val = 0; + return false; + } + + return true; +} + inline void printType( ShapeType type ) { printf( "%s", @@ -286,7 +306,7 @@ bool MultiPoint::read( int fd ) if( bbox.read(fd) == false ) return false; - if( readVal(fd, numPoints, LittleEndian ) == false ) + if( readPositiveVal(fd, numPoints, LittleEndian ) == false ) return false; points = new struct Point[numPoints]; @@ -354,10 +374,10 @@ bool PolyLine::read( int fd ) if( bbox.read(fd) == false ) return false; - if( readVal(fd, numParts, LittleEndian ) == false ) + if( readPositiveVal(fd, numParts, LittleEndian ) == false ) return false; - if( readVal(fd, numPoints, LittleEndian ) == false ) + if( readPositiveVal(fd, numPoints, LittleEndian ) == false ) return false; parts = new Integer[numParts]; @@ -426,10 +446,10 @@ bool Polygon::read( int fd ) if( bbox.read(fd) == false ) return false; - if( readVal(fd, numParts, LittleEndian ) == false ) + if( readPositiveVal(fd, numParts, LittleEndian ) == false ) return false; - if( readVal(fd, numPoints, LittleEndian ) == false ) + if( readPositiveVal(fd, numPoints, LittleEndian ) == false ) return false; parts = new Integer[numParts]; @@ -544,7 +564,7 @@ bool MultiPointM::read( int fd ) if( bbox.read(fd) == false ) return false; - if( readVal(fd, numPoints, LittleEndian ) == false ) + if( readPositiveVal(fd, numPoints, LittleEndian ) == false ) return false; points = new struct Point[numPoints]; @@ -637,10 +657,10 @@ bool PolyLineM::read( int fd ) if( bbox.read(fd) == false ) return false; - if( readVal(fd, numParts, LittleEndian ) == false ) + if( readPositiveVal(fd, numParts, LittleEndian ) == false ) return false; - if( readVal(fd, numPoints, LittleEndian ) == false ) + if( readPositiveVal(fd, numPoints, LittleEndian ) == false ) return false; parts = new Integer[numParts]; @@ -735,10 +755,10 @@ bool PolygonM::read( int fd ) if( bbox.read(fd) == false ) return false; - if( readVal(fd, numParts, LittleEndian ) == false ) + if( readPositiveVal(fd, numParts, LittleEndian ) == false ) return false; - if( readVal(fd, numPoints, LittleEndian ) == false ) + if( readPositiveVal(fd, numPoints, LittleEndian ) == false ) return false; parts = new Integer[numParts]; @@ -883,7 +903,7 @@ bool MultiPointZ::read( int fd ) if( bbox.read(fd) == false ) return false; - if( readVal(fd, numPoints, LittleEndian ) == false ) + if( readPositiveVal(fd, numPoints, LittleEndian ) == false ) return false; points = new struct Point[numPoints]; @@ -1000,10 +1020,10 @@ bool PolyLineZ::read( int fd ) if( bbox.read(fd) == false ) return false; - if( readVal(fd, numParts, LittleEndian ) == false ) + if( readPositiveVal(fd, numParts, LittleEndian ) == false ) return false; - if( readVal(fd, numPoints, LittleEndian ) == false ) + if( readPositiveVal(fd, numPoints, LittleEndian ) == false ) return false; parts = new Integer[numParts];