From 62ed15319570d80690915f0d0dba0dc2e9478631 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Sun, 12 Apr 2015 08:21:35 +0100 Subject: Fixed : a few minor coverity warnings --- lib/lz4frame.c | 18 ++++++++++++------ programs/bench.c | 12 ++++-------- programs/frametest.c | 18 +++++++++--------- programs/fullbench.c | 18 +++++++++++------- programs/fuzzer.c | 9 +++++---- programs/lz4cli.c | 2 +- programs/lz4io.c | 22 ++++++++++++++++------ 7 files changed, 58 insertions(+), 41 deletions(-) diff --git a/lib/lz4frame.c b/lib/lz4frame.c index b6dbd20..7094364 100644 --- a/lib/lz4frame.c +++ b/lib/lz4frame.c @@ -372,9 +372,12 @@ LZ4F_errorCode_t LZ4F_freeCompressionContext(LZ4F_compressionContext_t LZ4F_comp { LZ4F_cctx_internal_t* cctxPtr = (LZ4F_cctx_internal_t*)LZ4F_compressionContext; - FREEMEM(cctxPtr->lz4CtxPtr); - FREEMEM(cctxPtr->tmpBuff); - FREEMEM(LZ4F_compressionContext); + if (cctxPtr != NULL) /* null pointers can be safely provided to this function, like free() */ + { + FREEMEM(cctxPtr->lz4CtxPtr); + FREEMEM(cctxPtr->tmpBuff); + FREEMEM(LZ4F_compressionContext); + } return OK_NoError; } @@ -768,9 +771,12 @@ LZ4F_errorCode_t LZ4F_createDecompressionContext(LZ4F_decompressionContext_t* LZ LZ4F_errorCode_t LZ4F_freeDecompressionContext(LZ4F_decompressionContext_t LZ4F_decompressionContext) { LZ4F_dctx_internal_t* dctxPtr = (LZ4F_dctx_internal_t*)LZ4F_decompressionContext; - FREEMEM(dctxPtr->tmpIn); - FREEMEM(dctxPtr->tmpOutBuffer); - FREEMEM(dctxPtr); + if (dctxPtr != NULL) /* can accept NULL input, like free() */ + { + FREEMEM(dctxPtr->tmpIn); + FREEMEM(dctxPtr->tmpOutBuffer); + FREEMEM(dctxPtr); + } return OK_NoError; } diff --git a/programs/bench.c b/programs/bench.c index a5c72d6..8921f09 100644 --- a/programs/bench.c +++ b/programs/bench.c @@ -281,15 +281,11 @@ int BMK_benchFiles(const char** fileNamesTable, int nbFiles, int cLevel) /* Check file existence */ inFileName = fileNamesTable[fileIdx++]; inFile = fopen( inFileName, "rb" ); - if (inFile==NULL) - { - DISPLAY( "Pb opening %s\n", inFileName); - return 11; - } + if (inFile==NULL) { DISPLAY( "Pb opening %s\n", inFileName); return 11; } /* Memory allocation & restrictions */ inFileSize = BMK_GetFileSize(inFileName); - if (inFileSize==0) { DISPLAY( "file is empty\n"); return 11; } + if (inFileSize==0) { DISPLAY( "file is empty\n"); fclose(inFile); return 11; } benchedSize = (size_t) BMK_findMaxMem(inFileSize * 2) / 2; if (benchedSize==0) { DISPLAY( "not enough memory\n"); return 11; } if ((U64)benchedSize > inFileSize) benchedSize = (size_t)inFileSize; @@ -306,7 +302,6 @@ int BMK_benchFiles(const char** fileNamesTable, int nbFiles, int cLevel) compressedBuffSize = nbChunks * maxCompressedChunkSize; compressedBuffer = (char*)malloc((size_t)compressedBuffSize); - if (!orig_buff || !compressedBuffer) { DISPLAY("\nError: not enough memory!\n"); @@ -402,6 +397,7 @@ int BMK_benchFiles(const char** fileNamesTable, int nbFiles, int cLevel) } milliTime = BMK_GetMilliSpan(milliTime); + nbLoops += !nbLoops; /* avoid division by zero */ if ((double)milliTime < fastestD*nbLoops) fastestD = (double)milliTime/nbLoops; DISPLAY("%1i-%-14.14s : %9i -> %9i (%5.2f%%),%7.1f MB/s ,%7.1f MB/s \r", loopNb, inFileName, (int)benchedSize, (int)cSize, ratio, (double)benchedSize / fastestC / 1000., (double)benchedSize / fastestD / 1000.); @@ -431,7 +427,7 @@ int BMK_benchFiles(const char** fileNamesTable, int nbFiles, int cLevel) if (nbFiles > 1) DISPLAY("%-16.16s :%10llu ->%10llu (%5.2f%%), %6.1f MB/s , %6.1f MB/s\n", " TOTAL", (long long unsigned int)totals, (long long unsigned int)totalz, (double)totalz/(double)totals*100., (double)totals/totalc/1000., (double)totals/totald/1000.); - if (BMK_pause) { DISPLAY("\npress enter...\n"); getchar(); } + if (BMK_pause) { DISPLAY("\npress enter...\n"); (void)getchar(); } return 0; } diff --git a/programs/frametest.c b/programs/frametest.c index 5fccd08..6204d7a 100644 --- a/programs/frametest.c +++ b/programs/frametest.c @@ -238,10 +238,11 @@ int basicTests(U32 seed, double compressibility) U32 randState = seed; size_t cSize, testSize; LZ4F_preferences_t prefs; - LZ4F_decompressionContext_t dCtx; + LZ4F_decompressionContext_t dCtx = NULL; + LZ4F_compressionContext_t cctx = NULL; U64 crcOrig; - // Create compressible test buffer + /* Create compressible test buffer */ memset(&prefs, 0, sizeof(prefs)); CNBuffer = malloc(COMPRESSIBLE_NOISE_LENGTH); compressedBuffer = malloc(LZ4F_compressFrameBound(COMPRESSIBLE_NOISE_LENGTH, NULL)); @@ -249,7 +250,7 @@ int basicTests(U32 seed, double compressibility) FUZ_fillCompressibleNoiseBuffer(CNBuffer, COMPRESSIBLE_NOISE_LENGTH, compressibility, &randState); crcOrig = XXH64(CNBuffer, COMPRESSIBLE_NOISE_LENGTH, 1); - // Trivial tests : one-step frame + /* Trivial tests : one-step frame */ testSize = COMPRESSIBLE_NOISE_LENGTH; DISPLAYLEVEL(3, "Using NULL preferences : \n"); cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, NULL), CNBuffer, testSize, NULL); @@ -385,7 +386,6 @@ int basicTests(U32 seed, double compressibility) size_t errorCode; BYTE* const ostart = (BYTE*)compressedBuffer; BYTE* op = ostart; - LZ4F_compressionContext_t cctx; errorCode = LZ4F_createCompressionContext(&cctx, LZ4F_VERSION); if (LZ4F_isError(errorCode)) goto _output_error; @@ -430,6 +430,7 @@ int basicTests(U32 seed, double compressibility) errorCode = LZ4F_freeCompressionContext(cctx); if (LZ4F_isError(errorCode)) goto _output_error; + cctx = NULL; } DISPLAYLEVEL(3, "Skippable frame test : \n"); @@ -500,10 +501,6 @@ int basicTests(U32 seed, double compressibility) ip += iSize; } DISPLAYLEVEL(3, "Skipped %i bytes \n", (int)(ip - (BYTE*)compressedBuffer - 8)); - - /* release memory */ - errorCode = LZ4F_freeDecompressionContext(dCtx); - if (LZ4F_isError(errorCode)) goto _output_error; } DISPLAY("Basic tests completed \n"); @@ -511,6 +508,8 @@ _end: free(CNBuffer); free(compressedBuffer); free(decodedBuffer); + LZ4F_freeDecompressionContext(dCtx); dCtx = NULL; + LZ4F_freeCompressionContext(cctx); cctx = NULL; return testResult; _output_error: @@ -667,6 +666,7 @@ int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressi unsigned nonContiguousDst = (FUZ_rand(&randState) & 3) == 1; nonContiguousDst += FUZ_rand(&randState) & nonContiguousDst; /* 0=>0; 1=>1,2 */ XXH64_reset(&xxh64, 1); + if (maxBits < 3) maxBits = 3; while (ip < iend) { unsigned nbBitsI = (FUZ_rand(&randState) % (maxBits-1)) + 1; @@ -709,7 +709,7 @@ _end: if (pause) { DISPLAY("press enter to finish \n"); - getchar(); + (void)getchar(); } return testResult; diff --git a/programs/fullbench.c b/programs/fullbench.c index 700655b..8c95d29 100644 --- a/programs/fullbench.c +++ b/programs/fullbench.c @@ -533,14 +533,15 @@ static int local_LZ4F_decompress(const char* in, char* out, int inSize, int outS } +#define NB_COMPRESSION_ALGORITHMS 100 +#define NB_DECOMPRESSION_ALGORITHMS 100 +#define CLEANEXIT(c) { benchStatus = c; goto _clean_up; } int fullSpeedBench(char** fileNamesTable, int nbFiles) { int fileIdx=0; char* orig_buff; -# define NB_COMPRESSION_ALGORITHMS 100 -# define NB_DECOMPRESSION_ALGORITHMS 100 size_t errorCode; - + int benchStatus = 0; /* Loop for each fileName */ while (fileIdx %.2f%%\n", (unsigned long long) filesize, (unsigned long long) compressedfilesize, (double)compressedfilesize/filesize*100); { @@ -538,6 +539,7 @@ static unsigned LZ4IO_readLE32 (const void* s) return value32; } +static unsigned g_magicRead = 0; static unsigned long long decodeLegacyStream(FILE* finput, FILE* foutput) { unsigned long long filesize = 0; @@ -562,7 +564,7 @@ static unsigned long long decodeLegacyStream(FILE* finput, FILE* foutput) blockSize = LZ4IO_readLE32(in_buff); /* Convert to Little Endian */ if (blockSize > LZ4_COMPRESSBOUND(LEGACY_BLOCKSIZE)) { /* Cannot read next block : maybe new stream ? */ - fseek(finput, -4, SEEK_CUR); + g_magicRead = blockSize; break; } @@ -761,10 +763,18 @@ static unsigned long long selectDecoder( FILE* finput, FILE* foutput) nbCalls++; /* Check Archive Header */ - nbReadBytes = fread(U32store, 1, MAGICNUMBER_SIZE, finput); - if (nbReadBytes==0) return ENDOFSTREAM; /* EOF */ - if (nbReadBytes != MAGICNUMBER_SIZE) EXM_THROW(40, "Unrecognized header : Magic Number unreadable"); - magicNumber = LZ4IO_readLE32(U32store); /* Little Endian format */ + if (g_magicRead) + { + magicNumber = g_magicRead; + g_magicRead = 0; + } + else + { + nbReadBytes = fread(U32store, 1, MAGICNUMBER_SIZE, finput); + if (nbReadBytes==0) return ENDOFSTREAM; /* EOF */ + if (nbReadBytes != MAGICNUMBER_SIZE) EXM_THROW(40, "Unrecognized header : Magic Number unreadable"); + magicNumber = LZ4IO_readLE32(U32store); /* Little Endian format */ + } if (LZ4IO_isSkippableMagicNumber(magicNumber)) magicNumber = LZ4IO_SKIPPABLE0; /* fold skippable magic numbers */ switch(magicNumber) @@ -809,7 +819,7 @@ int LZ4IO_decompressFilename(const char* input_filename, const char* output_file get_fileHandle(input_filename, output_filename, &finput, &foutput); /* sparse file */ - if (g_sparseFileSupport && foutput) { SET_SPARSE_FILE_MODE(foutput); } + if (g_sparseFileSupport) { SET_SPARSE_FILE_MODE(foutput); } /* Loop over multiple streams */ do -- cgit v0.12