Skip to content

Commit 0965193

Browse files
committed
Convert more zlib warnings to exceptions
This PR further extends the functionality of a previous PR #4985. Ran into this while working on a [particular WordPress functionality](Automattic/jetpack#34186) - our servers reported tons of warnings. Currently, the only way to avoid warnings is to call `@gzinflate`, but silencing errors is discouraged so we also have to `phpcs:ignore` it. If `gzinflate` threw an exception instead, it would be much easier for developers to handle this. Applied same reasoning to all the zlib functions. I also found out that the `json` extension also does not use any notices: ``` bor0:~/dev/php-src/ext/json$ grep -R php_error_docref * bor0:~/dev/php-src/ext/json$ ``` ⚠️This is a breaking change and may cause code flow interruptions unless handled with exceptions⚠️
1 parent 931a8b0 commit 0965193

22 files changed

+279
-213
lines changed

ext/standard/tests/filters/filter_errors_zlib_inflate.phpt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ Filter errors: zlib.inflate
55
--FILE--
66
<?php
77
require 'filter_errors.inc';
8-
filter_errors_test('zlib.inflate', gzencode('42'));
8+
try {
9+
filter_errors_test('zlib.inflate', gzencode('42'));
10+
} catch (\Throwable $e) {
11+
echo $e->getMessage() . \PHP_EOL;
12+
}
913
?>
1014
--EXPECTF--
1115
test filtering of buffered data
1216

13-
Notice: stream_filter_append(): zlib: data error in %s on line %d
14-
1517
Warning: stream_filter_append(): Filter failed to process pre-buffered data in %s
16-
test filtering of non buffered data
17-
18-
Notice: stream_get_contents(): zlib: data error in %s on line %d
18+
zlib: data error

ext/zlib/tests/005.phpt

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ zlib
77

88
try {
99
var_dump(gzcompress("", 1000));
10-
} catch (\ValueError $e) {
10+
} catch (\Throwable $e) {
1111
echo $e->getMessage() . \PHP_EOL;
1212
}
1313

@@ -24,23 +24,36 @@ var_dump($data2 = gzcompress($string, 9));
2424

2525
try {
2626
var_dump(gzuncompress("", 1000));
27-
} catch (\ValueError $e) {
27+
} catch (\Throwable $e) {
2828
echo $e->getMessage() . \PHP_EOL;
2929
}
3030

3131
try {
3232
var_dump(gzuncompress("", -1));
33-
} catch (\ValueError $e) {
33+
} catch (\Throwable $e) {
3434
echo $e->getMessage() . \PHP_EOL;
3535
}
3636

37-
var_dump(gzuncompress(""));
38-
var_dump(gzuncompress("", 9));
37+
try {
38+
var_dump(gzuncompress(""));
39+
} catch (\Throwable $e) {
40+
echo $e->getMessage() . \PHP_EOL;
41+
}
42+
43+
try {
44+
var_dump(gzuncompress("", 9));
45+
} catch (\Throwable $e) {
46+
echo $e->getMessage() . \PHP_EOL;
47+
}
3948

4049
var_dump(gzuncompress($data1));
4150
var_dump(gzuncompress($data2));
4251
$data2[4] = 0;
43-
var_dump(gzuncompress($data2));
52+
try {
53+
var_dump(gzuncompress($data2));
54+
} catch (\Throwable $e) {
55+
echo $e->getMessage() . \PHP_EOL;
56+
}
4457

4558
?>
4659
--EXPECTF--
@@ -50,22 +63,14 @@ string(%d) "%a"
5063
string(%d) "%a"
5164
string(%d) "%a"
5265
string(%d) "%a"
53-
54-
Warning: gzuncompress(): %s error in %s on line %d
55-
bool(false)
66+
data error
5667
gzuncompress(): Argument #2 ($max_length) must be greater than or equal to 0
57-
58-
Warning: gzuncompress(): %s error in %s on line %d
59-
bool(false)
60-
61-
Warning: gzuncompress(): %s error in %s on line %d
62-
bool(false)
68+
data error
69+
data error
6370
string(94) "Answer me, it can't be so hard
6471
Cry to relieve what's in your heart
6572
Desolation, grief and agony"
6673
string(94) "Answer me, it can't be so hard
6774
Cry to relieve what's in your heart
6875
Desolation, grief and agony"
69-
70-
Warning: gzuncompress(): %s error in %s on line %d
71-
bool(false)
76+
data error

ext/zlib/tests/006.phpt

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ zlib
77

88
try {
99
var_dump(gzcompress("", 1000));
10-
} catch (\ValueError $e) {
10+
} catch (\Throwable $e) {
1111
echo $e->getMessage() . \PHP_EOL;
1212
}
1313

@@ -23,23 +23,44 @@ Desolation, grief and agony";
2323
var_dump($data1 = gzdeflate($string));
2424
var_dump($data2 = gzdeflate($string, 9));
2525

