From 76c2fdc7b76e1ab502dd058af9e58a22eeb382ad Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Mon, 24 Mar 2025 21:20:21 -0700 Subject: [PATCH] better naming and more narrow scope of local variables --- programs/util.c | 34 +++++++++++++++++----------------- programs/util.h | 4 ++-- programs/zstdcli.c | 2 +- tests/playTests.sh | 1 + 4 files changed, 21 insertions(+), 20 deletions(-) diff --git a/programs/util.c b/programs/util.c index b6431b49f..43f2b9408 100644 --- a/programs/util.c +++ b/programs/util.c @@ -723,34 +723,33 @@ UTIL_createLinePointers(char* buffer, size_t numLines, size_t bufferSize) } FileNamesTable* -UTIL_createFileNamesTable_fromFileName(const char* inputFileName) +UTIL_createFileNamesTable_fromFileList(const char* fileList) { stat_t statbuf; - FILE* inFile = NULL; char* buffer = NULL; - const char** linePointers = NULL; size_t numLines = 0; size_t bufferSize = 0; /* Check if the input is a valid file */ - if (!UTIL_stat(inputFileName, &statbuf)) { + if (!UTIL_stat(fileList, &statbuf)) { return NULL; } /* Check if the input is a supported type */ if (!UTIL_isRegularFileStat(&statbuf) && !UTIL_isFIFOStat(&statbuf) && - !UTIL_isFileDescriptorPipe(inputFileName)) { + !UTIL_isFileDescriptorPipe(fileList)) { return NULL; } /* Open the input file */ - inFile = fopen(inputFileName, "rb"); - if (inFile == NULL) return NULL; + { FILE* const inFile = fopen(fileList, "rb"); + if (inFile == NULL) return NULL; - /* Read the file content */ - buffer = UTIL_readFileContent(inFile, &bufferSize); - fclose(inFile); + /* Read the file content */ + buffer = UTIL_readFileContent(inFile, &bufferSize); + fclose(inFile); + } if (buffer == NULL) return NULL; @@ -762,14 +761,15 @@ UTIL_createFileNamesTable_fromFileName(const char* inputFileName) } /* Create line pointers */ - linePointers = UTIL_createLinePointers(buffer, numLines, bufferSize); - if (linePointers == NULL) { - free(buffer); - return NULL; - } + { const char** linePointers = UTIL_createLinePointers(buffer, numLines, bufferSize); + if (linePointers == NULL) { + free(buffer); + return NULL; + } - /* Create the final table */ - return UTIL_assembleFileNamesTable(linePointers, numLines, buffer); + /* Create the final table */ + return UTIL_assembleFileNamesTable(linePointers, numLines, buffer); + } } diff --git a/programs/util.h b/programs/util.h index 5fd5de40b..427bcf441 100644 --- a/programs/util.h +++ b/programs/util.h @@ -251,13 +251,13 @@ typedef struct size_t tableCapacity; } FileNamesTable; -/*! UTIL_createFileNamesTable_fromFileName() : +/*! UTIL_createFileNamesTable_fromFileList() : * read filenames from @inputFileName, and store them into returned object. * @return : a FileNamesTable*, or NULL in case of error (ex: @inputFileName doesn't exist). * Note: inputFileSize must be less than 50MB */ FileNamesTable* -UTIL_createFileNamesTable_fromFileName(const char* inputFileName); +UTIL_createFileNamesTable_fromFileList(const char* inputFileName); /*! UTIL_assembleFileNamesTable() : * This function takes ownership of its arguments, @filenames and @buf, diff --git a/programs/zstdcli.c b/programs/zstdcli.c index b9f961c16..fa7ea37b3 100644 --- a/programs/zstdcli.c +++ b/programs/zstdcli.c @@ -1379,7 +1379,7 @@ int main(int argCount, const char* argv[]) size_t const nbFileLists = file_of_names->tableSize; size_t flNb; for (flNb=0; flNb < nbFileLists; flNb++) { - FileNamesTable* const fnt = UTIL_createFileNamesTable_fromFileName(file_of_names->fileNames[flNb]); + FileNamesTable* const fnt = UTIL_createFileNamesTable_fromFileList(file_of_names->fileNames[flNb]); if (fnt==NULL) { DISPLAYLEVEL(1, "zstd: error reading %s \n", file_of_names->fileNames[flNb]); CLEAN_RETURN(1); diff --git a/tests/playTests.sh b/tests/playTests.sh index 486b395eb..d3ce39846 100755 --- a/tests/playTests.sh +++ b/tests/playTests.sh @@ -848,6 +848,7 @@ ls tmp* > tmpList zstd -f tmp1 --filelist=tmpList --filelist=tmpList tmp2 tmp3 # can trigger an overflow of internal file list rm -rf tmp* + println "\n===> --[no-]content-size tests" datagen > tmp_contentsize