Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b27fd43

Browse files
committed
Issue #1477: lzf_compress margin compatibility
1 parent 602740d commit b27fd43

1 file changed

Lines changed: 13 additions & 13 deletions

File tree

library.c

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212

1313
#ifdef HAVE_REDIS_LZF
1414
#include <lzf.h>
15+
16+
#ifndef LZF_MARGIN
17+
#define LZF_MARGIN 128
18+
#endif
1519
#endif
1620

1721
#include <zend_exceptions.h>
@@ -2133,20 +2137,16 @@ redis_pack(RedisSock *redis_sock, zval *z, char **val, strlen_t *val_len TSRMLS_
21332137
switch (redis_sock->compression) {
21342138
case REDIS_COMPRESSION_LZF:
21352139
#ifdef HAVE_REDIS_LZF
2136-
/**
2137-
* output buffer might be considerably more than in_len
2138-
* (but less than 104% of the original size)
2139-
*/
2140-
if ((size = ceil(len * 1.04)) < UINT_MAX) {
2141-
data = emalloc(size);
2142-
if ((res = lzf_compress(buf, len, data, size)) > 0) {
2143-
if (valfree) efree(buf);
2144-
*val = data;
2145-
*val_len = res;
2146-
return 1;
2147-
}
2148-
efree(data);
2140+
/* preserve compatibility with PECL lzf_compress margin (greater of 4% and LZF_MARGIN) */
2141+
size = len + MIN(UINT_MAX - len, MAX(LZF_MARGIN, len / 25));
2142+
data = emalloc(size);
2143+
if ((res = lzf_compress(buf, len, data, size)) > 0) {
2144+
if (valfree) efree(buf);
2145+
*val = data;
2146+
*val_len = res;
2147+
return 1;
21492148
}
2149+
efree(data);
21502150
#endif
21512151
break;
21522152
}

0 commit comments

Comments
 (0)