26-
var_dump(gzinflate(""));
27-
var_dump(gzinflate("asfwe", 1000));
26+
try {
27+
var_dump(gzinflate(""));
28+
} catch (\Throwable $e) {
29+
echo $e->getMessage() . \PHP_EOL;
30+
}
31+
32+
try {
33+
var_dump(gzinflate("asfwe", 1000));
34+
} catch (\Throwable $e) {
35+
echo $e->getMessage() . \PHP_EOL;
36+
}
2837

2938
try {
3039
var_dump(gzinflate("asdf", -1));
31-
} catch (\ValueError $e) {
40+
} catch (\Throwable $e) {
41+
echo $e->getMessage() . \PHP_EOL;
42+
}
43+
44+
try {
45+
var_dump(gzinflate("asdf"));
46+
} catch (\Throwable $e) {
3247
echo $e->getMessage() . \PHP_EOL;
3348
}
3449

35-
var_dump(gzinflate("asdf"));
36-
var_dump(gzinflate("asdf", 9));
50+
try {
51+
var_dump(gzinflate("asdf", 9));
52+
} catch (\Throwable $e) {
53+
echo $e->getMessage() . \PHP_EOL;
54+
}
3755

3856
var_dump(gzinflate($data1));
3957
var_dump(gzinflate($data2));
4058
$data2[4] = 0;
41-
var_dump(gzinflate($data2));
42-
59+
try {
60+
var_dump(gzinflate($data2));
61+
} catch (\Throwable $e) {
62+
echo $e->getMessage() . \PHP_EOL;
63+
}
4364
?>
4465
--EXPECTF--
4566
gzcompress(): Argument #2 ($level) must be between -1 and 9
@@ -48,25 +69,15 @@ string(%d) "%a"
4869
string(%d) "%a"
4970
string(%d) "%a"
5071
string(%d) "%a"
51-
52-
Warning: gzinflate(): data error in %s on line %d
53-
bool(false)
54-
55-
Warning: gzinflate(): data error in %s on line %d
56-
bool(false)
72+
data error
73+
data error
5774
gzinflate(): Argument #2 ($max_length) must be greater than or equal to 0
58-
59-
Warning: gzinflate(): data error in %s on line %d
60-
bool(false)
61-
62-
Warning: gzinflate(): data error in %s on line %d
63-
bool(false)
75+
data error
76+
data error
6477
string(94) "Answer me, it can't be so hard
6578
Cry to relieve what's in your heart
6679
Desolation, grief and agony"
6780
string(94) "Answer me, it can't be so hard
6881
Cry to relieve what's in your heart
6982
Desolation, grief and agony"
70-
71-
Warning: gzinflate(): data error in %s on line %d
72-
bool(false)
83+
data error

ext/zlib/tests/bug61139.phpt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ Bug #61139 (gzopen leaks when specifying invalid mode)
44
zlib
55
--FILE--
66
<?php
7-
8-
gzopen('someFile', 'c');
9-
?>
7+
try {
8+
gzopen('someFile', 'c');
9+
} catch (\Throwable $e) {
10+
echo $e->getMessage() . \PHP_EOL;
11+
}
1012
--CLEAN--
1113
<?php
1214
unlink('someFile');
1315
?>
1416
--EXPECTF--
15-
Warning: gzopen(): gzopen failed in %s on line %d
17+
gzopen failed

