Merge pull request #581 from lz4/cppcheck

Added cppcheck static analyzer test
This commit is contained in:
Yann Collet 2018-09-19 13:48:46 -07:00 committed by GitHub
commit 21277d27ec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 325 additions and 299 deletions

View File

@ -129,13 +129,14 @@ matrix:
- qemu-user-static - qemu-user-static
- gcc-powerpc-linux-gnu - gcc-powerpc-linux-gnu
- env: Ubu=14.04 Cmd='make staticAnalyze' COMPILER=clang - env: Ubu=14.04 Cmd='make staticAnalyze && make cppcheck' COMPILER=clang
dist: trusty dist: trusty
sudo: required sudo: required
addons: addons:
apt: apt:
packages: packages:
- clang - clang
- cppcheck
- env: Ubu=14.04 Cmd='make clean all CC=gcc-4.4 MOREFLAGS=-Werror && make clean && CFLAGS=-fPIC LDFLAGS="-pie -fPIE -D_FORTIFY_SOURCE=2" make -C programs' COMPILER=gcc-4.4 - env: Ubu=14.04 Cmd='make clean all CC=gcc-4.4 MOREFLAGS=-Werror && make clean && CFLAGS=-fPIC LDFLAGS="-pie -fPIE -D_FORTIFY_SOURCE=2" make -C programs' COMPILER=gcc-4.4
dist: trusty dist: trusty

View File

@ -148,9 +148,14 @@ usan: clean
usan32: clean usan32: clean
CFLAGS="-m32 -O3 -g -fsanitize=undefined" $(MAKE) test FUZZER_TIME="-T30s" NB_LOOPS=-i1 CFLAGS="-m32 -O3 -g -fsanitize=undefined" $(MAKE) test FUZZER_TIME="-T30s" NB_LOOPS=-i1
.PHONY: staticAnalyze
staticAnalyze: clean staticAnalyze: clean
CFLAGS=-g scan-build --status-bugs -v $(MAKE) all CFLAGS=-g scan-build --status-bugs -v $(MAKE) all
.PHONY: cppcheck
cppcheck:
cppcheck . --force --enable=warning,portability,performance,style --error-exitcode=1 > /dev/null
platformTest: clean platformTest: clean
@echo "\n ---- test lz4 with $(CC) compiler ----" @echo "\n ---- test lz4 with $(CC) compiler ----"
@$(CC) -v @$(CC) -v

View File

