NetTopologySuite / NetTopologySuite.IO.ShapeFile

The ShapeFile IO module for NTS.
35 stars 25 forks source link

System.OverflowException when reading empty header #60

Closed kristofdegrave closed 3 years ago

kristofdegrave commented 3 years ago

If we write a feature without attributes, we get an System.OverflowException : Arithmetic operation resulted in an overflow when reading the header again. the error occurs on the following line: https://github.com/NetTopologySuite/NetTopologySuite.IO.ShapeFile/blob/c0819c1cfbf8c52146a0004e0e7b3a2b48044d98/src/NetTopologySuite.IO.ShapeFile/Dbase/DbaseFileHeader.cs#L303 This is because the _numFields is -1 and this is because of the calculation above, where the _headerLength is 0 and FileDescriptorSize defaults to 32. https://github.com/NetTopologySuite/NetTopologySuite.IO.ShapeFile/blob/c0819c1cfbf8c52146a0004e0e7b3a2b48044d98/src/NetTopologySuite.IO.ShapeFile/Dbase/DbaseFileHeader.cs#L300

Currently the header length is only set when adding or removing columns to the header. Add Column https://github.com/NetTopologySuite/NetTopologySuite.IO.ShapeFile/blob/c0819c1cfbf8c52146a0004e0e7b3a2b48044d98/src/NetTopologySuite.IO.ShapeFile/Dbase/DbaseFileHeader.cs#L207 Remove Column https://github.com/NetTopologySuite/NetTopologySuite.IO.ShapeFile/blob/c0819c1cfbf8c52146a0004e0e7b3a2b48044d98/src/NetTopologySuite.IO.ShapeFile/Dbase/DbaseFileHeader.cs#L242

My proposed solution is to check the headerLength when writing and set it to 33 when it is 0. https://github.com/NetTopologySuite/NetTopologySuite.IO.ShapeFile/blob/c0819c1cfbf8c52146a0004e0e7b3a2b48044d98/src/NetTopologySuite.IO.ShapeFile/Dbase/DbaseFileHeader.cs#L439

LevYas commented 3 years ago

Cool! Thank you for the issue and especially the fix. I've experienced it recently, but it was a deadline and I had to add a fake column as a workaround.