From 1f9a45fdfb16188ee9c95a2601602cad0bf1a256 Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Mon, 3 Nov 2014 20:49:17 +0000 Subject: Fix a leak found by asan. llvm-svn: 221179 --- lld/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) (limited to 'lld/lib/ReaderWriter') diff --git a/lld/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp b/lld/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp index 1fb3291f82d..5f0f73ae14c 100644 --- a/lld/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp +++ b/lld/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp @@ -872,7 +872,8 @@ private: void reorderSEHTableEntriesX64(uint8_t *bufferStart); void addChunk(Chunk *chunk); - void addSectionChunk(SectionChunk *chunk, SectionHeaderTableChunk *table); + void addSectionChunk(std::unique_ptr chunk, + SectionHeaderTableChunk *table); void setImageSizeOnDisk(); uint64_t calcSectionSize(llvm::COFF::SectionCharacteristics sectionType) const; @@ -982,9 +983,10 @@ void PECOFFWriter::build(const File &linkedFile) { for (auto i : atoms) { StringRef sectionName = i.first; std::vector &contents = i.second; - auto *section = new AtomChunk(_ctx, sectionName, contents); + std::unique_ptr section( + new AtomChunk(_ctx, sectionName, contents)); if (section->size() > 0) - addSectionChunk(section, sectionTable); + addSectionChunk(std::move(section), sectionTable); } // Build atom to its RVA map. @@ -996,12 +998,12 @@ void PECOFFWriter::build(const File &linkedFile) { // relocated. So we can create the ".reloc" section which contains // all the relocation sites. if (_ctx.getBaseRelocationEnabled()) { - BaseRelocChunk *baseReloc = new BaseRelocChunk(_chunks, _ctx); + std::unique_ptr baseReloc(new BaseRelocChunk(_chunks, _ctx)); if (baseReloc->size()) { - addSectionChunk(baseReloc, sectionTable); + SectionChunk &ref = *baseReloc; + addSectionChunk(std::move(baseReloc), sectionTable); dataDirectory->setField(DataDirectoryIndex::BASE_RELOCATION_TABLE, - baseReloc->getVirtualAddress(), - baseReloc->size()); + ref.getVirtualAddress(), ref.size()); } } @@ -1177,18 +1179,19 @@ void PECOFFWriter::addChunk(Chunk *chunk) { _chunks.push_back(std::unique_ptr(chunk)); } -void PECOFFWriter::addSectionChunk(SectionChunk *chunk, +void PECOFFWriter::addSectionChunk(std::unique_ptr chunk, SectionHeaderTableChunk *table) { - _chunks.push_back(std::unique_ptr(chunk)); - table->addSection(chunk); + SectionChunk &ref = *chunk; + _chunks.push_back(std::move(chunk)); + table->addSection(&ref); _numSections++; // Compute and set the starting address of sections when loaded in // memory. They are different from positions on disk because sections need // to be sector-aligned on disk but page-aligned in memory. - chunk->setVirtualAddress(_imageSizeInMemory); + ref.setVirtualAddress(_imageSizeInMemory); _imageSizeInMemory = llvm::RoundUpToAlignment( - _imageSizeInMemory + chunk->size(), _ctx.getPageSize()); + _imageSizeInMemory + ref.size(), _ctx.getPageSize()); } void PECOFFWriter::setImageSizeOnDisk() { -- cgit v1.2.3