summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYann Collet <Cyan4973@users.noreply.github.com>2020-02-26 18:06:54 (GMT)
committerGitHub <noreply@github.com>2020-02-26 18:06:54 (GMT)
commit23b6a700b3088b74fc9058daf9f9b31c6f4838d2 (patch)
treed48f5963f936af91925986b8b9d13302fd1223a2
parent9f3ee5522312162acebb6c893dc77b5e0632f2ee (diff)
parent4cc9d863a3ef97b41979dbb119178ebe1df46dd1 (diff)
downloadlz4-23b6a700b3088b74fc9058daf9f9b31c6f4838d2.zip
lz4-23b6a700b3088b74fc9058daf9f9b31c6f4838d2.tar.gz
lz4-23b6a700b3088b74fc9058daf9f9b31c6f4838d2.tar.bz2
Merge pull request #838 from Yanpas/stack_frame_2
fix: allocate LZ4HC_optimal_t opt on heap each time (#837)
-rw-r--r--lib/lz4hc.c22
1 files changed, 19 insertions, 3 deletions
diff --git a/lib/lz4hc.c b/lib/lz4hc.c
index afadb3f..b75514f 100644
--- a/lib/lz4hc.c
+++ b/lib/lz4hc.c
@@ -1287,8 +1287,13 @@ static int LZ4HC_compress_optimal ( LZ4HC_CCtx_internal* ctx,
const dictCtx_directive dict,
const HCfavor_e favorDecSpeed)
{
+ int retval = 0;
#define TRAILING_LITERALS 3
+#ifdef LZ4HC_HEAPMODE
+ LZ4HC_optimal_t* const opt = (LZ4HC_optimal_t*)malloc(sizeof(LZ4HC_optimal_t) * (LZ4_OPT_NUM + TRAILING_LITERALS));
+#else
LZ4HC_optimal_t opt[LZ4_OPT_NUM + TRAILING_LITERALS]; /* ~64 KB, which is a bit large for stack... */
+#endif
const BYTE* ip = (const BYTE*) source;
const BYTE* anchor = ip;
@@ -1300,6 +1305,9 @@ static int LZ4HC_compress_optimal ( LZ4HC_CCtx_internal* ctx,
BYTE* oend = op + dstCapacity;
/* init */
+#ifdef LZ4HC_HEAPMODE
+ if (opt == NULL) goto _return_label;
+#endif
DEBUGLOG(5, "LZ4HC_compress_optimal(dst=%p, dstCapa=%u)", dst, (unsigned)dstCapacity);
*srcSizePtr = 0;
if (limit == fillOutput) oend -= LASTLITERALS; /* Hack for support LZ4 format restriction */
@@ -1505,7 +1513,10 @@ static int LZ4HC_compress_optimal ( LZ4HC_CCtx_internal* ctx,
size_t const totalSize = 1 + litLength + lastRunSize;
if (limit == fillOutput) oend += LASTLITERALS; /* restore correct value */
if (limit && (op + totalSize > oend)) {
- if (limit == limitedOutput) return 0; /* Check output limit */
+ if (limit == limitedOutput) { /* Check output limit */
+ retval = 0;
+ goto _return_label;
+ }
/* adapt lastRunSize to fill 'dst' */
lastRunSize = (size_t)(oend - op) - 1;
litLength = (lastRunSize + 255 - RUN_MASK) / 255;
@@ -1527,12 +1538,17 @@ static int LZ4HC_compress_optimal ( LZ4HC_CCtx_internal* ctx,
/* End */
*srcSizePtr = (int) (((const char*)ip) - source);
- return (int) ((char*)op-dst);
+ retval = (int) ((char*)op-dst);
+ goto _return_label;
_dest_overflow:
if (limit == fillOutput) {
op = opSaved; /* restore correct out pointer */
goto _last_literals;
}
- return 0;
+ _return_label:
+#ifdef LZ4HC_HEAPMODE
+ free(opt);
+#endif
+ return retval;
}