From b2215f2a8996e0cfd0d1950968c1ab43733be161 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Tue, 18 Sep 2018 18:08:59 -0700 Subject: tried to clean another bunch of cppcheck warnings so "funny" thing with cppcheck is that no 2 versions give the same list of warnings. On Mac, I'm using v1.81, which had all warnings fixed. On Travis CI, it's v1.61, and it complains about a dozen more/different things. On Linux, it's v1.72, and it finds a completely different list of a half dozen warnings. Some of these seems to be bugs/limitations in cppcheck itself. The TravisCI version v1.61 seems unable to understand %zu correctly, and seems to assume it means %u. --- examples/frameCompress.c | 13 +++++++------ lib/lz4.c | 9 +++++---- programs/lz4io.c | 40 ++++++++++++++++++++++++---------------- tests/frametest.c | 11 ++++++----- 4 files changed, 42 insertions(+), 31 deletions(-) diff --git a/examples/frameCompress.c b/examples/frameCompress.c index 2cc4649..a189329 100644 --- a/examples/frameCompress.c +++ b/examples/frameCompress.c @@ -70,11 +70,12 @@ compress_file_internal(FILE* f_in, FILE* f_out, /* write frame header */ { size_t const headerSize = LZ4F_compressBegin(ctx, outBuff, outCapacity, &kPrefs); 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; } 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); } @@ -89,11 +90,11 @@ compress_file_internal(FILE* f_in, FILE* f_out, inBuff, readSize, NULL); if (LZ4F_isError(compressedSize)) { - printf("Compression failed: error %zu\n", compressedSize); + printf("Compression failed: error %u \n", (unsigned)compressedSize); return result; } - printf("Writing %zu bytes\n", compressedSize); + printf("Writing %u bytes\n", (unsigned)compressedSize); safe_fwrite(outBuff, 1, compressedSize, f_out); count_out += compressedSize; } @@ -103,11 +104,11 @@ compress_file_internal(FILE* f_in, FILE* f_out, outBuff, outCapacity, NULL); 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; } - printf("Writing %zu bytes\n", compressedSize); + printf("Writing %u bytes \n", (unsigned)compressedSize); safe_fwrite(outBuff, 1, compressedSize, f_out); count_out += compressedSize; } diff --git a/lib/lz4.c b/lib/lz4.c index df7a2c7..3c7467a 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -522,13 +522,14 @@ static U32 LZ4_hash4(U32 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; - if (LZ4_isLittleEndian()) + if (LZ4_isLittleEndian()) { + const U64 prime5bytes = 889523592379ULL; return (U32)(((sequence << 24) * prime5bytes) >> (64 - hashLog)); - else + } else { + const U64 prime8bytes = 11400714785074694791ULL; return (U32)(((sequence >> 24) * prime8bytes) >> (64 - hashLog)); + } } LZ4_FORCE_INLINE U32 LZ4_hashPosition(const void* const p, tableType_t const tableType) diff --git a/programs/lz4io.c b/programs/lz4io.c index 28d6537..cdc4c27 100644 --- a/programs/lz4io.c +++ b/programs/lz4io.c @@ -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 */ 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 compressedfilesize = MAGICNUMBER_SIZE; char* in_buff; char* out_buff; const int outBuffSize = LZ4_compressBound(LEGACY_BLOCKSIZE); - FILE* finput; + FILE* const finput = LZ4IO_openSrcFile(input_filename); FILE* foutput; clock_t clockEnd; /* Init */ 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); - 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 */ in_buff = (char*)malloc(LEGACY_BLOCKSIZE); - out_buff = (char*)malloc(outBuffSize); - if (!in_buff || !out_buff) EXM_THROW(21, "Allocation error : not enough memory"); + out_buff = (char*)malloc(outBuffSize + 4); + if (!in_buff || !out_buff) + EXM_THROW(21, "Allocation error : not enough memory"); /* Write Archive Header */ LZ4IO_writeLE32(out_buff, LEGACY_MAGICNUMBER); - { size_t const sizeCheck = fwrite(out_buff, 1, MAGICNUMBER_SIZE, foutput); - if (sizeCheck != MAGICNUMBER_SIZE) EXM_THROW(22, "Write error : cannot write header"); } + { size_t const writeSize = fwrite(out_buff, 1, MAGICNUMBER_SIZE, foutput); + if (writeSize != MAGICNUMBER_SIZE) + EXM_THROW(22, "Write error : cannot write header"); + } /* Main Loop */ while (1) { - unsigned int outSize; + int outSize; /* 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 > LEGACY_BLOCKSIZE) EXM_THROW(23, "Read error : wrong fread() size report "); /* should be impossible */ filesize += inSize; /* Compress Block */ @@ -376,9 +382,11 @@ int LZ4IO_compressFilename_Legacy(const char* input_filename, const char* output (int)(filesize>>20), (double)compressedfilesize/filesize*100); /* Write Block */ - LZ4IO_writeLE32(out_buff, outSize); - { size_t const sizeCheck = fwrite(out_buff, 1, outSize+4, foutput); - if (sizeCheck!=(size_t)(outSize+4)) + assert(outSize > 0); + assert(outSize < outBuffSize); + 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"); } } if (ferror(finput)) EXM_THROW(25, "Error while reading %s ", input_filename); diff --git a/tests/frametest.c b/tests/frametest.c index 5714732..f8498b7 100644 --- a/tests/frametest.c +++ b/tests/frametest.c @@ -343,9 +343,10 @@ int basicTests(U32 seed, double compressibility) op += oSize; ip += iSize; } - { U64 const crcDest = XXH64(decodedBuffer, COMPRESSIBLE_NOISE_LENGTH, 1); - if (crcDest != crcOrig) goto _output_error; } - DISPLAYLEVEL(3, "Regenerated %u/%u bytes \n", (unsigned)(op-ostart), COMPRESSIBLE_NOISE_LENGTH); + { U64 const crcDest = XXH64(decodedBuffer, COMPRESSIBLE_NOISE_LENGTH, 1); + if (crcDest != crcOrig) goto _output_error; + } + 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 oSize = LZ4F_compressBound(iSize, prefsPtr); cOptions.stableSrc = ((FUZ_rand(&randState) & 3) == 1); - DISPLAYLEVEL(6, "Sending %zu bytes to compress (stableSrc:%u) \n", - iSize, cOptions.stableSrc); + DISPLAYLEVEL(6, "Sending %u bytes to compress (stableSrc:%u) \n", + (unsigned)iSize, cOptions.stableSrc); result = LZ4F_compressUpdate(cCtx, op, oSize, ip, iSize, &cOptions); CHECK(LZ4F_isError(result), "Compression failed (error %i : %s)", (int)result, LZ4F_getErrorName(result)); -- cgit v0.12