Skip to content

Commit ad1cf51

Browse files
committed
Merge branch 'PHP-8.5'
* PHP-8.5: zlib: Fix memory leak in inflate_add() zlib: no need to free dict on inflate init error
2 parents 42cc4ad + 0814870 commit ad1cf51

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--TEST--
2+
inflate_add(): Z_NEED_DICT returned when no dictionary provided to inflate_init()
3+
--EXTENSIONS--
4+
zlib
5+
--FILE--
6+
<?php
7+
8+
$dict = "the quick brown fox jumps over the lazy dog";
9+
$data = "the quick brown fox";
10+
11+
$dc = deflate_init(ZLIB_ENCODING_DEFLATE, ['dictionary' => $dict]);
12+
$compressed = deflate_add($dc, $data, ZLIB_FINISH);
13+
14+
// Inflate without supplying the dictionary
15+
$ic = inflate_init(ZLIB_ENCODING_DEFLATE);
16+
$result = inflate_add($ic, $compressed, ZLIB_SYNC_FLUSH);
17+
var_dump($result);
18+
19+
?>
20+
--EXPECTF--
21+
Warning: inflate_add(): Inflating this data requires a preset dictionary, please specify it in the options array of inflate_init() in %s on line %d
22+
bool(false)

ext/zlib/zlib.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -925,7 +925,7 @@ PHP_FUNCTION(inflate_init)
925925
}
926926

927927
if (inflateInit2(&ctx->Z, encoding) != Z_OK) {
928-
efree(dict);
928+
/* dict is stored in the ctx in the object and will thus be freed by zval_ptr_dtor(). */
929929
zval_ptr_dtor(return_value);
930930
php_error_docref(NULL, E_WARNING, "Failed allocating zlib.inflate context");
931931
RETURN_FALSE;
@@ -1043,6 +1043,7 @@ PHP_FUNCTION(inflate_add)
10431043
}
10441044
break;
10451045
} else {
1046+
zend_string_release_ex(out, false);
10461047
php_error_docref(NULL, E_WARNING, "Inflating this data requires a preset dictionary, please specify it in the options array of inflate_init()");
10471048
RETURN_FALSE;
10481049
}

0 commit comments

Comments
 (0)