[llvm] cd7bc0e - Support: Avoid using SmallVector::set_size() in zlib

Duncan P. N. Exon Smith via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 8 16:22:52 PST 2021


Author: Duncan P. N. Exon Smith
Date: 2021-12-08T16:22:37-08:00
New Revision: cd7bc0e010a3c0b4eb349e8c9a0e7edd591a5fff

URL: https://github.com/llvm/llvm-project/commit/cd7bc0e010a3c0b4eb349e8c9a0e7edd591a5fff
DIFF: https://github.com/llvm/llvm-project/commit/cd7bc0e010a3c0b4eb349e8c9a0e7edd591a5fff.diff

LOG: Support: Avoid using SmallVector::set_size() in zlib

Stop using `SmallVector::set_size()` in zlib. Replace pairs of
`reserve()` / `set_size()` with `resize_for_overwrite()` and
`truncate()`.

Differential Revision: https://reviews.llvm.org/D115391

Added: 
    

Modified: 
    llvm/lib/Support/Compression.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Support/Compression.cpp b/llvm/lib/Support/Compression.cpp
index b8c77cf69b95f..ccf6ef4bb6622 100644
--- a/llvm/lib/Support/Compression.cpp
+++ b/llvm/lib/Support/Compression.cpp
@@ -49,14 +49,14 @@ bool zlib::isAvailable() { return true; }
 Error zlib::compress(StringRef InputBuffer,
                      SmallVectorImpl<char> &CompressedBuffer, int Level) {
   unsigned long CompressedSize = ::compressBound(InputBuffer.size());
-  CompressedBuffer.reserve(CompressedSize);
+  CompressedBuffer.resize_for_overwrite(CompressedSize);
   int Res =
       ::compress2((Bytef *)CompressedBuffer.data(), &CompressedSize,
                   (const Bytef *)InputBuffer.data(), InputBuffer.size(), Level);
   // Tell MemorySanitizer that zlib output buffer is fully initialized.
   // This avoids a false report when running LLVM with uninstrumented ZLib.
   __msan_unpoison(CompressedBuffer.data(), CompressedSize);
-  CompressedBuffer.set_size(CompressedSize);
+  CompressedBuffer.truncate(CompressedSize);
   return Res ? createError(convertZlibCodeToString(Res)) : Error::success();
 }
 
@@ -74,10 +74,10 @@ Error zlib::uncompress(StringRef InputBuffer, char *UncompressedBuffer,
 Error zlib::uncompress(StringRef InputBuffer,
                        SmallVectorImpl<char> &UncompressedBuffer,
                        size_t UncompressedSize) {
-  UncompressedBuffer.reserve(UncompressedSize);
+  UncompressedBuffer.resize_for_overwrite(UncompressedSize);
   Error E =
       uncompress(InputBuffer, UncompressedBuffer.data(), UncompressedSize);
-  UncompressedBuffer.set_size(UncompressedSize);
+  UncompressedBuffer.truncate(UncompressedSize);
   return E;
 }
 


        


More information about the llvm-commits mailing list