Match new[] and delete[]

.. this would otherwise cause a memory use error in the unusual case
where a numeric identifer held more letters than expected.

For instance, passing the following file as the input to tst_inverse_attr3:

ISO-10303-21;
HEADER;
FILE_DESCRIPTION(('SCL test file'),'2;1');
FILE_NAME('test_inverse_attr.p21','2012-06-30T',('mp'),(''),'0','1','2');
FILE_SCHEMA(('test_inverse_attr'));
ENDSEC;
DATA;
ENDSEC;
END-ISO-10303-21;

and running it under valgrind can cause a diagnostic similar to the following to be displayed:
Mismatched free() / delete / delete []
   at 0x4C2D2DB: operator delete(void*) (vg_replace_malloc.c:576)
   by 0x507A5A6: sectionReader::readInstanceNumber() (sectionReader.cc:224)
   by 0x507CCC7: lazyP21DataSectionReader::nextInstance() (lazyP21DataSectionReader.cc:53)
   by 0x507C797: lazyP21DataSectionReader::lazyP21DataSectionReader(lazyFileReader*, std::basic_ifstream<char, std::char_traits<char> >&, std::fpos<__mbstate_t>, unsigned short) (lazyP21DataSectionReader.cc:11)
   by 0x50699F2: lazyFileReader::initP21() (lazyFileReader.cc:14)
   by 0x5069E5D: lazyFileReader::lazyFileReader(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, lazyInstMgr*, unsigned short) (lazyFileReader.cc:61)
] and delete[]
   by 0x506AAA7: lazyInstMgr::openFile(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >) (lazyInstMgr.cc:103)
   by 0x4023A1: main (inverse_attr3.cc:35)
 Address 0x6a861a0 is 0 bytes inside a block of size 21 alloc'd
   at 0x4C2C93F: operator new[](unsigned long) (vg_replace_malloc.c:423)
   by 0x507A3A7: sectionReader::readInstanceNumber() (sectionReader.cc:202)

This problem is also reported in static analysis by clang, and as such can be seen in travis ci build logs.
This commit is contained in:
Jeff Epler 2017-08-15 21:09:48 -05:00
parent a96336ab97
commit 02910b2d38

View file

@ -221,7 +221,7 @@ instanceID sectionReader::readInstanceNumber() {
_error->UserMsg( "A very large instance ID encountered" );
_error->DetailMsg( errorMsg.str() );
delete buffer;
delete [] buffer;
return 0;
}