@ -67,14 +67,13 @@ vector<string> get_lines(vector<string>& input, int& linenum, string terminator)
{ {
vector<string> out; vector<string> out;
string line; string line;
size_t epos;
while ((size_t)linenum < input.size()) { while ((size_t)linenum < input.size()) {
line = input[linenum]; line = input[linenum];
if (terminator.empty() && line.empty()) { linenum--; break; } if (terminator.empty() && line.empty()) { linenum--; break; }
epos = line.find(terminator); size_t const epos = line.find(terminator);
if (!terminator.empty() && epos!=string::npos) { if (!terminator.empty() && epos!=string::npos) {
out.push_back(line); out.push_back(line);
break; break;
@ -152,8 +151,9 @@ int main(int argc, char *argv[]) {
continue; continue;
} }
/* comments of type /**< and /*!< are detected and only function declaration is highlighted (bold) */ /* comments of type / * * < and / * ! < are detected, and only function declaration is highlighted (bold) */
if ((line.find("/**<")!=string::npos || line.find("/*!<")!=string::npos) && line.find("*/")!=string::npos) { if ((line.find("/**<")!=string::npos || line.find("/*!<")!=string::npos)
&& line.find("*/")!=string::npos) {
sout << "<pre><b>"; sout << "<pre><b>";
print_line(sout, line); print_line(sout, line);
sout << "</b></pre><BR>" << endl; sout << "</b></pre><BR>" << endl;
@ -177,10 +177,13 @@ int main(int argc, char *argv[]) {
comments = get_lines(input, linenum, "*/"); comments = get_lines(input, linenum, "*/");
if (!comments.empty()) comments[0] = line.substr(spos+3); if (!comments.empty()) comments[0] = line.substr(spos+3);
if (!comments.empty()) comments[comments.size()-1] = comments[comments.size()-1].substr(0, comments[comments.size()-1].find("*/")); if (!comments.empty())
comments[comments.size()-1] = comments[comments.size()-1].substr(0, comments[comments.size()-1].find("*/"));
for (l=0; l<comments.size(); l++) { for (l=0; l<comments.size(); l++) {
if (comments[l].find(" *")==0) comments[l] = comments[l].substr(2); if (comments[l].compare(0, 2, " *") == 0)
else if (comments[l].find(" *")==0) comments[l] = comments[l].substr(3); comments[l] = comments[l].substr(2);
else if (comments[l].compare(0, 3, " *") == 0)
comments[l] = comments[l].substr(3);
trim(comments[l], "*-="); trim(comments[l], "*-=");
} }
while (!comments.empty() && comments[comments.size()-1].empty()) comments.pop_back(); // remove empty line at the end while (!comments.empty() && comments[comments.size()-1].empty()) comments.pop_back(); // remove empty line at the end
@ -194,7 +197,6 @@ int main(int argc, char *argv[]) {
sout << "<pre><b>"; sout << "<pre><b>";
for (l=0; l<lines.size(); l++) { for (l=0; l<lines.size(); l++) {
// fprintf(stderr, "line[%d]=%s\n", l, lines[l].c_str());
print_line(sout, lines[l]); print_line(sout, lines[l]);
} }
sout << "</b><p>"; sout << "</b><p>";

View File

@ -99,7 +99,7 @@ static void test_decompress(
uint16_t cmpBytes = 0; uint16_t cmpBytes = 0;
if (read_uint16(inpFp, &cmpBytes) != 1) break; if (read_uint16(inpFp, &cmpBytes) != 1) break;
if (cmpBytes <= 0) break; if (cmpBytes == 0) break;
if (read_bin(inpFp, cmpBuf, cmpBytes) != cmpBytes) break; if (read_bin(inpFp, cmpBuf, cmpBytes) != cmpBytes) break;
{ {

View File

@ -88,7 +88,6 @@
void run_screaming(const char *message, const int code) { void run_screaming(const char *message, const int code) {
printf("%s\n", message); printf("%s\n", message);
exit(code); exit(code);
return;
} }
@ -343,19 +342,19 @@ int main(int argc, char **argv) {
printf("%s", separator); printf("%s", separator);
printf(header_format, "Source", "Function Benchmarked", "Total Seconds", "Iterations/sec", "ns/Iteration", "% of default"); printf(header_format, "Source", "Function Benchmarked", "Total Seconds", "Iterations/sec", "ns/Iteration", "% of default");
printf("%s", separator); printf("%s", separator);
printf(format, "Normal Text", "LZ4_compress_default()", (double)time_taken__default / BILLION, (int)(iterations / ((double)time_taken__default /BILLION)), time_taken__default / iterations, (double)time_taken__default * 100 / time_taken__default); printf(format, "Normal Text", "LZ4_compress_default()", (double)time_taken__default / BILLION, (int)(iterations / ((double)time_taken__default /BILLION)), (int)time_taken__default / iterations, (double)time_taken__default * 100 / time_taken__default);
printf(format, "Normal Text", "LZ4_compress_fast()", (double)time_taken__fast / BILLION, (int)(iterations / ((double)time_taken__fast /BILLION)), time_taken__fast / iterations, (double)time_taken__fast * 100 / time_taken__default); printf(format, "Normal Text", "LZ4_compress_fast()", (double)time_taken__fast / BILLION, (int)(iterations / ((double)time_taken__fast /BILLION)), (int)time_taken__fast / iterations, (double)time_taken__fast * 100 / time_taken__default);
printf(format, "Normal Text", "LZ4_compress_fast_extState()", (double)time_taken__fast_extstate / BILLION, (int)(iterations / ((double)time_taken__fast_extstate /BILLION)), time_taken__fast_extstate / iterations, (double)time_taken__fast_extstate * 100 / time_taken__default); printf(format, "Normal Text", "LZ4_compress_fast_extState()", (double)time_taken__fast_extstate / BILLION, (int)(iterations / ((double)time_taken__fast_extstate /BILLION)), (int)time_taken__fast_extstate / iterations, (double)time_taken__fast_extstate * 100 / time_taken__default);
//printf(format, "Normal Text", "LZ4_compress_generic()", (double)time_taken__generic / BILLION, (int)(iterations / ((double)time_taken__generic /BILLION)), time_taken__generic / iterations, (double)time_taken__generic * 100 / time_taken__default); //printf(format, "Normal Text", "LZ4_compress_generic()", (double)time_taken__generic / BILLION, (int)(iterations / ((double)time_taken__generic /BILLION)), (int)time_taken__generic / iterations, (double)time_taken__generic * 100 / time_taken__default);
printf(format, "Normal Text", "LZ4_decompress_safe()", (double)time_taken__decomp_safe / BILLION, (int)(iterations / ((double)time_taken__decomp_safe /BILLION)), time_taken__decomp_safe / iterations, (double)time_taken__decomp_safe * 100 / time_taken__default); printf(format, "Normal Text", "LZ4_decompress_safe()", (double)time_taken__decomp_safe / BILLION, (int)(iterations / ((double)time_taken__decomp_safe /BILLION)), (int)time_taken__decomp_safe / iterations, (double)time_taken__decomp_safe * 100 / time_taken__default);
printf(format, "Normal Text", "LZ4_decompress_fast()", (double)time_taken__decomp_fast / BILLION, (int)(iterations / ((double)time_taken__decomp_fast /BILLION)), time_taken__decomp_fast / iterations, (double)time_taken__decomp_fast * 100 / time_taken__default); printf(format, "Normal Text", "LZ4_decompress_fast()", (double)time_taken__decomp_fast / BILLION, (int)(iterations / ((double)time_taken__decomp_fast /BILLION)), (int)time_taken__decomp_fast / iterations, (double)time_taken__decomp_fast * 100 / time_taken__default);
printf(header_format, "", "", "", "", "", ""); printf(header_format, "", "", "", "", "", "");
printf(format, "Compressible", "LZ4_compress_default()", (double)time_taken_hc__default / BILLION, (int)(iterations / ((double)time_taken_hc__default /BILLION)), time_taken_hc__default / iterations, (double)time_taken_hc__default * 100 / time_taken_hc__default); printf(format, "Compressible", "LZ4_compress_default()", (double)time_taken_hc__default / BILLION, (int)(iterations / ((double)time_taken_hc__default /BILLION)), (int)time_taken_hc__default / iterations, (double)time_taken_hc__default * 100 / time_taken_hc__default);
printf(format, "Compressible", "LZ4_compress_fast()", (double)time_taken_hc__fast / BILLION, (int)(iterations / ((double)time_taken_hc__fast /BILLION)), time_taken_hc__fast / iterations, (double)time_taken_hc__fast * 100 / time_taken_hc__default); printf(format, "Compressible", "LZ4_compress_fast()", (double)time_taken_hc__fast / BILLION, (int)(iterations / ((double)time_taken_hc__fast /BILLION)), (int)time_taken_hc__fast / iterations, (double)time_taken_hc__fast * 100 / time_taken_hc__default);
printf(format, "Compressible", "LZ4_compress_fast_extState()", (double)time_taken_hc__fast_extstate / BILLION, (int)(iterations / ((double)time_taken_hc__fast_extstate /BILLION)), time_taken_hc__fast_extstate / iterations, (double)time_taken_hc__fast_extstate * 100 / time_taken_hc__default); printf(format, "Compressible", "LZ4_compress_fast_extState()", (double)time_taken_hc__fast_extstate / BILLION, (int)(iterations / ((double)time_taken_hc__fast_extstate /BILLION)), (int)time_taken_hc__fast_extstate / iterations, (double)time_taken_hc__fast_extstate * 100 / time_taken_hc__default);
//printf(format, "Compressible", "LZ4_compress_generic()", (double)time_taken_hc__generic / BILLION, (int)(iterations / ((double)time_taken_hc__generic /BILLION)), time_taken_hc__generic / iterations, (double)time_taken_hc__generic * 100 / time_taken_hc__default); //printf(format, "Compressible", "LZ4_compress_generic()", (double)time_taken_hc__generic / BILLION, (int)(iterations / ((double)time_taken_hc__generic /BILLION)), (int)time_taken_hc__generic / iterations, (double)time_taken_hc__generic * 100 / time_taken_hc__default);
printf(format, "Compressible", "LZ4_decompress_safe()", (double)time_taken_hc__decomp_safe / BILLION, (int)(iterations / ((double)time_taken_hc__decomp_safe /BILLION)), time_taken_hc__decomp_safe / iterations, (double)time_taken_hc__decomp_safe * 100 / time_taken_hc__default); printf(format, "Compressible", "LZ4_decompress_safe()", (double)time_taken_hc__decomp_safe / BILLION, (int)(iterations / ((double)time_taken_hc__decomp_safe /BILLION)), (int)time_taken_hc__decomp_safe / iterations, (double)time_taken_hc__decomp_safe * 100 / time_taken_hc__default);
printf(format, "Compressible", "LZ4_decompress_fast()", (double)time_taken_hc__decomp_fast / BILLION, (int)(iterations / ((double)time_taken_hc__decomp_fast /BILLION)), time_taken_hc__decomp_fast / iterations, (double)time_taken_hc__decomp_fast * 100 / time_taken_hc__default); printf(format, "Compressible", "LZ4_decompress_fast()", (double)time_taken_hc__decomp_fast / BILLION, (int)(iterations / ((double)time_taken_hc__decomp_fast /BILLION)), (int)time_taken_hc__decomp_fast / iterations, (double)time_taken_hc__decomp_fast * 100 / time_taken_hc__default);
printf("%s", separator); printf("%s", separator);
printf("\n"); printf("\n");
printf("All done, ran %d iterations per test.\n", iterations); printf("All done, ran %d iterations per test.\n", iterations);

View File

@ -70,11 +70,12 @@ compress_file_internal(FILE* f_in, FILE* f_out,
/* write frame header */ /* write frame header */
{ size_t const headerSize = LZ4F_compressBegin(ctx, outBuff, outCapacity, &kPrefs); { size_t const headerSize = LZ4F_compressBegin(ctx, outBuff, outCapacity, &kPrefs);
if (LZ4F_isError(headerSize)) { if (LZ4F_isError(headerSize)) {
printf("Failed to start compression: error %zu\n", headerSize); printf("Failed to start compression: error %u \n", (unsigned)headerSize);
return result; return result;
} }
count_out = headerSize; count_out = headerSize;
printf("Buffer size is %zu bytes, header size %zu bytes\n", outCapacity, headerSize); printf("Buffer size is %u bytes, header size %u bytes \n",
(unsigned)outCapacity, (unsigned)headerSize);
safe_fwrite(outBuff, 1, headerSize, f_out); safe_fwrite(outBuff, 1, headerSize, f_out);
} }
@ -89,11 +90,11 @@ compress_file_internal(FILE* f_in, FILE* f_out,
inBuff, readSize, inBuff, readSize,
NULL); NULL);
if (LZ4F_isError(compressedSize)) { if (LZ4F_isError(compressedSize)) {
printf("Compression failed: error %zu\n", compressedSize); printf("Compression failed: error %u \n", (unsigned)compressedSize);
return result; return result;
} }
printf("Writing %zu bytes\n", compressedSize); printf("Writing %u bytes\n", (unsigned)compressedSize);
safe_fwrite(outBuff, 1, compressedSize, f_out); safe_fwrite(outBuff, 1, compressedSize, f_out);
count_out += compressedSize; count_out += compressedSize;
} }
@ -103,11 +104,11 @@ compress_file_internal(FILE* f_in, FILE* f_out,
outBuff, outCapacity, outBuff, outCapacity,
NULL); NULL);
if (LZ4F_isError(compressedSize)) { if (LZ4F_isError(compressedSize)) {
printf("Failed to end compression: error %zu\n", compressedSize); printf("Failed to end compression: error %u \n", (unsigned)compressedSize);
return result; return result;
} }
printf("Writing %zu bytes\n", compressedSize); printf("Writing %u bytes \n", (unsigned)compressedSize);
safe_fwrite(outBuff, 1, compressedSize, f_out); safe_fwrite(outBuff, 1, compressedSize, f_out);
count_out += compressedSize; count_out += compressedSize;
} }

View File

@ -19,7 +19,6 @@
void run_screaming(const char* message, const int code) { void run_screaming(const char* message, const int code) {
printf("%s \n", message); printf("%s \n", message);
exit(code); exit(code);
return;
} }

View File

@ -522,14 +522,15 @@ static U32 LZ4_hash4(U32 sequence, tableType_t const tableType)
static U32 LZ4_hash5(U64 sequence, tableType_t const tableType) static U32 LZ4_hash5(U64 sequence, tableType_t const tableType)
{ {
static const U64 prime5bytes = 889523592379ULL;
static const U64 prime8bytes = 11400714785074694791ULL;
const U32 hashLog = (tableType == byU16) ? LZ4_HASHLOG+1 : LZ4_HASHLOG; const U32 hashLog = (tableType == byU16) ? LZ4_HASHLOG+1 : LZ4_HASHLOG;
if (LZ4_isLittleEndian()) if (LZ4_isLittleEndian()) {
const U64 prime5bytes = 889523592379ULL;
return (U32)(((sequence << 24) * prime5bytes) >> (64 - hashLog)); return (U32)(((sequence << 24) * prime5bytes) >> (64 - hashLog));
else } else {
const U64 prime8bytes = 11400714785074694791ULL;
return (U32)(((sequence >> 24) * prime8bytes) >> (64 - hashLog)); return (U32)(((sequence >> 24) * prime8bytes) >> (64 - hashLog));
} }
}
LZ4_FORCE_INLINE U32 LZ4_hashPosition(const void* const p, tableType_t const tableType) LZ4_FORCE_INLINE U32 LZ4_hashPosition(const void* const p, tableType_t const tableType)
{ {
@ -1414,14 +1415,16 @@ LZ4_decompress_generic(
const size_t dictSize /* note : = 0 if noDict */ const size_t dictSize /* note : = 0 if noDict */
) )
{ {
const BYTE* ip = (const BYTE*) src; if (src == NULL) return -1;
{ const BYTE* ip = (const BYTE*) src;
const BYTE* const iend = ip + srcSize; const BYTE* const iend = ip + srcSize;
BYTE* op = (BYTE*) dst; BYTE* op = (BYTE*) dst;
BYTE* const oend = op + outputSize; BYTE* const oend = op + outputSize;
BYTE* cpy; BYTE* cpy;
const BYTE* const dictEnd = (const BYTE*)dictStart + dictSize; const BYTE* const dictEnd = (dictStart == NULL) ? NULL : dictStart + dictSize;
const unsigned inc32table[8] = {0, 1, 2, 1, 0, 4, 4, 4}; const unsigned inc32table[8] = {0, 1, 2, 1, 0, 4, 4, 4};
const int dec64table[8] = {0, 0, 0, -1, -4, 1, 2, 3}; const int dec64table[8] = {0, 0, 0, -1, -4, 1, 2, 3};
@ -1436,7 +1439,6 @@ LZ4_decompress_generic(
/* Special cases */ /* Special cases */
assert(lowPrefix <= op); assert(lowPrefix <= op);
assert(src != NULL);
if ((endOnInput) && (unlikely(outputSize==0))) return ((srcSize==1) && (*ip==0)) ? 0 : -1; /* Empty output buffer */ if ((endOnInput) && (unlikely(outputSize==0))) return ((srcSize==1) && (*ip==0)) ? 0 : -1; /* Empty output buffer */
if ((!endOnInput) && (unlikely(outputSize==0))) return (*ip==0 ? 1 : -1); if ((!endOnInput) && (unlikely(outputSize==0))) return (*ip==0 ? 1 : -1);
if ((endOnInput) && unlikely(srcSize==0)) return -1; if ((endOnInput) && unlikely(srcSize==0)) return -1;
@ -1643,6 +1645,7 @@ _copy_match:
_output_error: _output_error:
return (int) (-(((const char*)ip)-src))-1; return (int) (-(((const char*)ip)-src))-1;
} }
}
/*===== Instantiate the API decoding functions. =====*/ /*===== Instantiate the API decoding functions. =====*/

View File

@ -635,7 +635,7 @@ size_t LZ4F_compressBegin_usingCDict(LZ4F_cctx* cctxPtr,
} } } }
cctxPtr->tmpIn = cctxPtr->tmpBuff; cctxPtr->tmpIn = cctxPtr->tmpBuff;
cctxPtr->tmpInSize = 0; cctxPtr->tmpInSize = 0;
XXH32_reset(&(cctxPtr->xxh), 0); (void)XXH32_reset(&(cctxPtr->xxh), 0);
/* context init */ /* context init */
cctxPtr->cdict = cdict; cctxPtr->cdict = cdict;
@ -887,7 +887,7 @@ size_t LZ4F_compressUpdate(LZ4F_cctx* cctxPtr,
} }
if (cctxPtr->prefs.frameInfo.contentChecksumFlag == LZ4F_contentChecksumEnabled) if (cctxPtr->prefs.frameInfo.contentChecksumFlag == LZ4F_contentChecksumEnabled)
XXH32_update(&(cctxPtr->xxh), srcBuffer, srcSize); (void)XXH32_update(&(cctxPtr->xxh), srcBuffer, srcSize);
cctxPtr->totalInSize += srcSize; cctxPtr->totalInSize += srcSize;
return dstPtr - dstStart; return dstPtr - dstStart;
@ -1366,7 +1366,7 @@ size_t LZ4F_decompress(LZ4F_dctx* dctx,
break; break;
case dstage_init: case dstage_init:
if (dctx->frameInfo.contentChecksumFlag) XXH32_reset(&(dctx->xxh), 0); if (dctx->frameInfo.contentChecksumFlag) (void)XXH32_reset(&(dctx->xxh), 0);
/* internal buffers allocation */ /* internal buffers allocation */
{ size_t const bufferNeeded = dctx->maxBlockSize { size_t const bufferNeeded = dctx->maxBlockSize
+ ((dctx->frameInfo.blockMode==LZ4F_blockLinked) * 128 KB); + ((dctx->frameInfo.blockMode==LZ4F_blockLinked) * 128 KB);
@ -1431,7 +1431,7 @@ size_t LZ4F_decompress(LZ4F_dctx* dctx,
/* next block is uncompressed */ /* next block is uncompressed */
dctx->tmpInTarget = nextCBlockSize; dctx->tmpInTarget = nextCBlockSize;
if (dctx->frameInfo.blockChecksumFlag) { if (dctx->frameInfo.blockChecksumFlag) {
XXH32_reset(&dctx->blockChecksum, 0); (void)XXH32_reset(&dctx->blockChecksum, 0);
} }
dctx->dStage = dstage_copyDirect; dctx->dStage = dstage_copyDirect;
break; break;
@ -1451,10 +1451,10 @@ size_t LZ4F_decompress(LZ4F_dctx* dctx,
size_t const sizeToCopy = MIN(dctx->tmpInTarget, minBuffSize); size_t const sizeToCopy = MIN(dctx->tmpInTarget, minBuffSize);
memcpy(dstPtr, srcPtr, sizeToCopy); memcpy(dstPtr, srcPtr, sizeToCopy);
if (dctx->frameInfo.blockChecksumFlag) { if (dctx->frameInfo.blockChecksumFlag) {
XXH32_update(&dctx->blockChecksum, srcPtr, sizeToCopy); (void)XXH32_update(&dctx->blockChecksum, srcPtr, sizeToCopy);
} }
if (dctx->frameInfo.contentChecksumFlag) if (dctx->frameInfo.contentChecksumFlag)
XXH32_update(&dctx->xxh, srcPtr, sizeToCopy); (void)XXH32_update(&dctx->xxh, srcPtr, sizeToCopy);
if (dctx->frameInfo.contentSize) if (dctx->frameInfo.contentSize)
dctx->frameRemainingSize -= sizeToCopy; dctx->frameRemainingSize -= sizeToCopy;

View File

@ -231,7 +231,6 @@ LZ4HC_InsertAndGetWiderMatch (
int matchChainPos = 0; int matchChainPos = 0;
U32 const pattern = LZ4_read32(ip); U32 const pattern = LZ4_read32(ip);
U32 matchIndex; U32 matchIndex;
U32 dictMatchIndex;
repeat_state_e repeat = rep_untested; repeat_state_e repeat = rep_untested;
size_t srcPatternLength = 0; size_t srcPatternLength = 0;
@ -349,8 +348,8 @@ LZ4HC_InsertAndGetWiderMatch (
if (dict == usingDictCtx && nbAttempts && ipIndex - lowestMatchIndex < MAX_DISTANCE) { if (dict == usingDictCtx && nbAttempts && ipIndex - lowestMatchIndex < MAX_DISTANCE) {
size_t const dictEndOffset = dictCtx->end - dictCtx->base; size_t const dictEndOffset = dictCtx->end - dictCtx->base;
U32 dictMatchIndex = dictCtx->hashTable[LZ4HC_hashPtr(ip)];
assert(dictEndOffset <= 1 GB); assert(dictEndOffset <= 1 GB);
dictMatchIndex = dictCtx->hashTable[LZ4HC_hashPtr(ip)];
matchIndex = dictMatchIndex + lowestMatchIndex - (U32)dictEndOffset; matchIndex = dictMatchIndex + lowestMatchIndex - (U32)dictEndOffset;
while (ipIndex - matchIndex <= MAX_DISTANCE && nbAttempts--) { while (ipIndex - matchIndex <= MAX_DISTANCE && nbAttempts--) {
const BYTE* const matchPtr = dictCtx->base + dictMatchIndex; const BYTE* const matchPtr = dictCtx->base + dictMatchIndex;

View File

@ -141,7 +141,7 @@ static int usage_advanced(const char* exeName)
DISPLAY( "--content-size : compressed frame includes original size (default:not present)\n"); DISPLAY( "--content-size : compressed frame includes original size (default:not present)\n");
DISPLAY( "--[no-]sparse : sparse mode (default:enabled on file, disabled on stdout)\n"); DISPLAY( "--[no-]sparse : sparse mode (default:enabled on file, disabled on stdout)\n");
DISPLAY( "--favor-decSpeed: compressed files decompress faster, but are less compressed \n"); DISPLAY( "--favor-decSpeed: compressed files decompress faster, but are less compressed \n");
DISPLAY( "--fast[=#]: switch to ultra fast compression level (default: %u)\n", 1); DISPLAY( "--fast[=#]: switch to ultra fast compression level (default: %i)\n", 1);
DISPLAY( "Benchmark arguments : \n"); DISPLAY( "Benchmark arguments : \n");
DISPLAY( " -b# : benchmark file(s), using # compression level (default : 1) \n"); DISPLAY( " -b# : benchmark file(s), using # compression level (default : 1) \n");
DISPLAY( " -e# : test all compression levels from -bX to # (default : 1)\n"); DISPLAY( " -e# : test all compression levels from -bX to # (default : 1)\n");

View File

@ -331,42 +331,48 @@ static int LZ4IO_LZ4_compress(const char* src, char* dst, int srcSize, int dstSi
* It generates compressed streams using the old 'legacy' format */ * It generates compressed streams using the old 'legacy' format */
int LZ4IO_compressFilename_Legacy(const char* input_filename, const char* output_filename, int compressionlevel) int LZ4IO_compressFilename_Legacy(const char* input_filename, const char* output_filename, int compressionlevel)
{ {
int (*compressionFunction)(const char* src, char* dst, int srcSize, int dstSize, int cLevel); typedef int (*compress_f)(const char* src, char* dst, int srcSize, int dstSize, int cLevel);
compress_f const compressionFunction = (compressionlevel < 3) ? LZ4IO_LZ4_compress : LZ4_compress_HC;
unsigned long long filesize = 0; unsigned long long filesize = 0;
unsigned long long compressedfilesize = MAGICNUMBER_SIZE; unsigned long long compressedfilesize = MAGICNUMBER_SIZE;
char* in_buff; char* in_buff;
char* out_buff; char* out_buff;
const int outBuffSize = LZ4_compressBound(LEGACY_BLOCKSIZE); const int outBuffSize = LZ4_compressBound(LEGACY_BLOCKSIZE);
FILE* finput; FILE* const finput = LZ4IO_openSrcFile(input_filename);
FILE* foutput; FILE* foutput;
clock_t clockEnd; clock_t clockEnd;
/* Init */ /* Init */
clock_t const clockStart = clock(); clock_t const clockStart = clock();
compressionFunction = (compressionlevel < 3) ? LZ4IO_LZ4_compress : LZ4_compress_HC; if (finput == NULL)
EXM_THROW(20, "%s : open file error ", input_filename);
finput = LZ4IO_openSrcFile(input_filename);
if (finput == NULL) EXM_THROW(20, "%s : open file error ", input_filename);
foutput = LZ4IO_openDstFile(output_filename); foutput = LZ4IO_openDstFile(output_filename);
if (foutput == NULL) { fclose(finput); EXM_THROW(20, "%s : open file error ", input_filename); } if (foutput == NULL) {
fclose(finput);
EXM_THROW(20, "%s : open file error ", input_filename);
}
/* Allocate Memory */ /* Allocate Memory */
in_buff = (char*)malloc(LEGACY_BLOCKSIZE); in_buff = (char*)malloc(LEGACY_BLOCKSIZE);
out_buff = (char*)malloc(outBuffSize); out_buff = (char*)malloc(outBuffSize + 4);
if (!in_buff || !out_buff) EXM_THROW(21, "Allocation error : not enough memory"); if (!in_buff || !out_buff)
EXM_THROW(21, "Allocation error : not enough memory");
/* Write Archive Header */ /* Write Archive Header */
LZ4IO_writeLE32(out_buff, LEGACY_MAGICNUMBER); LZ4IO_writeLE32(out_buff, LEGACY_MAGICNUMBER);
{ size_t const sizeCheck = fwrite(out_buff, 1, MAGICNUMBER_SIZE, foutput); { size_t const writeSize = fwrite(out_buff, 1, MAGICNUMBER_SIZE, foutput);
if (sizeCheck != MAGICNUMBER_SIZE) EXM_THROW(22, "Write error : cannot write header"); } if (writeSize != MAGICNUMBER_SIZE)
EXM_THROW(22, "Write error : cannot write header");
}
/* Main Loop */ /* Main Loop */
while (1) { while (1) {
unsigned int outSize; int outSize;
/* Read Block */ /* Read Block */
size_t const inSize = (int) fread(in_buff, (size_t)1, (size_t)LEGACY_BLOCKSIZE, finput); size_t const inSize = fread(in_buff, (size_t)1, (size_t)LEGACY_BLOCKSIZE, finput);
assert(inSize <= LEGACY_BLOCKSIZE);
if (inSize == 0) break; if (inSize == 0) break;
if (inSize > LEGACY_BLOCKSIZE) EXM_THROW(23, "Read error : wrong fread() size report "); /* should be impossible */
filesize += inSize; filesize += inSize;
/* Compress Block */ /* Compress Block */
@ -376,9 +382,11 @@ int LZ4IO_compressFilename_Legacy(const char* input_filename, const char* output
(int)(filesize>>20), (double)compressedfilesize/filesize*100); (int)(filesize>>20), (double)compressedfilesize/filesize*100);
/* Write Block */ /* Write Block */
LZ4IO_writeLE32(out_buff, outSize); assert(outSize > 0);
{ size_t const sizeCheck = fwrite(out_buff, 1, outSize+4, foutput); assert(outSize < outBuffSize);
if (sizeCheck!=(size_t)(outSize+4)) LZ4IO_writeLE32(out_buff, (unsigned)outSize);
{ size_t const writeSize = fwrite(out_buff, 1, outSize+4, foutput);
if (writeSize != (size_t)(outSize+4))
EXM_THROW(24, "Write error : cannot write compressed block"); EXM_THROW(24, "Write error : cannot write compressed block");
} } } }
if (ferror(finput)) EXM_THROW(25, "Error while reading %s ", input_filename); if (ferror(finput)) EXM_THROW(25, "Error while reading %s ", input_filename);

View File

@ -34,6 +34,7 @@ extern "C" {
#include <stdlib.h> /* malloc */ #include <stdlib.h> /* malloc */
#include <string.h> /* strlen, strncpy */ #include <string.h> /* strlen, strncpy */
#include <stdio.h> /* fprintf */ #include <stdio.h> /* fprintf */
#include <assert.h>
#include <sys/types.h> /* stat, utime */ #include <sys/types.h> /* stat, utime */
#include <sys/stat.h> /* stat */ #include <sys/stat.h> /* stat */
#if defined(_MSC_VER) #if defined(_MSC_VER)
@ -390,7 +391,7 @@ UTIL_STATIC void *UTIL_realloc(void *ptr, size_t size)
UTIL_STATIC int UTIL_prepareFileList(const char* dirName, char** bufStart, size_t* pos, char** bufEnd) UTIL_STATIC int UTIL_prepareFileList(const char* dirName, char** bufStart, size_t* pos, char** bufEnd)
{ {
char* path; char* path;
int dirLength, fnameLength, pathLength, nbFiles = 0; int dirLength, nbFiles = 0;
WIN32_FIND_DATAA cFile; WIN32_FIND_DATAA cFile;
HANDLE hFile; HANDLE hFile;
@ -411,7 +412,8 @@ UTIL_STATIC int UTIL_prepareFileList(const char *dirName, char** bufStart, size_
free(path); free(path);
do { do {
fnameLength = (int)strlen(cFile.cFileName); int pathLength;
int const fnameLength = (int)strlen(cFile.cFileName);
path = (char*) malloc(dirLength + fnameLength + 2); path = (char*) malloc(dirLength + fnameLength + 2);
if (!path) { FindClose(hFile); return 0; } if (!path) { FindClose(hFile); return 0; }
memcpy(path, dirName, dirLength); memcpy(path, dirName, dirLength);
@ -455,8 +457,7 @@ UTIL_STATIC int UTIL_prepareFileList(const char *dirName, char** bufStart, size_
{ {
DIR* dir; DIR* dir;
struct dirent * entry; struct dirent * entry;
char* path; int dirLength, nbFiles = 0;
int dirLength, fnameLength, pathLength, nbFiles = 0;
if (!(dir = opendir(dirName))) { if (!(dir = opendir(dirName))) {
fprintf(stderr, "Cannot open directory '%s': %s\n", dirName, strerror(errno)); fprintf(stderr, "Cannot open directory '%s': %s\n", dirName, strerror(errno));
@ -466,6 +467,8 @@ UTIL_STATIC int UTIL_prepareFileList(const char *dirName, char** bufStart, size_
dirLength = (int)strlen(dirName); dirLength = (int)strlen(dirName);
errno = 0; errno = 0;
while ((entry = readdir(dir)) != NULL) { while ((entry = readdir(dir)) != NULL) {
char* path;
int fnameLength, pathLength;
if (strcmp (entry->d_name, "..") == 0 || if (strcmp (entry->d_name, "..") == 0 ||
strcmp (entry->d_name, ".") == 0) continue; strcmp (entry->d_name, ".") == 0) continue;
fnameLength = (int)strlen(entry->d_name); fnameLength = (int)strlen(entry->d_name);
@ -523,12 +526,13 @@ UTIL_STATIC int UTIL_prepareFileList(const char *dirName, char** bufStart, size_
* After finishing usage of the list the structures should be freed with UTIL_freeFileList(params: return value, allocatedBuffer) * After finishing usage of the list the structures should be freed with UTIL_freeFileList(params: return value, allocatedBuffer)
* In case of error UTIL_createFileList returns NULL and UTIL_freeFileList should not be called. * In case of error UTIL_createFileList returns NULL and UTIL_freeFileList should not be called.
*/ */
UTIL_STATIC const char** UTIL_createFileList(const char **inputNames, unsigned inputNamesNb, char** allocatedBuffer, unsigned* allocatedNamesNb) UTIL_STATIC const char**
UTIL_createFileList(const char** inputNames, unsigned inputNamesNb, char** allocatedBuffer, unsigned* allocatedNamesNb)
{ {
size_t pos; size_t pos;
unsigned i, nbFiles; unsigned i, nbFiles;
char* buf = (char*)malloc(LIST_SIZE_INCREASE); char* buf = (char*)malloc(LIST_SIZE_INCREASE);
char* bufend = buf + LIST_SIZE_INCREASE; size_t bufSize = LIST_SIZE_INCREASE;
const char** fileTable; const char** fileTable;
if (!buf) return NULL; if (!buf) return NULL;
@ -536,20 +540,23 @@ UTIL_STATIC const char** UTIL_createFileList(const char **inputNames, unsigned i
for (i=0, pos=0, nbFiles=0; i<inputNamesNb; i++) { for (i=0, pos=0, nbFiles=0; i<inputNamesNb; i++) {
if (!UTIL_isDirectory(inputNames[i])) { if (!UTIL_isDirectory(inputNames[i])) {
size_t const len = strlen(inputNames[i]); size_t const len = strlen(inputNames[i]);
if (buf + pos + len >= bufend) { if (pos + len >= bufSize) {
ptrdiff_t newListSize = (bufend - buf) + LIST_SIZE_INCREASE; size_t newListSize = bufSize + LIST_SIZE_INCREASE;
buf = (char*)UTIL_realloc(buf, newListSize); buf = (char*)UTIL_realloc(buf, newListSize);
bufend = buf + newListSize; bufSize = newListSize;
if (!buf) return NULL; if (!buf) return NULL;
} }
if (buf + pos + len < bufend) { if (pos + len < bufSize) {
strncpy(buf + pos, inputNames[i], bufend - (buf + pos)); strncpy(buf + pos, inputNames[i], bufSize - pos);
pos += len + 1; pos += len + 1;
nbFiles++; nbFiles++;
} }
} else { } else {
char* bufend = buf + bufSize;
nbFiles += UTIL_prepareFileList(inputNames[i], &buf, &pos, &bufend); nbFiles += UTIL_prepareFileList(inputNames[i], &buf, &pos, &bufend);
if (buf == NULL) return NULL; if (buf == NULL) return NULL;
assert(bufend > buf);
bufSize = bufend - buf;
} } } }
if (nbFiles == 0) { free(buf); return NULL; } if (nbFiles == 0) { free(buf); return NULL; }
@ -562,7 +569,7 @@ UTIL_STATIC const char** UTIL_createFileList(const char **inputNames, unsigned i
pos += strlen(fileTable[i]) + 1; pos += strlen(fileTable[i]) + 1;
} }
if (buf + pos > bufend) { free(buf); free((void*)fileTable); return NULL; } if (pos > bufSize) { free(buf); free((void*)fileTable); return NULL; } /* can this happen ? */
*allocatedBuffer = buf; *allocatedBuffer = buf;
*allocatedNamesNb = nbFiles; *allocatedNamesNb = nbFiles;
@ -571,7 +578,8 @@ UTIL_STATIC const char** UTIL_createFileList(const char **inputNames, unsigned i
} }
UTIL_STATIC void UTIL_freeFileList(const char** filenameTable, char* allocatedBuffer) UTIL_STATIC void
UTIL_freeFileList(const char** filenameTable, char* allocatedBuffer)
{ {
if (allocatedBuffer) free(allocatedBuffer); if (allocatedBuffer) free(allocatedBuffer);
if (filenameTable) free((void*)filenameTable); if (filenameTable) free((void*)filenameTable);

View File

@ -344,8 +344,9 @@ int basicTests(U32 seed, double compressibility)
ip += iSize; ip += iSize;
} }
{ U64 const crcDest = XXH64(decodedBuffer, COMPRESSIBLE_NOISE_LENGTH, 1); { U64 const crcDest = XXH64(decodedBuffer, COMPRESSIBLE_NOISE_LENGTH, 1);
if (crcDest != crcOrig) goto _output_error; } if (crcDest != crcOrig) goto _output_error;
DISPLAYLEVEL(3, "Regenerated %u/%u bytes \n", (unsigned)(op-ostart), COMPRESSIBLE_NOISE_LENGTH); }
DISPLAYLEVEL(3, "Regenerated %u/%u bytes \n", (unsigned)(op-ostart), (unsigned)COMPRESSIBLE_NOISE_LENGTH);
} }
} }
@ -840,8 +841,8 @@ int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressi
size_t const iSize = MIN(sampleMax, (size_t)(iend-ip)); size_t const iSize = MIN(sampleMax, (size_t)(iend-ip));
size_t const oSize = LZ4F_compressBound(iSize, prefsPtr); size_t const oSize = LZ4F_compressBound(iSize, prefsPtr);
cOptions.stableSrc = ((FUZ_rand(&randState) & 3) == 1); cOptions.stableSrc = ((FUZ_rand(&randState) & 3) == 1);
DISPLAYLEVEL(6, "Sending %zi bytes to compress (stableSrc:%u) \n", DISPLAYLEVEL(6, "Sending %u bytes to compress (stableSrc:%u) \n",
iSize, cOptions.stableSrc); (unsigned)iSize, cOptions.stableSrc);
result = LZ4F_compressUpdate(cCtx, op, oSize, ip, iSize, &cOptions); result = LZ4F_compressUpdate(cCtx, op, oSize, ip, iSize, &cOptions);
CHECK(LZ4F_isError(result), "Compression failed (error %i : %s)", (int)result, LZ4F_getErrorName(result)); CHECK(LZ4F_isError(result), "Compression failed (error %i : %s)", (int)result, LZ4F_getErrorName(result));

View File

@ -367,7 +367,6 @@ int fullSpeedBench(const char** fileNamesTable, int nbFiles)
size_t readSize; size_t readSize;
int compressedBuffSize; int compressedBuffSize;
U32 crcOriginal; U32 crcOriginal;
size_t errorCode;
/* Check file existence */ /* Check file existence */
if (inFile==NULL) { DISPLAY( "Pb opening %s\n", inFileName); return 11; } if (inFile==NULL) { DISPLAY( "Pb opening %s\n", inFileName); return 11; }
@ -561,7 +560,7 @@ int fullSpeedBench(const char** fileNamesTable, int nbFiles)
case 8: decompressionFunction = local_LZ4_decompress_safe_forceExtDict; dName = "LZ4_decompress_safe_forceExtDict"; break; case 8: decompressionFunction = local_LZ4_decompress_safe_forceExtDict; dName = "LZ4_decompress_safe_forceExtDict"; break;
#endif #endif
case 9: decompressionFunction = local_LZ4F_decompress; dName = "LZ4F_decompress"; case 9: decompressionFunction = local_LZ4F_decompress; dName = "LZ4F_decompress";
errorCode = LZ4F_compressFrame(compressed_buff, compressedBuffSize, orig_buff, benchedSize, NULL); { size_t const errorCode = LZ4F_compressFrame(compressed_buff, compressedBuffSize, orig_buff, benchedSize, NULL);
if (LZ4F_isError(errorCode)) { if (LZ4F_isError(errorCode)) {
DISPLAY("Error while preparing compressed frame\n"); DISPLAY("Error while preparing compressed frame\n");
free(orig_buff); free(orig_buff);
@ -573,6 +572,7 @@ int fullSpeedBench(const char** fileNamesTable, int nbFiles)
chunkP[0].compressedSize = (int)errorCode; chunkP[0].compressedSize = (int)errorCode;
nbChunks = 1; nbChunks = 1;
break; break;
}
default : default :
continue; /* skip if unknown ID */ continue; /* skip if unknown ID */
} }