ext/zlib/tests/bug71417.phpt

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,23 @@ function test($case) {
4545
// The gzdecode() function applied to the corrupted compressed data always
4646
// detects the error:
4747
// --> gzdecode(): PHP Fatal error: Uncaught ErrorException: gzdecode(): data error in ...
48-
echo "gzdecode(): ", rawurldecode(gzdecode($compressed)), "\n";
48+
try {
49+
echo "gzdecode(): ", rawurldecode(gzdecode($compressed)), "\n";
50+
} catch (\Throwable $e) {
51+
echo $e->getMessage() . \PHP_EOL;
52+
}
4953

5054
file_put_contents($fn, $compressed);
5155

5256
$r = fopen($fn, "r");
5357
stream_filter_append($r, 'zlib.inflate', STREAM_FILTER_READ, array('window' => 15+16));
5458
while (!feof($r)) {
55-
$s = fread($r, 100);
56-
echo "read: "; var_dump($s);
59+
try {
60+
$s = fread($r, 100);
61+
echo "read: "; var_dump($s);
62+
} catch (\Throwable $e) {
63+
echo $e->getMessage() . \PHP_EOL;
64+
}
5765
}
5866
fclose($r);
5967
unlink($fn);
@@ -66,25 +74,11 @@ test(4);
6674

6775
?>
6876
--EXPECTF--
69-
gzdecode():
70-
Warning: gzdecode(): data error in %s on line %d
71-
72-
73-
Notice: fread(): zlib: data error in %s on line %d
74-
read: bool(false)
75-
gzdecode():
76-
Warning: gzdecode(): data error in %s on line %d
77-
77+
gzdecode(): data error
78+
zlib: data error
79+
gzdecode(): data error
7880
read: string(32) "The quick brown fox jumps over t"
79-
gzdecode():
80-
Warning: gzdecode(): data error in %s on line %d
81-
82-
83-
Notice: fread(): zlib: data error in %s on line %d
84-
read: bool(false)
85-
gzdecode():
86-
Warning: gzdecode(): data error in %s on line %d
87-
88-
89-
Notice: fread(): zlib: data error in %s on line %d
90-
read: bool(false)
81+
gzdecode(): data error
82+
zlib: data error
83+
gzdecode(): data error
84+
zlib: data error

ext/zlib/tests/bug_52944.phpt

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,20 @@ require __DIR__ . "/bug_52944_corrupted_data.inc";
1414

1515
$fp = fopen('data://text/plain;base64,' . $data, 'r');
1616
stream_filter_append($fp, 'zlib.inflate', STREAM_FILTER_READ);
17-
var_dump(fread($fp,1));
18-
var_dump(fread($fp,1));
17+
try {
18+
var_dump(fread($fp,1));
19+
} catch (\Throwable $e) {
20+
echo $e->getMessage() . \PHP_EOL;
21+
}
22+
try {
23+
var_dump(fread($fp,1));
24+
} catch (\Throwable $e) {
25+
echo $e->getMessage() . \PHP_EOL;
26+
}
1927
fclose($fp);
2028
echo "Done.\n";
2129
?>
2230
--EXPECTF--
23-
Notice: fread(): zlib: data error in %s on line %d
24-
bool(false)
31+
zlib: data error
2532
string(0) ""
2633
Done.

ext/zlib/tests/dictionary_usage.phpt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ $r = inflate_init(ZLIB_ENCODING_DEFLATE, ["dictionary" => $dict]);
1919
var_dump(inflate_add($r, $a, ZLIB_FINISH));
2020

2121
$r = inflate_init(ZLIB_ENCODING_DEFLATE, ["dictionary" => ["8"] + range("a", "z")]);
22-
var_dump(inflate_add($r, $a, ZLIB_FINISH));
22+
try {
23+
var_dump(inflate_add($r, $a, ZLIB_FINISH));
24+
} catch (\Throwable $e) {
25+
echo $e->getMessage() . \PHP_EOL;
26+
}
2327

2428
?>
2529
--EXPECTF--
2630
string(%d) "%s"
2731
bool(true)
2832
string(6) "abdcde"
29-
30-
Warning: inflate_add(): Dictionary does not match expected dictionary (incorrect adler32 hash) in %s on line %d
31-
bool(false)
33+
Dictionary does not match expected dictionary (incorrect adler32 hash)

ext/zlib/tests/gzinflate-bug42663.phpt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ var_dump(strlen($deflated));
1515
$truncated = substr($deflated, 0, 65535);
1616
var_dump(strlen($truncated));
1717
// inflate $truncated string (check if it will not eat all memory)
18-
var_dump(gzinflate($truncated));
18+
try {
19+
var_dump(gzinflate($truncated));
20+
} catch (\Throwable $e) {
21+
echo $e->getMessage() . \PHP_EOL;
22+
}
1923
?>
2024
--EXPECTF--
2125
int(168890)
2226
int(66743)
2327
int(65535)
24-
25-
Warning: gzinflate(): data error in %s on line %d
26-
bool(false)
28+
data error

ext/zlib/tests/gzinflate_error1.phpt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ $data = 'string_val';
1313
$short_len = strlen($data) - 1;
1414
$compressed = gzcompress($data);
1515

16-
var_dump(gzinflate($compressed, $short_len));
16+
try {
17+
var_dump(gzinflate($compressed, $short_len));
18+
} catch (\Throwable $e) {
19+
echo $e->getMessage() . \PHP_EOL;
20+
}
1721

1822
?>
1923
--EXPECTF--
2024
*** Testing gzinflate() : error conditions ***
2125

2226
-- Testing with a buffer that is too small --
23-
24-
Warning: gzinflate(): data error in %s on line %d
25-
bool(false)
27+
data error

ext/zlib/tests/gzinflate_length.phpt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@ if (strcmp($original,$unpacked)==0) echo "Strings are equal\n";
1313
$unpacked=gzinflate($packed, strlen($original)*10);
1414
if (strcmp($original,$unpacked)==0) echo "Strings are equal\n";
1515

16-
$unpacked=gzinflate($packed, 1);
17-
if ($unpacked === false) echo "Failed (as expected)\n";
16+
try {
17+
$unpacked=gzinflate($packed, 1);
18+
} catch (\Throwable $e) {
19+
echo $e->getMessage() . \PHP_EOL;
20+
}
1821
?>
1922
--EXPECTF--
2023
5 15
2124
Strings are equal
2225
Strings are equal
23-
24-
Warning: gzinflate(): insufficient memory in %s on line %d
25-
Failed (as expected)
26+
insufficient memory

0 commit comments

Comments
 (0)