From 02910b2d38654ed9bb1e144502d87fa7cc0afa38 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Tue, 15 Aug 2017 21:09:48 -0500 Subject: [PATCH] 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 >&, std::fpos<__mbstate_t>, unsigned short) (lazyP21DataSectionReader.cc:11) by 0x50699F2: lazyFileReader::initP21() (lazyFileReader.cc:14) by 0x5069E5D: lazyFileReader::lazyFileReader(std::__cxx11::basic_string, std::allocator >, lazyInstMgr*, unsigned short) (lazyFileReader.cc:61) ] and delete[] by 0x506AAA7: lazyInstMgr::openFile(std::__cxx11::basic_string, std::allocator >) (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. --- src/cllazyfile/sectionReader.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cllazyfile/sectionReader.cc b/src/cllazyfile/sectionReader.cc index bd3d9c23..65ccd254 100644 --- a/src/cllazyfile/sectionReader.cc +++ b/src/cllazyfile/sectionReader.cc @@ -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; }