diff --git a/lld/ELF/OutputSections.cpp b/lld/ELF/OutputSections.cpp index cffde5d61ac91e..f01f74f44d22c1 100644 --- a/lld/ELF/OutputSections.cpp +++ b/lld/ELF/OutputSections.cpp @@ -337,9 +337,9 @@ template void OutputSection::maybeCompress() { hdr->ch_size = size; hdr->ch_addralign = alignment; - // Write section contents to a temporary buffer and compress it. - std::vector buf(size); - writeTo(buf.data()); + // Write uncompressed data to a temporary zero-initialized buffer. + auto buf = std::make_unique(size); + writeTo(buf.get()); // We chose 1 as the default compression level because it is the fastest. If // -O2 is given, we use level 6 to compress debug info more by ~15%. We found // that level 7 to 9 doesn't make much difference (~1% more compression) while @@ -350,9 +350,9 @@ template void OutputSection::maybeCompress() { constexpr size_t shardSize = 1 << 20; const size_t numShards = (size + shardSize - 1) / shardSize; auto shardsIn = std::make_unique[]>(numShards); - for (size_t i = 0, start = 0, end; start != buf.size(); ++i, start = end) { - end = std::min(start + shardSize, buf.size()); - shardsIn[i] = makeArrayRef(buf.data() + start, end - start); + for (size_t i = 0, start = 0, end; start != size; ++i, start = end) { + end = std::min(start + shardSize, size); + shardsIn[i] = makeArrayRef(buf.get() + start, end - start); } // Compress shards and compute Alder-32 checksums. Use Z_SYNC_FLUSH for all