From: shakeelrao Date: Sun, 24 Mar 2019 04:53:13 +0000 (-0700) Subject: Implement file check X-Git-Tag: v1.4.0^2~22^2~5 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1290933d192c4a42db5ad5b90978f53a8a8a8d26;p=thirdparty%2Fzstd.git Implement file check --- diff --git a/programs/fileio.c b/programs/fileio.c index ffa43ab22..5672de02d 100644 --- a/programs/fileio.c +++ b/programs/fileio.c @@ -515,7 +515,7 @@ static FILE* FIO_openDstFile(FIO_prefs_t* const prefs, const char* srcFileName, return stdout; } - /* check if src file is the same as dst */ + /* ensure dst is not the same as src */ if (srcFileName != NULL && UTIL_isSameFile(srcFileName, dstFileName)) { DISPLAYLEVEL(1, "zstd: Refusing to open an output file which will overwrite the input file \n"); return NULL; @@ -610,6 +610,7 @@ typedef struct { size_t srcBufferSize; void* dstBuffer; size_t dstBufferSize; + const char* dictFileName; ZSTD_CStream* cctx; } cRess_t; @@ -637,6 +638,7 @@ static cRess_t FIO_createCResources(FIO_prefs_t* const prefs, size_t const dictBuffSize = FIO_createDictBuffer(&dictBuffer, dictFileName); /* works with dictFileName==NULL */ if (dictFileName && (dictBuffer==NULL)) EXM_THROW(32, "allocation error : can't create dictBuffer"); + ress.dictFileName = dictFileName; if (prefs->adaptiveMode && !prefs->ldmFlag && !comprParams.windowLog) comprParams.windowLog = ADAPT_WINDOWLOG_DEFAULT; @@ -1290,12 +1292,18 @@ FIO_compressFilename_srcFile(FIO_prefs_t* const prefs, { int result; - /* File check */ + /* ensure src is not a directory */ if (UTIL_isDirectory(srcFileName)) { DISPLAYLEVEL(1, "zstd: %s is a directory -- ignored \n", srcFileName); return 1; } + /* ensure src is not the same as dict */ + if (UTIL_isSameFile(srcFileName, ress.dictFileName)) { + DISPLAYLEVEL(1, "zstd: Refusing to use %s as an input file and dictionary \n", srcFileName); + return 1; + } + ress.srcFile = FIO_openSrcFile(srcFileName); if (ress.srcFile == NULL) return 1; /* srcFile could not be opened */