Skip to content

Commit c488172

Browse files
committed
Improve ZBLOCK handling with zlib < 1.2.4
The original commit for this issue (62b1293) assumed Z_BLOCK was only defined in < 1.2.4. However, this flush type *is* defined but is only unavailable for use with deflate(). This new commit correctly checks the ZLIB_VERNUM constant to determine if Z_BLOCK flush is available for the current deflate() operation and triggers an appropriate error as needed. New ZLIB_VERSION and ZLIB_VERNUM constants are also exposed in userland to allow testing this behavior in environments running zlib < 1.2.4 (ZLIB_VERNUM check is needed).
1 parent 3cc59b3 commit c488172

File tree

4 files changed

+37
-21
lines changed

4 files changed

+37
-21
lines changed

ext/zlib/tests/deflate_add_basic.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ $flushTypes = [
4343
'ZLIB_NO_FLUSH' => ZLIB_NO_FLUSH,
4444
];
4545

46-
/* Z_BLOCK is only defined when built against zlib > 1.2.3 */
47-
if (defined(ZLIB_BLOCK)) {
46+
/* Z_BLOCK is only available for deflate when built against zlib >= 1.2.4 */
47+
if (ZLIB_VERNUM >= 0x1240) {
4848
$flushTypes['ZLIB_BLOCK'] = ZLIB_BLOCK;
4949
}
5050

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--TEST--
2+
Test deflate_add() errors with ZLIB_BLOCK in zlib < 1.2.4
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded("zlib")) {
6+
print "skip - ZLIB extension not loaded";
7+
}
8+
if (ZLIB_VERNUM >= 0x1240) {
9+
print "skip - ZLIB < 1.2.4 required for test";
10+
}
11+
?>
12+
--FILE--
13+
<?php
14+
15+
$resource = deflate_init(ZLIB_ENCODING_GZIP);
16+
var_dump(deflate_add($resource, "aaaaaaaaaaaaaaaaaaaaaa", ZLIB_BLOCK));
17+
18+
?>
19+
===DONE===
20+
--EXPECTF--
21+
22+
Warning: deflate_add(): zlib >= 1.2.4 required for BLOCK deflate; current version: %s in %s on line %d
23+
bool(false)
24+
===DONE===

ext/zlib/tests/inflate_add_basic.phpt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,9 @@ $flushTypes = [
4040
'ZLIB_PARTIAL_FLUSH' => ZLIB_PARTIAL_FLUSH,
4141
'ZLIB_FULL_FLUSH' => ZLIB_FULL_FLUSH,
4242
'ZLIB_NO_FLUSH' => ZLIB_NO_FLUSH,
43+
'ZLIB_BLOCK' => ZLIB_BLOCK,
4344
];
4445

45-
/* Z_BLOCK is only defined when built against zlib > 1.2.3 */
46-
if (defined(ZLIB_BLOCK)) {
47-
$flushTypes['ZLIB_BLOCK'] = ZLIB_BLOCK;
48-
}
49-
5046
$uncompressed = "";
5147
for ($i=0;$i<(32768*2);$i++) {
5248
$uncompressed .= chr(rand(48,125));

ext/zlib/zlib.c

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,6 @@
4646
#undef gzseek
4747
#undef gztell
4848

49-
/* Z_BLOCK was added in zlib 1.2.4 and stable distros (RHEL6, at least) still
50-
* package zlib 1.2.3
51-
*/
52-
#ifdef Z_BLOCK
53-
#define HAVE_Z_BLOCK 1
54-
#endif
55-
5649
int le_deflate;
5750
int le_inflate;
5851

@@ -821,9 +814,7 @@ PHP_FUNCTION(inflate_add)
821814
case Z_PARTIAL_FLUSH:
822815
case Z_SYNC_FLUSH:
823816
case Z_FULL_FLUSH:
824-
#ifdef HAVE_Z_BLOCK
825817
case Z_BLOCK:
826-
#endif
827818
case Z_FINISH:
828819
break;
829820

@@ -965,13 +956,16 @@ PHP_FUNCTION(deflate_add)
965956
}
966957

967958
switch (flush_type) {
959+
case Z_BLOCK:
960+
#if ZLIB_VERNUM < 0x1240L
961+
php_error_docref(NULL, E_WARNING,
962+
"zlib >= 1.2.4 required for BLOCK deflate; current version: %s", ZLIB_VERSION);
963+
RETURN_FALSE;
964+
#endif
968965
case Z_NO_FLUSH:
969966
case Z_PARTIAL_FLUSH:
970967
case Z_SYNC_FLUSH:
971968
case Z_FULL_FLUSH:
972-
#ifdef HAVE_Z_BLOCK
973-
case Z_BLOCK:
974-
#endif
975969
case Z_FINISH:
976970
break;
977971

@@ -1279,10 +1273,12 @@ static PHP_MINIT_FUNCTION(zlib)
12791273
REGISTER_LONG_CONSTANT("ZLIB_PARTIAL_FLUSH", Z_PARTIAL_FLUSH, CONST_CS|CONST_PERSISTENT);
12801274
REGISTER_LONG_CONSTANT("ZLIB_SYNC_FLUSH", Z_SYNC_FLUSH, CONST_CS|CONST_PERSISTENT);
12811275
REGISTER_LONG_CONSTANT("ZLIB_FULL_FLUSH", Z_FULL_FLUSH, CONST_CS|CONST_PERSISTENT);
1282-
#ifdef HAVE_Z_BLOCK
12831276
REGISTER_LONG_CONSTANT("ZLIB_BLOCK", Z_BLOCK, CONST_CS|CONST_PERSISTENT);
1284-
#endif
12851277
REGISTER_LONG_CONSTANT("ZLIB_FINISH", Z_FINISH, CONST_CS|CONST_PERSISTENT);
1278+
1279+
REGISTER_STRING_CONSTANT("ZLIB_VERSION", ZLIB_VERSION, CONST_CS|CONST_PERSISTENT);
1280+
REGISTER_LONG_CONSTANT("ZLIB_VERNUM", ZLIB_VERNUM, CONST_CS|CONST_PERSISTENT);
1281+
12861282
REGISTER_INI_ENTRIES();
12871283
return SUCCESS;
12881284
}

0 commit comments

Comments
 (0)