From d2be69b144d6c5fd9a3dcbc4133e93e710cda998 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Tue, 15 Nov 2016 14:01:37 -0800 Subject: fixed minor coverity warnings --- lib/lz4frame.c | 37 ++++++++++++++++++------------------- programs/bench.c | 1 + programs/datagen.c | 29 ++++++++++------------------- programs/lz4.1 | 3 ++- programs/lz4cli.c | 22 ++++++++-------------- programs/lz4io.c | 7 ++++--- programs/lz4io.h | 16 ++++++++++------ 7 files changed, 53 insertions(+), 62 deletions(-) diff --git a/lib/lz4frame.c b/lib/lz4frame.c index c31f82d..c8e5bde 100644 --- a/lib/lz4frame.c +++ b/lib/lz4frame.c @@ -817,9 +817,8 @@ static size_t LZ4F_headerSize(const void* src, size_t srcSize) */ static size_t LZ4F_decodeHeader(LZ4F_dctx* dctxPtr, const void* src, size_t srcSize) { - BYTE FLG, BD, HC; + BYTE FLG, BD; unsigned version, blockMode, blockChecksumFlag, contentSizeFlag, contentChecksumFlag, blockSizeID; - size_t bufferNeeded; size_t frameHeaderSize; const BYTE* srcPtr = (const BYTE*)src; @@ -877,9 +876,9 @@ static size_t LZ4F_decodeHeader(LZ4F_dctx* dctxPtr, const void* src, size_t srcS if (blockSizeID < 4) return err0r(LZ4F_ERROR_maxBlockSize_invalid); /* 4-7 only supported values for the time being */ if (((BD>>0)&_4BITS) != 0) return err0r(LZ4F_ERROR_reservedFlag_set); /* Reserved bits */ - /* check */ - HC = LZ4F_headerChecksum(srcPtr+4, frameHeaderSize-5); - if (HC != srcPtr[frameHeaderSize-1]) return err0r(LZ4F_ERROR_headerChecksum_invalid); /* Bad header checksum error */ + /* check header */ + { BYTE const HC = LZ4F_headerChecksum(srcPtr+4, frameHeaderSize-5); + if (HC != srcPtr[frameHeaderSize-1]) return err0r(LZ4F_ERROR_headerChecksum_invalid); } /* save */ dctxPtr->frameInfo.blockMode = (LZ4F_blockMode_t)blockMode; @@ -892,17 +891,17 @@ static size_t LZ4F_decodeHeader(LZ4F_dctx* dctxPtr, const void* src, size_t srcS /* init */ if (contentChecksumFlag) XXH32_reset(&(dctxPtr->xxh), 0); - /* alloc */ - bufferNeeded = dctxPtr->maxBlockSize + ((dctxPtr->frameInfo.blockMode==LZ4F_blockLinked) * 128 KB); - if (bufferNeeded > dctxPtr->maxBufferSize) { /* tmp buffers too small */ - FREEMEM(dctxPtr->tmpIn); - FREEMEM(dctxPtr->tmpOutBuffer); - dctxPtr->maxBufferSize = bufferNeeded; - dctxPtr->tmpIn = (BYTE*)ALLOCATOR(dctxPtr->maxBlockSize); - if (dctxPtr->tmpIn == NULL) return err0r(LZ4F_ERROR_GENERIC); - dctxPtr->tmpOutBuffer= (BYTE*)ALLOCATOR(dctxPtr->maxBufferSize); - if (dctxPtr->tmpOutBuffer== NULL) return err0r(LZ4F_ERROR_GENERIC); - } + /* internal buffers allocation */ + { size_t const bufferNeeded = dctxPtr->maxBlockSize + ((dctxPtr->frameInfo.blockMode==LZ4F_blockLinked) * 128 KB); + if (bufferNeeded > dctxPtr->maxBufferSize) { /* tmp buffers too small */ + FREEMEM(dctxPtr->tmpIn); + dctxPtr->tmpIn = (BYTE*)ALLOCATOR(dctxPtr->maxBlockSize); + if (dctxPtr->tmpIn == NULL) return err0r(LZ4F_ERROR_allocation_failed); + dctxPtr->maxBufferSize = bufferNeeded; + FREEMEM(dctxPtr->tmpOutBuffer); + dctxPtr->tmpOutBuffer= (BYTE*)ALLOCATOR(bufferNeeded); + if (dctxPtr->tmpOutBuffer== NULL) return err0r(LZ4F_ERROR_allocation_failed); + } } dctxPtr->tmpInSize = 0; dctxPtr->tmpInTarget = 0; dctxPtr->dict = dctxPtr->tmpOutBuffer; @@ -981,9 +980,9 @@ static void LZ4F_updateDict(LZ4F_dctx* dctxPtr, const BYTE* dstPtr, size_t dstSi } if (withinTmp) { /* copy relevant dict portion in front of tmpOut within tmpOutBuffer */ - size_t preserveSize = dctxPtr->tmpOut - dctxPtr->tmpOutBuffer; + size_t const preserveSize = dctxPtr->tmpOut - dctxPtr->tmpOutBuffer; size_t copySize = 64 KB - dctxPtr->tmpOutSize; - const BYTE* oldDictEnd = dctxPtr->dict + dctxPtr->dictSize - dctxPtr->tmpOutStart; + const BYTE* const oldDictEnd = dctxPtr->dict + dctxPtr->dictSize - dctxPtr->tmpOutStart; if (dctxPtr->tmpOutSize > 64 KB) copySize = 0; if (copySize > preserveSize) copySize = preserveSize; @@ -996,7 +995,7 @@ static void LZ4F_updateDict(LZ4F_dctx* dctxPtr, const BYTE* dstPtr, size_t dstSi if (dctxPtr->dict == dctxPtr->tmpOutBuffer) { /* copy dst into tmp to complete dict */ if (dctxPtr->dictSize + dstSize > dctxPtr->maxBufferSize) { /* tmp buffer not large enough */ - size_t preserveSize = 64 KB - dstSize; /* note : dstSize < 64 KB */ + size_t const preserveSize = 64 KB - dstSize; /* note : dstSize < 64 KB */ memcpy(dctxPtr->tmpOutBuffer, dctxPtr->dict + dctxPtr->dictSize - preserveSize, preserveSize); dctxPtr->dictSize = preserveSize; } diff --git a/programs/bench.c b/programs/bench.c index cfd60db..434da8b 100644 --- a/programs/bench.c +++ b/programs/bench.c @@ -260,6 +260,7 @@ static int BMK_benchMem(const void* srcBuffer, size_t srcSize, cSize = 0; { U32 blockNb; for (blockNb=0; blockNb%10u (%5.3f),%6.1f MB/s\r", diff --git a/programs/datagen.c b/programs/datagen.c index d851418..ed37c2a 100644 --- a/programs/datagen.c +++ b/programs/datagen.c @@ -81,10 +81,10 @@ typedef BYTE litDistribTable[LTSIZE]; - /********************************************************* * Local Functions *********************************************************/ +#define MIN(a,b) ( (a) < (b) ? (a) :(b) ) #define RDG_rotl32(x,r) ((x << r) | (x >> (32 - r))) static unsigned int RDG_rand(U32* src) { @@ -99,24 +99,15 @@ static unsigned int RDG_rand(U32* src) static void RDG_fillLiteralDistrib(litDistribTable lt, double ld) { - U32 i = 0; - BYTE character = '0'; - BYTE firstChar = '('; - BYTE lastChar = '}'; - - if (ld==0.0) - { - character = 0; - firstChar = 0; - lastChar =255; - } - while (i LTSIZE) weight = LTSIZE-i; - end = i + weight; - while (i < end) lt[i++] = character; + BYTE const firstChar = ld <= 0.0 ? 0 : '('; + BYTE const lastChar = ld <= 0.0 ? 255 : '}'; + BYTE character = ld <= 0.0 ? 0 : '0'; + U32 u = 0; + + while (u lastChar) character = firstChar; } diff --git a/programs/lz4.1 b/programs/lz4.1 index 434b131..2c94033 100644 --- a/programs/lz4.1 +++ b/programs/lz4.1 @@ -207,7 +207,8 @@ hence for a file. It won't work with unknown source size, such as stdin or pipe. sparse mode support (default:enabled on file, disabled on stdout) .TP .B \-l - use Legacy format (useful for Linux Kernel compression) + use Legacy format (typically used for Linux Kernel compression) + note : \fB-l\fR is not compatible with \fB-m\fR (\fB--multiple\fR) . .SS "Other options" .TP diff --git a/programs/lz4cli.c b/programs/lz4cli.c index 88fbb53..e9a0d4b 100644 --- a/programs/lz4cli.c +++ b/programs/lz4cli.c @@ -288,11 +288,11 @@ int main(int argc, const char** argv) const char* input_filename = NULL; const char* output_filename= NULL; char* dynNameSpace = NULL; - const char** inFileNames = NULL; + const char** inFileNames = (const char**) calloc(argc, sizeof(char*));; unsigned ifnIdx=0; const char nullOutput[] = NULL_OUTPUT; const char extension[] = LZ4_EXTENSION; - int blockSize = LZ4IO_setBlockSizeID(LZ4_BLOCKSIZEID_DEFAULT); + size_t blockSize = LZ4IO_setBlockSizeID(LZ4_BLOCKSIZEID_DEFAULT); const char* const exeName = argv[0]; #ifdef UTIL_HAS_CREATEFILELIST const char** extendedFileList = NULL; @@ -301,6 +301,10 @@ int main(int argc, const char** argv) #endif /* Init */ + if (inFileNames==NULL) { + DISPLAY("Allocation error : not enough memory \n"); + return 1; + } LZ4IO_setOverwrite(0); /* lz4cat predefined behavior */ @@ -311,8 +315,6 @@ int main(int argc, const char** argv) output_filename=stdoutmark; displayLevel=1; multiple_inputs=1; - inFileNames = (const char**) calloc(argc, sizeof(char*)); - if (inFileNames==NULL) { perror(exeName); exit(1); } } if (!strcmp(exeName, UNLZ4)) { mode = om_decompress; } @@ -336,7 +338,7 @@ int main(int argc, const char** argv) if (!strcmp(argument, "--compress")) { mode = om_compress; continue; } if ((!strcmp(argument, "--decompress")) || (!strcmp(argument, "--uncompress"))) { mode = om_decompress; continue; } - if (!strcmp(argument, "--multiple")) { multiple_inputs = 1; if (inFileNames==NULL) inFileNames = (const char**)malloc(argc * sizeof(char*)); continue; } + if (!strcmp(argument, "--multiple")) { multiple_inputs = 1; continue; } if (!strcmp(argument, "--test")) { mode = om_test; continue; } if (!strcmp(argument, "--force")) { LZ4IO_setOverwrite(1); continue; } if (!strcmp(argument, "--no-force")) { LZ4IO_setOverwrite(0); continue; } @@ -454,10 +456,6 @@ int main(int argc, const char** argv) /* Benchmark */ case 'b': mode = om_bench; multiple_inputs=1; - if (inFileNames == NULL) { - inFileNames = (const char**) calloc(argc, sizeof(char*)); - if (inFileNames==NULL) { perror(exeName); exit(1); } - } break; #ifdef UTIL_HAS_CREATEFILELIST @@ -466,10 +464,6 @@ int main(int argc, const char** argv) #endif /* Treat non-option args as input files. See https://code.google.com/p/lz4/issues/detail?id=151 */ case 'm': multiple_inputs=1; - if (inFileNames == NULL) { - inFileNames = (const char**) calloc(argc, sizeof(char*)); - if (inFileNames==NULL) { perror(exeName); exit(1); } - } break; /* Modify Nb Seconds (benchmark only) */ @@ -514,7 +508,7 @@ int main(int argc, const char** argv) } DISPLAYLEVEL(3, WELCOME_MESSAGE); - if ((mode == om_compress) || (mode == om_bench)) DISPLAYLEVEL(4, "Blocks size : %i KB\n", blockSize>>10); + if ((mode == om_compress) || (mode == om_bench)) DISPLAYLEVEL(4, "Blocks size : %i KB\n", (U32)(blockSize>>10)); if (multiple_inputs) { input_filename = inFileNames[0]; diff --git a/programs/lz4io.c b/programs/lz4io.c index 5a366a4..f34d715 100644 --- a/programs/lz4io.c +++ b/programs/lz4io.c @@ -183,10 +183,10 @@ int LZ4IO_setTestMode(int yes) } /* blockSizeID : valid values : 4-5-6-7 */ -int LZ4IO_setBlockSizeID(int bsid) +size_t LZ4IO_setBlockSizeID(unsigned bsid) { - static const int blockSizeTable[] = { 64 KB, 256 KB, 1 MB, 4 MB }; - if ((bsid < minBlockSizeID) || (bsid > maxBlockSizeID)) return -1; + static const size_t blockSizeTable[] = { 64 KB, 256 KB, 1 MB, 4 MB }; + if ((bsid < minBlockSizeID) || (bsid > maxBlockSizeID)) return 0; g_blockSizeId = bsid; return blockSizeTable[g_blockSizeId-minBlockSizeID]; } @@ -777,6 +777,7 @@ static dRess_t LZ4IO_createDResources(void) ress.dstBuffer = malloc(ress.dstBufferSize); if (!ress.srcBuffer || !ress.dstBuffer) EXM_THROW(61, "Allocation error : not enough memory"); + ress.dstFile = NULL; return ress; } diff --git a/programs/lz4io.h b/programs/lz4io.h index 315c805..e1ab5f3 100644 --- a/programs/lz4io.h +++ b/programs/lz4io.h @@ -32,16 +32,20 @@ #ifndef LZ4IO_H_237902873 #define LZ4IO_H_237902873 +/*--- Dependency ---*/ +#include /* size_t */ + + /* ************************************************** */ /* Special input/output values */ /* ************************************************** */ #define NULL_OUTPUT "null" -static char const stdinmark[] = "stdin"; -static char const stdoutmark[] = "stdout"; +static const char stdinmark[] = "stdin"; +static const char stdoutmark[] = "stdout"; #ifdef _WIN32 -static char const nulmark[] = "nul"; +static const char nulmark[] = "nul"; #else -static char const nulmark[] = "/dev/null"; +static const char nulmark[] = "/dev/null"; #endif @@ -69,8 +73,8 @@ int LZ4IO_setOverwrite(int yes); int LZ4IO_setTestMode(int yes); /* blockSizeID : valid values : 4-5-6-7 - return : -1 if error, blockSize if OK */ -int LZ4IO_setBlockSizeID(int blockSizeID); + return : 0 if error, blockSize if OK */ +size_t LZ4IO_setBlockSizeID(unsigned blockSizeID); /* Default setting : independent blocks */ typedef enum { LZ4IO_blockLinked=0, LZ4IO_blockIndependent} LZ4IO_blockMode_t; -- cgit v0.12