diff options
author | Yann Collet <Cyan4973@users.noreply.github.com> | 2020-11-08 20:47:19 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-11-08 20:47:19 -0800 |
commit | b8cd01183f1cd255e8581b48f5225f338dfa8c39 (patch) | |
tree | a18e6df6225c4380d8a3f4f5774d0697c36b1b41 | |
parent | be634559e3b6bb7bce77cc83ec2080b2bfb6c844 (diff) | |
parent | c76564b944d1fe6e4ffc6dc3d002becfc3c74b5d (diff) | |
download | lz4-b8cd01183f1cd255e8581b48f5225f338dfa8c39.tar.gz lz4-b8cd01183f1cd255e8581b48f5225f338dfa8c39.tar.bz2 lz4-b8cd01183f1cd255e8581b48f5225f338dfa8c39.zip |
Merge pull request #945 from lz4/fix926
fixed remaining ubsan warnings
-rw-r--r-- | lib/lz4.c | 2 | ||||
-rw-r--r-- | lib/lz4hc.c | 7 | ||||
-rw-r--r-- | tests/fuzzer.c | 24 |
3 files changed, 18 insertions, 15 deletions
@@ -1662,7 +1662,7 @@ int LZ4_saveDict (LZ4_stream_t* LZ4_dict, char* safeBuffer, int dictSize) if ((U32)dictSize > dict->dictSize) { dictSize = (int)dict->dictSize; } if (safeBuffer == NULL) assert(dictSize == 0); - if (safeBuffer != NULL) + if (dictSize > 0) memmove(safeBuffer, previousDictEnd - dictSize, dictSize); dict->dictionary = (const BYTE*)safeBuffer; diff --git a/lib/lz4hc.c b/lib/lz4hc.c index 8875f1a..286ff68 100644 --- a/lib/lz4hc.c +++ b/lib/lz4hc.c @@ -1164,13 +1164,16 @@ int LZ4_saveDictHC (LZ4_streamHC_t* LZ4_streamHCPtr, char* safeBuffer, int dictS if (dictSize > 64 KB) dictSize = 64 KB; if (dictSize < 4) dictSize = 0; if (dictSize > prefixSize) dictSize = prefixSize; - memmove(safeBuffer, streamPtr->end - dictSize, dictSize); + if (safeBuffer == NULL) assert(dictSize == 0); + if (dictSize > 0) + memmove(safeBuffer, streamPtr->end - dictSize, dictSize); { U32 const endIndex = (U32)(streamPtr->end - streamPtr->base); streamPtr->end = (const BYTE*)safeBuffer + dictSize; streamPtr->base = streamPtr->end - endIndex; streamPtr->dictLimit = endIndex - (U32)dictSize; streamPtr->lowLimit = endIndex - (U32)dictSize; - if (streamPtr->nextToUpdate < streamPtr->dictLimit) streamPtr->nextToUpdate = streamPtr->dictLimit; + if (streamPtr->nextToUpdate < streamPtr->dictLimit) + streamPtr->nextToUpdate = streamPtr->dictLimit; } return dictSize; } diff --git a/tests/fuzzer.c b/tests/fuzzer.c index 99361dd..ba36621 100644 --- a/tests/fuzzer.c +++ b/tests/fuzzer.c @@ -341,7 +341,7 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c exit(1); \ } -# define FUZ_CHECKTEST(cond, ...) { if (cond) { EXIT_MSG(__VA_ARGS__) } } +# define FUZ_CHECKTEST(cond, ...) if (cond) { EXIT_MSG(__VA_ARGS__) } # define FUZ_DISPLAYTEST(...) { \ testNb++; \ @@ -1135,7 +1135,7 @@ static void FUZ_unitTests(int compressionLevel) shct* const shc = (shct*)malloc(sizeof(*shc)); assert(shc != NULL); memset(shc, 0, sizeof(*shc)); - DISPLAYLEVEL(3, "state1(%p) state2(%p) state3(%p) LZ4_stream_t size(0x%x): ", + DISPLAYLEVEL(4, "state1(%p) state2(%p) state3(%p) LZ4_stream_t size(0x%x): ", &(shc->state1), &(shc->state2), &(shc->state3), (unsigned)sizeof(LZ4_stream_t)); FUZ_CHECKTEST( LZ4_initStream(&(shc->state1), sizeof(shc->state1)) == NULL, "state1 (%p) failed init", &(shc->state1) ); FUZ_CHECKTEST( LZ4_initStream(&(shc->state2), sizeof(shc->state2)) == NULL, "state2 (%p) failed init", &(shc->state2) ); @@ -1156,16 +1156,16 @@ static void FUZ_unitTests(int compressionLevel) { LZ4_stream_t streamingState; /* simple compression test */ - { U64 const crcOrig = XXH64(testInput, testCompressedSize, 0); - LZ4_initStream(&streamingState, sizeof(streamingState)); - { int const cs = LZ4_compress_fast_continue(&streamingState, testInput, testCompressed, testCompressedSize, testCompressedSize-1, 1); - FUZ_CHECKTEST(cs==0, "LZ4_compress_fast_continue() compression failed!"); - { int const r = LZ4_decompress_safe(testCompressed, testVerify, cs, testCompressedSize); - FUZ_CHECKTEST(r!=(int)testCompressedSize, "LZ4_decompress_safe() decompression failed"); - } } - { U64 const crcNew = XXH64(testVerify, testCompressedSize, 0); - FUZ_CHECKTEST(crcOrig!=crcNew, "LZ4_decompress_safe() decompression corruption"); + LZ4_initStream(&streamingState, sizeof(streamingState)); + { int const cs = LZ4_compress_fast_continue(&streamingState, testInput, testCompressed, testCompressedSize, testCompressedSize-1, 1); + FUZ_CHECKTEST(cs==0, "LZ4_compress_fast_continue() compression failed!"); + { int const r = LZ4_decompress_safe(testCompressed, testVerify, cs, testCompressedSize); + FUZ_CHECKTEST(r!=(int)testCompressedSize, "LZ4_decompress_safe() decompression failed"); } } + { U64 const crcOrig = XXH64(testInput, testCompressedSize, 0); + U64 const crcNew = XXH64(testVerify, testCompressedSize, 0); + FUZ_CHECKTEST(crcOrig!=crcNew, "LZ4_decompress_safe() decompression corruption"); + } /* early saveDict */ DISPLAYLEVEL(3, "saveDict (right after init) : "); @@ -1245,7 +1245,7 @@ static void FUZ_unitTests(int compressionLevel) shct* const shc = (shct*)malloc(sizeof(*shc)); assert(shc != NULL); memset(shc, 0, sizeof(*shc)); - DISPLAYLEVEL(3, "hc1(%p) hc2(%p) hc3(%p) size(0x%x): ", + DISPLAYLEVEL(4, "hc1(%p) hc2(%p) hc3(%p) size(0x%x): ", &(shc->hc1), &(shc->hc2), &(shc->hc3), (unsigned)sizeof(LZ4_streamHC_t)); FUZ_CHECKTEST( LZ4_initStreamHC(&(shc->hc1), sizeof(shc->hc1)) == NULL, "hc1 (%p) failed init", &(shc->hc1) ); FUZ_CHECKTEST( LZ4_initStreamHC(&(shc->hc2), sizeof(shc->hc2)) == NULL, "hc2 (%p) failed init", &(shc->hc2) ); |