]> granicus.if.org Git - php/commitdiff
Improve ZBLOCK handling with zlib < 1.2.4
authorDaniel Lowrey <rdlowrey@php.net>
Thu, 7 May 2015 14:26:56 +0000 (10:26 -0400)
committerDaniel Lowrey <rdlowrey@php.net>
Thu, 7 May 2015 14:31:41 +0000 (10:31 -0400)
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).

ext/zlib/tests/deflate_add_basic.phpt
ext/zlib/tests/deflate_add_block_v123.phpt [new file with mode: 0644]
ext/zlib/tests/inflate_add_basic.phpt
ext/zlib/zlib.c

index fde22b5a52511e97472a3d3821fbdf73c581c873..05fcb037bd56198ac13a7c68e58ea858bde33163 100644 (file)
@@ -43,8 +43,8 @@ $flushTypes = [
     'ZLIB_NO_FLUSH' => ZLIB_NO_FLUSH,
 ];
 
-/* Z_BLOCK is only defined when built against zlib > 1.2.3 */
-if (defined(ZLIB_BLOCK)) {
+/* Z_BLOCK is only available for deflate when built against zlib >= 1.2.4 */
+if (ZLIB_VERNUM >= 0x1240) {
     $flushTypes['ZLIB_BLOCK'] = ZLIB_BLOCK;
 }
 
diff --git a/ext/zlib/tests/deflate_add_block_v123.phpt b/ext/zlib/tests/deflate_add_block_v123.phpt
new file mode 100644 (file)
index 0000000..3fe03f9
--- /dev/null
@@ -0,0 +1,24 @@
+--TEST--
+Test deflate_add() errors with ZLIB_BLOCK in zlib < 1.2.4
+--SKIPIF--
+<?php 
+if (!extension_loaded("zlib")) {
+    print "skip - ZLIB extension not loaded";
+}
+if (ZLIB_VERNUM >= 0x1240) {
+    print "skip - ZLIB < 1.2.4 required for test";
+}
+?>
+--FILE--
+<?php
+
+$resource = deflate_init(ZLIB_ENCODING_GZIP);
+var_dump(deflate_add($resource, "aaaaaaaaaaaaaaaaaaaaaa", ZLIB_BLOCK));
+
+?>
+===DONE===
+--EXPECTF--
+
+Warning: deflate_add(): zlib >= 1.2.4 required for BLOCK deflate; current version: %s in %s on line %d
+bool(false)
+===DONE===
index 7ef5ef11cb52c883ad97a4166f1cd249e628bb62..ff0458e9ef89e55bba747dbe3f5d2f2743e55de2 100644 (file)
@@ -40,13 +40,9 @@ $flushTypes = [
     'ZLIB_PARTIAL_FLUSH' => ZLIB_PARTIAL_FLUSH,
     'ZLIB_FULL_FLUSH' => ZLIB_FULL_FLUSH,
     'ZLIB_NO_FLUSH' => ZLIB_NO_FLUSH,
+    'ZLIB_BLOCK' => ZLIB_BLOCK,
 ];
 
-/* Z_BLOCK is only defined when built against zlib > 1.2.3 */
-if (defined(ZLIB_BLOCK)) {
-    $flushTypes['ZLIB_BLOCK'] = ZLIB_BLOCK;
-}
-
 $uncompressed = "";
 for ($i=0;$i<(32768*2);$i++) {
     $uncompressed .= chr(rand(48,125));
index 2b9c016a41dba08f5968eb6f6b7198541a518dfe..c375613ebac17745065f11cadcdf7300320a07a1 100644 (file)
 #undef gzseek
 #undef gztell
 
-/* Z_BLOCK was added in zlib 1.2.4 and stable distros (RHEL6, at least) still
- * package zlib 1.2.3
- */
-#ifdef Z_BLOCK
-#define HAVE_Z_BLOCK 1
-#endif
-
 int le_deflate;
 int le_inflate;
 
@@ -821,9 +814,7 @@ PHP_FUNCTION(inflate_add)
                case Z_PARTIAL_FLUSH:
                case Z_SYNC_FLUSH:
                case Z_FULL_FLUSH:
-#ifdef HAVE_Z_BLOCK
                case Z_BLOCK:
-#endif
                case Z_FINISH:
                        break;
 
@@ -965,13 +956,16 @@ PHP_FUNCTION(deflate_add)
        }
 
        switch (flush_type) {
+               case Z_BLOCK:
+#if ZLIB_VERNUM < 0x1240L
+                       php_error_docref(NULL, E_WARNING,
+                               "zlib >= 1.2.4 required for BLOCK deflate; current version: %s", ZLIB_VERSION);
+                       RETURN_FALSE;
+#endif
                case Z_NO_FLUSH:
                case Z_PARTIAL_FLUSH:
                case Z_SYNC_FLUSH:
                case Z_FULL_FLUSH:
-#ifdef HAVE_Z_BLOCK
-               case Z_BLOCK:
-#endif
                case Z_FINISH:
                        break;
 
@@ -1279,10 +1273,12 @@ static PHP_MINIT_FUNCTION(zlib)
        REGISTER_LONG_CONSTANT("ZLIB_PARTIAL_FLUSH", Z_PARTIAL_FLUSH, CONST_CS|CONST_PERSISTENT);
        REGISTER_LONG_CONSTANT("ZLIB_SYNC_FLUSH", Z_SYNC_FLUSH, CONST_CS|CONST_PERSISTENT);
        REGISTER_LONG_CONSTANT("ZLIB_FULL_FLUSH", Z_FULL_FLUSH, CONST_CS|CONST_PERSISTENT);
-#ifdef HAVE_Z_BLOCK
        REGISTER_LONG_CONSTANT("ZLIB_BLOCK", Z_BLOCK, CONST_CS|CONST_PERSISTENT);
-#endif
        REGISTER_LONG_CONSTANT("ZLIB_FINISH", Z_FINISH, CONST_CS|CONST_PERSISTENT);
+
+       REGISTER_STRING_CONSTANT("ZLIB_VERSION", ZLIB_VERSION, CONST_CS|CONST_PERSISTENT);
+       REGISTER_LONG_CONSTANT("ZLIB_VERNUM", ZLIB_VERNUM, CONST_CS|CONST_PERSISTENT);
+
        REGISTER_INI_ENTRIES();
        return SUCCESS;
 }