summaryrefslogtreecommitdiffstats
path: root/lib/lz4hc.c
diff options
context:
space:
mode:
authorYan Pashkovsky <yanp.bugz@gmail.com>2020-01-31 16:44:56 (GMT)
committerYan Pashkovsky <yanp.bugz@gmail.com>2020-01-31 17:10:56 (GMT)
commit2553cd550b53532cc7d19831db520c9812cbb667 (patch)
treeb39425ac2c28d5dab19dc058fdfa22a7e3a515c0 /lib/lz4hc.c
parente55548b0bd553cc25d8c0994db6f5d49663fb3f5 (diff)
downloadlz4-2553cd550b53532cc7d19831db520c9812cbb667.zip
lz4-2553cd550b53532cc7d19831db520c9812cbb667.tar.gz
lz4-2553cd550b53532cc7d19831db520c9812cbb667.tar.bz2
fix: allocate LZ4HC_optimal_t opt on heap each time (#837)
Diffstat (limited to 'lib/lz4hc.c')
-rw-r--r--lib/lz4hc.c19
1 files changed, 16 insertions, 3 deletions
diff --git a/lib/lz4hc.c b/lib/lz4hc.c
index afadb3f..8049840 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* 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;
@@ -1505,7 +1510,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 +1535,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;
}