[PATCH] D129698: [llvm] fix zlib buffer truncate edge cases and fix nits in tests
Cole Kissane via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Wed Jul 13 14:57:49 PDT 2022
ckissane created this revision.
ckissane added reviewers: phosek, MaskRay.
Herald added subscribers: StephenFan, hiraditya, mgorny.
Herald added a project: All.
ckissane requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.
- add check before truncating (un)compressed data buffer if the buffer is already a perfect length, to avoid triggering truncate assertion in edge case.
- explictly coerce LLVM_ENABLE_ZLIB to a 0 or 1 value in OFF case, to match current ON, FORCE_ON behavior.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D129698
Files:
llvm/cmake/config-ix.cmake
llvm/lib/Support/Compression.cpp
llvm/unittests/Support/CompressionTest.cpp
Index: llvm/unittests/Support/CompressionTest.cpp
===================================================================
--- llvm/unittests/Support/CompressionTest.cpp
+++ llvm/unittests/Support/CompressionTest.cpp
@@ -23,11 +23,9 @@
namespace {
#if LLVM_ENABLE_ZLIB
-
-void TestZlibCompression(StringRef Input, int Level) {
+static void testZlibCompression(StringRef Input, int Level) {
SmallString<32> Compressed;
SmallString<32> Uncompressed;
-
zlib::compress(Input, Compressed, Level);
// Check that uncompressed buffer is the same as original.
@@ -43,26 +41,24 @@
}
TEST(CompressionTest, Zlib) {
- TestZlibCompression("", zlib::DefaultCompression);
+ testZlibCompression("", zlib::DefaultCompression);
- TestZlibCompression("hello, world!", zlib::NoCompression);
- TestZlibCompression("hello, world!", zlib::BestSizeCompression);
- TestZlibCompression("hello, world!", zlib::BestSpeedCompression);
- TestZlibCompression("hello, world!", zlib::DefaultCompression);
+ testZlibCompression("hello, world!", zlib::NoCompression);
+ testZlibCompression("hello, world!", zlib::BestSizeCompression);
+ testZlibCompression("hello, world!", zlib::BestSpeedCompression);
+ testZlibCompression("hello, world!", zlib::DefaultCompression);
const size_t kSize = 1024;
char BinaryData[kSize];
- for (size_t i = 0; i < kSize; ++i) {
+ for (size_t i = 0; i < kSize; ++i)
BinaryData[i] = i & 255;
- }
StringRef BinaryDataStr(BinaryData, kSize);
- TestZlibCompression(BinaryDataStr, zlib::NoCompression);
- TestZlibCompression(BinaryDataStr, zlib::BestSizeCompression);
- TestZlibCompression(BinaryDataStr, zlib::BestSpeedCompression);
- TestZlibCompression(BinaryDataStr, zlib::DefaultCompression);
+ testZlibCompression(BinaryDataStr, zlib::NoCompression);
+ testZlibCompression(BinaryDataStr, zlib::BestSizeCompression);
+ testZlibCompression(BinaryDataStr, zlib::BestSpeedCompression);
+ testZlibCompression(BinaryDataStr, zlib::DefaultCompression);
}
-
#endif
}
Index: llvm/lib/Support/Compression.cpp
===================================================================
--- llvm/lib/Support/Compression.cpp
+++ llvm/lib/Support/Compression.cpp
@@ -57,7 +57,8 @@
// 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.truncate(CompressedSize);
+ if (CompressedSize < CompressedBuffer.size())
+ CompressedBuffer.truncate(CompressedSize);
}
Error zlib::uncompress(StringRef InputBuffer, char *UncompressedBuffer,
@@ -79,7 +80,8 @@
UncompressedBuffer.resize_for_overwrite(UncompressedSize);
Error E = zlib::uncompress(InputBuffer, UncompressedBuffer.data(),
UncompressedSize);
- UncompressedBuffer.truncate(UncompressedSize);
+ if (UncompressedSize < UncompressedBuffer.size())
+ UncompressedBuffer.truncate(UncompressedSize);
return E;
}
Index: llvm/cmake/config-ix.cmake
===================================================================
--- llvm/cmake/config-ix.cmake
+++ llvm/cmake/config-ix.cmake
@@ -134,6 +134,8 @@
endif()
endif()
set(LLVM_ENABLE_ZLIB "${HAVE_ZLIB}")
+else()
+ set(LLVM_ENABLE_ZLIB 0)
endif()
if(LLVM_ENABLE_LIBXML2)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D129698.444428.patch
Type: text/x-patch
Size: 3340 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220713/3a42e33f/attachment.bin>
More information about the llvm-commits
mailing list