@@ -766,6 +766,44 @@ def test_reader_len_no_dbf_shx():
766766 assert len (sf ) == len (sf .shapes ())
767767
768768
769+ def test_reader_corrupt_files ():
770+ """
771+ Assert that reader is able to handle corrupt files by
772+ strictly going off the header information.
773+ """
774+ basename = "shapefiles/test/corrupt_too_long"
775+
776+ # write a shapefile with junk byte data at end of files
777+ with shapefile .Writer (basename ) as w :
778+ w .field ("test" , "C" , 50 )
779+ # add 10 line geoms
780+ for _ in range (10 ):
781+ w .record ("value" )
782+ w .line ([[(1 ,1 ),(1 ,2 ),(2 ,2 )]])
783+ # add junk byte data to end of dbf and shp files
784+ w .dbf .write (b'12345' )
785+ w .shp .write (b'12345' )
786+
787+ # read the corrupt shapefile and assert that it reads correctly
788+ with shapefile .Reader (basename ) as sf :
789+ # assert correct shapefile length metadata
790+ assert len (sf ) == sf .numRecords == sf .numShapes == 10
791+ # assert that records are read without error
792+ assert len (sf .records ()) == 10
793+ # assert that didn't read the extra junk data
794+ stopped = sf .dbf .tell ()
795+ sf .dbf .seek (0 , 2 )
796+ end = sf .dbf .tell ()
797+ assert (end - stopped ) == 5
798+ # assert that shapes are read without error
799+ assert len (sf .shapes ()) == 10
800+ # assert that didn't read the extra junk data
801+ stopped = sf .shp .tell ()
802+ sf .shp .seek (0 , 2 )
803+ end = sf .shp .tell ()
804+ assert (end - stopped ) == 5
805+
806+
769807def test_bboxfilter_shape ():
770808 """
771809 Assert that applying the bbox filter to shape() correctly ignores the shape
0 commit comments