From: Bimba Shrestha Date: Fri, 13 Dec 2019 23:47:28 +0000 (-0800) Subject: Adding bool to check if enough room left for noCompress superblocks X-Git-Tag: v1.4.5^2~129^2~5 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5225dcfc0f8c03ea718417d58c81cd0c799abf22;p=thirdparty%2Fzstd.git Adding bool to check if enough room left for noCompress superblocks --- diff --git a/lib/compress/zstd_compress.c b/lib/compress/zstd_compress.c index b624e3c18..338f8dd56 100644 --- a/lib/compress/zstd_compress.c +++ b/lib/compress/zstd_compress.c @@ -2562,6 +2562,14 @@ static size_t ZSTD_compress_frameChunk (ZSTD_CCtx* cctx, BYTE* const ostart = (BYTE*)dst; BYTE* op = ostart; U32 const maxDist = (U32)1 << cctx->appliedParams.cParams.windowLog; + + /* This bool is set if there is enough room to output all noCompress superblocks. + * Just checks if the number of compressed blocks we can fit in dstCapacity is + * greater than the optimistic number of blocks we still have remaining. + * This might be UNset when data is uncompressable and we're streaming. */ + + int enoughDstCapacityForNoCompressSuperBlocks = + (dstCapacity / (blockSize + 7 /* header + checksum */)) > (srcSize / blockSize); assert(cctx->appliedParams.cParams.windowLog <= ZSTD_WINDOWLOG_MAX); DEBUGLOG(5, "ZSTD_compress_frameChunk (blockSize=%u)", (unsigned)blockSize); @@ -2586,7 +2594,7 @@ static size_t ZSTD_compress_frameChunk (ZSTD_CCtx* cctx, { size_t cSize; int useTargetCBlockSize = ZSTD_useTargetCBlockSize(&cctx->appliedParams); - if (useTargetCBlockSize) { + if (useTargetCBlockSize && enoughDstCapacityForNoCompressSuperBlocks) { cSize = ZSTD_compressBlock_targetCBlockSize(cctx, op, dstCapacity, ip, blockSize, lastBlock); FORWARD_IF_ERROR(cSize); } else {