increase compression buffer up to 104% to avoid failure when output is larger than input

(128 bytes is minimal, but not enough)

don't try to compress very large input (>4GB)
This commit is contained in:
Remi Collet
2018-12-20 13:35:10 +01:00
parent 2520ff86ba
commit d32685b494
4 changed files with 33 additions and 7 deletions

1
.gitignore vendored
View File

@@ -19,6 +19,7 @@ config.status
config.sub
configure
configure.in
configure.ac
install-sh
libtool
ltmain.sh

12
lzf.c
View File

@@ -127,18 +127,24 @@ Return a string compressed with LZF */
PHP_FUNCTION(lzf_compress)
{
char *retval, *arg = NULL;
strsize_t arg_len, result;
strsize_t arg_len, out_len, result;
if (ZEND_NUM_ARGS() != 1 || zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {
WRONG_PARAM_COUNT;
}
retval = emalloc(arg_len + LZF_MARGIN);
if (arg_len > UINT_MAX) { /* LZF library do not support support large strings */
RETURN_FALSE;
}
out_len = arg_len + MIN(UINT_MAX - arg_len, MAX(LZF_MARGIN, arg_len / 25));
retval = emalloc(out_len);
if (!retval) {
RETURN_FALSE;
}
result = lzf_compress(arg, arg_len, retval, arg_len + LZF_MARGIN);
result = lzf_compress(arg, arg_len, retval, out_len);
if (result == 0) {
efree(retval);
RETURN_FALSE;

View File

@@ -22,7 +22,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
</lead>
<date>2017-05-29</date>
<version>
<release>1.6.6</release>
<release>1.6.7dev</release>
<api>1.5.0</api>
</version>
<stability>
@@ -31,7 +31,8 @@ http://pear.php.net/dtd/package-2.0.xsd">
</stability>
<license uri="http://www.php.net/license">PHP License</license>
<notes>
- update bundled liblzf to 3.6
- increase compression buffer up to 104% to avoid failure when output is larger than input
- don't try to compress very large input (>4GB)
</notes>
<contents>
<dir name="/">
@@ -40,6 +41,8 @@ http://pear.php.net/dtd/package-2.0.xsd">
<file name="002.phpt" role="test" />
<file name="003.phpt" role="test" />
<file name="004.phpt" role="test" />
<file name="005.phpt" role="test" />
<file name="006.phpt" role="test" />
<file name="bug70727.phpt" role="test" />
</dir> <!-- //tests -->
<dir name="examples">
@@ -81,7 +84,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
<release>
<date>2017-05-29</date>
<version>
<release>1.6.7dev</release>
<release>1.6.6</release>
<api>1.5.0</api>
</version>
<stability>
@@ -90,7 +93,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
</stability>
<license uri="http://www.php.net/license">PHP License</license>
<notes>
-
- update bundled liblzf to 3.6
</notes>
</release>
<release>

16
tests/006.phpt Normal file
View File

@@ -0,0 +1,16 @@
--TEST--
lzf.compress on binary may result in larger string than input
--SKIPIF--
<?php if (!extension_loaded("lzf")) print "skip"; ?>
<?php if (!function_exists("random_bytes")) print "skip"; ?>
--FILE--
<?php
for ($i = 0 ; $i<32 ; $i++) {
$in = random_bytes(random_int(10000,50000));
$out = lzf_compress($in);
if (!$out) echo "Failure\n";
}
?>
Done
--EXPECT--
Done