summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/lz4frame.c18
-rw-r--r--programs/bench.c12
-rw-r--r--programs/frametest.c18
-rw-r--r--programs/fullbench.c18
-rw-r--r--programs/fuzzer.c9
-rw-r--r--programs/lz4cli.c2
-rw-r--r--programs/lz4io.c22
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<nbFiles)
@@ -580,6 +581,7 @@ int fullSpeedBench(char** fileNamesTable, int nbFiles)
if (LZ4F_isError(errorCode))
{
DISPLAY("dctx allocation issue \n");
+ fclose(inFile);
return 10;
}
chunkP = (struct chunkParameters*) malloc(((benchedSize / (size_t)chunkSize)+1) * sizeof(struct chunkParameters));
@@ -765,14 +767,14 @@ int fullSpeedBench(char** fileNamesTable, int nbFiles)
case 8: decompressionFunction = local_LZ4_decompress_safe_forceExtDict; dName = "LZ4_decompress_safe_forceExtDict"; break;
case 9: decompressionFunction = local_LZ4F_decompress; dName = "LZ4F_decompress";
errorCode = LZ4F_compressFrame(compressed_buff, compressedBuffSize, orig_buff, benchedSize, NULL);
- if (LZ4F_isError(errorCode)) { DISPLAY("Preparation error compressing frame\n"); return 1; }
+ if (LZ4F_isError(errorCode)) { DISPLAY("Preparation error compressing frame\n"); CLEANEXIT(1); }
chunkP[0].origSize = (int)benchedSize;
chunkP[0].compressedSize = (int)errorCode;
nbChunks = 1;
break;
default :
continue; /* skip if unknown ID */
- DISPLAY("ERROR ! Bad decompression algorithm Id !! \n"); free(chunkP); return 1;
+ DISPLAY("ERROR ! Bad decompression algorithm Id !! \n"); CLEANEXIT(1);
}
{ size_t i; for (i=0; i<benchedSize; i++) orig_buff[i]=0; } /* zeroing source area, for CRC checking */
@@ -800,6 +802,7 @@ int fullSpeedBench(char** fileNamesTable, int nbFiles)
}
milliTime = BMK_GetMilliSpan(milliTime);
+ nb_loops += !nb_loops; /* Avoid division by zero */
averageTime = (double)milliTime / nb_loops;
if (averageTime < bestTime) bestTime = averageTime;
@@ -815,15 +818,16 @@ int fullSpeedBench(char** fileNamesTable, int nbFiles)
}
+_clean_up:
free(orig_buff);
free(compressed_buff);
free(chunkP);
LZ4F_freeDecompressionContext(g_dCtx);
}
- if (BMK_pause) { printf("press enter...\n"); getchar(); }
+ if (BMK_pause) { printf("press enter...\n"); (void)getchar(); }
- return 0;
+ return benchStatus;
}
diff --git a/programs/fuzzer.c b/programs/fuzzer.c
index d811eb2..aa91ee8 100644
--- a/programs/fuzzer.c
+++ b/programs/fuzzer.c
@@ -574,9 +574,9 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c
crcCheck = XXH32(decodedBuffer+dictSize, blockSize, 0);
FUZ_CHECKTEST(crcCheck!=crcOrig, "LZ4_decompress_safe_withPrefix64k corrupted decoded data");
- // Compress using External dictionary
+ /* Compress using External dictionary */
FUZ_DISPLAYTEST;
- dict -= (FUZ_rand(&randState) & 0xF) + 1; // Separation, so it is an ExtDict
+ dict -= (FUZ_rand(&randState) & 0xF) + 1; /* Separation, so it is an ExtDict */
if (dict < (char*)CNBuffer) dict = (char*)CNBuffer;
LZ4_loadDict(&LZ4dict, dict, dictSize);
blockContinueCompressedSize = LZ4_compress_continue(&LZ4dict, block, compressedBuffer, blockSize);
@@ -683,7 +683,8 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c
ccbytes += blockContinueCompressedSize;
}
- if (nbCycles<=1) nbCycles = cycleNb;
+ if (nbCycles<=1) nbCycles = cycleNb; /* end by time */
+ bytes += !bytes; /* avoid division by 0 */
printf("\r%7u /%7u - ", cycleNb, nbCycles);
printf("all tests completed successfully \n");
printf("compression ratio: %0.3f%%\n", (double)cbytes/bytes*100);
@@ -1163,7 +1164,7 @@ int main(int argc, char** argv)
if (pause)
{
DISPLAY("press enter ... \n");
- getchar();
+ (void)getchar();
}
return result;
}
diff --git a/programs/lz4cli.c b/programs/lz4cli.c
index f33ea82..7ed0f30 100644
--- a/programs/lz4cli.c
+++ b/programs/lz4cli.c
@@ -251,7 +251,7 @@ static int badusage(void)
static void waitEnter(void)
{
DISPLAY("Press enter to continue...\n");
- getchar();
+ (void)getchar();
}
diff --git a/programs/lz4io.c b/programs/lz4io.c
index b6e678e..1595a0d 100644
--- a/programs/lz4io.c
+++ b/programs/lz4io.c
@@ -372,6 +372,7 @@ int LZ4IO_compressFilename_Legacy(const char* input_filename, const char* output
/* Status */
end = clock();
DISPLAYLEVEL(2, "\r%79s\r", "");
+ filesize += !filesize; /* avoid divide by zero */
DISPLAYLEVEL(2,"Compressed %llu bytes into %llu bytes ==> %.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