NetTopologySuite / NetTopologySuite.IO.ShapeFile

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

NullReferenceException in ShapeDataReader #79

Closed ALVIS-Software closed 2 years ago

ALVIS-Software commented 2 years ago

https://github.com/NetTopologySuite/NetTopologySuite.IO.ShapeFile/blob/63dd59f2e1614a7e79dee780e0be001643ff892d/src/NetTopologySuite.IO.ShapeFile/ShapefileDataReader.FullFat.cs#L89

After checking the 2 read call results there is not return if one/both have return false. Adding 2 lines fixes a NRE in next call: if (!_moreRecords) return false;

DGuidi commented 2 years ago

cab you add a unit test (or at least some sample data) that reproduces the error? looking at the code, before returning falsewe should reset _columnValues private member, probably.

ALVIS-Software commented 2 years ago

Thank you for feedback. The error occures for empty shapefiles (0 features), where already the first read call will fail. That's why I added the return clause before trying to access features's attributes.

DGuidi commented 2 years ago

Can you post an unit test and some data to see what happens? This can be helpful

ALVIS-Software commented 2 years ago

Please use this test case to reproduce bug and confirm my fix above. Put it it ShapeFileDataWriterTests.cs, line 55.

[Test]
        public void TestWriteAndReadEmptyShapefile()
        {
            const string filename = "__empty";
            TestCreateEmptyShapefile();

            using (ShapefileDataReader shpReader = new ShapefileDataReader(filename, Factory))
            {
                var success = shpReader.Read();
                Assert.Pass();
                Assert.IsFalse(success);
            }
        }

It reuses one existing test that creates and empty file. Without my fix, the shpReader.Read(); will throw the NRE. With fix applied, it will pass.

DGuidi commented 2 years ago

fixed with 477fd02a770ea72fd17766a290f686320f308a3d