diff options
Diffstat (limited to 'Zend/tests/try')
28 files changed, 726 insertions, 4 deletions
diff --git a/Zend/tests/try/bug70228.phpt b/Zend/tests/try/bug70228.phpt index 23f5864740..8b812517a3 100644 --- a/Zend/tests/try/bug70228.phpt +++ b/Zend/tests/try/bug70228.phpt @@ -1,16 +1,14 @@ --TEST-- Bug #70228 (memleak if return in finally block) ---XFAIL-- -See https://bugs.php.net/bug.php?id=70228 --FILE-- <?php function foo() { try { return str_repeat("a", 2); } - finally { return true; } + finally { return str_repeat("b", 2); } } var_dump(foo()); ?> --EXPECT-- -string(3) "bar" +string(2) "bb" diff --git a/Zend/tests/try/bug70228_2.phpt b/Zend/tests/try/bug70228_2.phpt new file mode 100644 index 0000000000..c988e706ce --- /dev/null +++ b/Zend/tests/try/bug70228_2.phpt @@ -0,0 +1,20 @@ +--TEST-- +Bug #70228 (memleak if return in finally block) +--FILE-- +<?php +function test() { + try { + throw new Exception(1); + } finally { + try { + throw new Exception(2); + } finally { + return 42; + } + } +} + +var_dump(test()); +?> +--EXPECT-- +int(42) diff --git a/Zend/tests/try/bug70228_3.phpt b/Zend/tests/try/bug70228_3.phpt new file mode 100644 index 0000000000..55dbe4f891 --- /dev/null +++ b/Zend/tests/try/bug70228_3.phpt @@ -0,0 +1,31 @@ +--TEST-- +Bug #70228 (memleak if return in finally block) +--FILE-- +<?php +function test() { + try { + throw new Exception(1); + } finally { + try { + try { + } finally { + return 42; + } + } finally { + throw new Exception(2); + } + } +} + +try { + var_dump(test()); +} catch (Exception $e) { + do { + echo $e->getMessage() . "\n"; + $e = $e->getPrevious(); + } while ($e); +} +?> +--EXPECT-- +2 +1 diff --git a/Zend/tests/try/bug70228_4.phpt b/Zend/tests/try/bug70228_4.phpt new file mode 100644 index 0000000000..f0ab3b0c2c --- /dev/null +++ b/Zend/tests/try/bug70228_4.phpt @@ -0,0 +1,32 @@ +--TEST-- +Bug #70228 (memleak if return in finally block) +--FILE-- +<?php +function test() { + try { + throw new Exception(1); + } finally { + try { + try { + try { + } finally { + return 42; + } + } finally { + throw new Exception(3); + } + } catch (Exception $e) {} + } +} + +try { + var_dump(test()); +} catch (Exception $e) { + do { + echo $e->getMessage() . "\n"; + $e = $e->getPrevious(); + } while ($e); +} +?> +--EXPECT-- +1 diff --git a/Zend/tests/try/bug70228_5.phpt b/Zend/tests/try/bug70228_5.phpt new file mode 100644 index 0000000000..29cbf4910d --- /dev/null +++ b/Zend/tests/try/bug70228_5.phpt @@ -0,0 +1,20 @@ +--TEST-- +Bug #70228 (memleak if return hidden by throw in finally block) +--FILE-- +<?php +function test() { + try { + return str_repeat("a", 2); + } finally { + throw new Exception("ops"); + } +} + +try { + test(); +} catch (Exception $e) { + echo $e->getMessage(), "\n"; +} +?> +--EXPECT-- +ops diff --git a/Zend/tests/try/bug70228_6.phpt b/Zend/tests/try/bug70228_6.phpt new file mode 100644 index 0000000000..fc68657f4c --- /dev/null +++ b/Zend/tests/try/bug70228_6.phpt @@ -0,0 +1,18 @@ +--TEST-- +Bug #70228 (memleak if return in finally block) +--FILE-- +<?php +function test($x) { + foreach ($x as $v) { + try { + return str_repeat("a", 2); + } finally { + return 42; + } + } +} + +var_dump(test([1])); +?> +--EXPECT-- +int(42) diff --git a/Zend/tests/try/bug70228_7.phpt b/Zend/tests/try/bug70228_7.phpt new file mode 100644 index 0000000000..4b8a80351c --- /dev/null +++ b/Zend/tests/try/bug70228_7.phpt @@ -0,0 +1,29 @@ +--TEST-- +Bug #70228 (memleak if return in finally block) +--FILE-- +<?php + +function foo() { + $array = [1, 2, $n = 3]; + foreach ($array as $value) { + var_dump($value); + try { + try { + foreach ($array as $_) { + return str_repeat("a", 2); + } + } finally { + throw new Exception; + } + } catch (Exception $e) { } + } +} + +foo(); +?> +===DONE=== +--EXPECT-- +int(1) +int(2) +int(3) +===DONE=== diff --git a/Zend/tests/try/bug70228_8.phpt b/Zend/tests/try/bug70228_8.phpt new file mode 100644 index 0000000000..ad13113c71 --- /dev/null +++ b/Zend/tests/try/bug70228_8.phpt @@ -0,0 +1,30 @@ +--TEST-- +Bug #70228 (memleak if return in finally block) +--FILE-- +<?php + +function foo() { + $array = [1, 2, $n = 3]; + foreach ($array as $value) { + var_dump($value); + try { + try { + switch (str_repeat("b", 2)) { + case "bb": + return str_repeat("a", 2); + } + } finally { + throw new Exception; + } + } catch (Exception $e) { } + } +} + +foo(); +?> +===DONE=== +--EXPECT-- +int(1) +int(2) +int(3) +===DONE=== diff --git a/Zend/tests/try/bug71604.phpt b/Zend/tests/try/bug71604.phpt new file mode 100644 index 0000000000..79803b93ea --- /dev/null +++ b/Zend/tests/try/bug71604.phpt @@ -0,0 +1,25 @@ +--TEST-- +Bug #71604: Aborted Generators continue after nested finally +--FILE-- +<?php +function gen() { + try { + try { + yield; + } finally { + print "INNER\n"; + } + } catch (Exception $e) { + print "EX\n"; + } finally { + print "OUTER\n"; + } + print "NOTREACHED\n"; +} + +gen()->current(); + +?> +--EXPECT-- +INNER +OUTER diff --git a/Zend/tests/try/bug71604_2.phpt b/Zend/tests/try/bug71604_2.phpt new file mode 100644 index 0000000000..8736cd8347 --- /dev/null +++ b/Zend/tests/try/bug71604_2.phpt @@ -0,0 +1,39 @@ +--TEST-- +Bug #71604: Aborted Generators continue after nested finally (2) +--FILE-- +<?php + +function gen() { + try { + throw new Exception(1); + } finally { + try { + throw new Exception(2); + } finally { + try { + yield; + } finally { + } + } + } +} + +try { + gen()->rewind(); +} catch (Exception $e) { + echo $e, "\n"; +} + +?> +--EXPECTF-- +Exception: 1 in %s:%d +Stack trace: +#0 [internal function]: gen() +#1 %s(%d): Generator->rewind() +#2 {main} + +Next Exception: 2 in %s:%d +Stack trace: +#0 [internal function]: gen() +#1 %s(%d): Generator->rewind() +#2 {main} diff --git a/Zend/tests/try/bug71604_3.phpt b/Zend/tests/try/bug71604_3.phpt new file mode 100644 index 0000000000..058c9a70a8 --- /dev/null +++ b/Zend/tests/try/bug71604_3.phpt @@ -0,0 +1,38 @@ +--TEST-- +Bug #71604: Aborted Generators continue after nested finally (3) +--FILE-- +<?php + +function gen() { + try { + throw new Exception(1); + } finally { + try { + yield; + } finally { + try { + throw new Exception(2); + } finally { + } + } + } +} + +try { + gen()->rewind(); +} catch (Exception $e) { + echo $e, "\n"; +} + +?> +--EXPECTF-- +Exception: 1 in %s:%d +Stack trace: +#0 [internal function]: gen() +#1 %s(%d): Generator->rewind() +#2 {main} + +Next Exception: 2 in %s:%d +Stack trace: +#0 %s(%d): gen() +#1 {main} diff --git a/Zend/tests/try/bug72213.phpt b/Zend/tests/try/bug72213.phpt new file mode 100644 index 0000000000..624050295e --- /dev/null +++ b/Zend/tests/try/bug72213.phpt @@ -0,0 +1,25 @@ +--TEST-- +Bug #72213 (Finally leaks on nested exceptions) +--FILE-- +<?php +function test() { + try { + throw new Exception('a'); + } finally { + try { + throw new Exception('b'); + } finally { + } + } +} + +try { + test(); +} catch (Exception $e) { + var_dump($e->getMessage()); + var_dump($e->getPrevious()->getMessage()); +} +?> +--EXPECT-- +string(1) "b" +string(1) "a" diff --git a/Zend/tests/try/bug72213_2.phpt b/Zend/tests/try/bug72213_2.phpt new file mode 100644 index 0000000000..790abe125d --- /dev/null +++ b/Zend/tests/try/bug72213_2.phpt @@ -0,0 +1,25 @@ +--TEST-- +Bug #72213 (Finally leaks on nested exceptions) +--FILE-- +<?php +function test() { + try { + throw new Exception(1); + } finally { + try { + try { + throw new Exception(2); + } finally { + } + } catch (Exception $e) { + } + } +} + +try { + test(); +} catch (Exception $e) { + echo "caught {$e->getMessage()}\n"; +} +--EXPECT-- +caught 1 diff --git a/Zend/tests/try/exceptions.inc b/Zend/tests/try/exceptions.inc new file mode 100644 index 0000000000..68cb1c62f7 --- /dev/null +++ b/Zend/tests/try/exceptions.inc @@ -0,0 +1,6 @@ +<?php + +class Exception1 extends Exception {} +class Exception2 extends Exception {} +class Exception3 extends Exception {} +class Exception4 extends Exception {} diff --git a/Zend/tests/try/try_finally_021.phpt b/Zend/tests/try/try_finally_021.phpt new file mode 100644 index 0000000000..ed162f40d0 --- /dev/null +++ b/Zend/tests/try/try_finally_021.phpt @@ -0,0 +1,20 @@ +--TEST-- +Live range & return from finally +--FILE-- +<?php +$array = [1]; +foreach ([0] as $_) { + foreach ($array as $v) { + try { + echo "ok\n"; + return; + } finally { + echo "ok\n"; + return; + } + } +} +?> +--EXPECT-- +ok +ok diff --git a/Zend/tests/try/try_finally_022.phpt b/Zend/tests/try/try_finally_022.phpt new file mode 100644 index 0000000000..51f6a26419 --- /dev/null +++ b/Zend/tests/try/try_finally_022.phpt @@ -0,0 +1,41 @@ +--TEST-- +Try finally (exception in "return" statement) +--FILE-- +<?php +class A { + public $x = 1; + public $y = 2; + function __destruct() { + throw new Exception(); + } +} +try{ + $a = 0; + switch ($a) { + case 0: + } + switch ($a) { + case 0: + } + switch ($a) { + case 0: + } + foreach([new stdClass()] as $x) { + foreach(new A() as $a) { + foreach([new stdClass()] as $y) { + try { + if (0) { echo "0" . (int)5; } + return $a; + } catch (Exception $e) { + echo "exception1\n"; + } + } + } + } +} catch (Exception $e) { + echo "exception2\n"; +} +?> +--EXPECT-- +exception2 + diff --git a/Zend/tests/try/try_finally_023.phpt b/Zend/tests/try/try_finally_023.phpt new file mode 100644 index 0000000000..e88eddb3b2 --- /dev/null +++ b/Zend/tests/try/try_finally_023.phpt @@ -0,0 +1,37 @@ +--TEST-- +Loop var dtor throwing exception during return inside try/catch inside finally +--FILE-- +<?php + +class Dtor { + public function __destruct() { + throw new Exception(2); + } +} + +function test() { + try { + throw new Exception(1); + } finally { + try { + foreach ([new Dtor] as $v) { + unset($v); + return 42; + } + } catch (Exception $e) { + } + } +} + +try { + test(); +} catch (Exception $e) { + echo $e, "\n"; +} + +?> +--EXPECTF-- +Exception: 1 in %s:%d +Stack trace: +#0 %s(%d): test() +#1 {main} diff --git a/Zend/tests/try/try_finally_024.phpt b/Zend/tests/try/try_finally_024.phpt new file mode 100644 index 0000000000..eb0b26acbf --- /dev/null +++ b/Zend/tests/try/try_finally_024.phpt @@ -0,0 +1,38 @@ +--TEST-- +Exception in finally inside finally following try/catch containing throwing try/finally +--FILE-- +<?php + +function test() { + try { + throw new Exception(1); + } finally { + try { + try { + } finally { + throw new Exception(2); + } + } catch (Exception $e) {} + try { + } finally { + throw new Exception(3); + } + } +} + +try { + test(); +} catch (Exception $e) { + echo $e, "\n"; +} +?> +--EXPECTF-- +Exception: 1 in %s:%d +Stack trace: +#0 %s(%d): test() +#1 {main} + +Next Exception: 3 in %s:%d +Stack trace: +#0 %s(%d): test() +#1 {main} diff --git a/Zend/tests/try/try_finally_025.phpt b/Zend/tests/try/try_finally_025.phpt new file mode 100644 index 0000000000..7ca535bcf6 --- /dev/null +++ b/Zend/tests/try/try_finally_025.phpt @@ -0,0 +1,28 @@ +--TEST-- +Throw in try of try/finally inside catch +--FILE-- +<?php + +function test() { + try { + throw new Exception(1); + } catch (Exception $e) { + try { + throw new Exception(2); + } finally { + } + } +} + +try { + test(); +} catch (Exception $e) { + echo $e, "\n"; +} + +?> +--EXPECTF-- +Exception: 2 in %s:%d +Stack trace: +#0 %s(%d): test() +#1 {main} diff --git a/Zend/tests/try/try_finally_026.phpt b/Zend/tests/try/try_finally_026.phpt new file mode 100644 index 0000000000..29a5de3b3a --- /dev/null +++ b/Zend/tests/try/try_finally_026.phpt @@ -0,0 +1,37 @@ +--TEST-- +Throw in finally inside catch inside finally +--FILE-- +<?php + +function test() { + try { + throw new Exception(1); + } finally { + try { + throw new Exception(2); + } catch (Exception $e) { + try { + } finally { + throw new Exception(3); + } + } + } +} + +try { + test(); +} catch (Exception $e) { + echo $e, "\n"; +} + +?> +--EXPECTF-- +Exception: 1 in %s:%d +Stack trace: +#0 %s(%d): test() +#1 {main} + +Next Exception: 3 in %s:%d +Stack trace: +#0 %s(%d): test() +#1 {main} diff --git a/Zend/tests/try/try_finally_027.phpt b/Zend/tests/try/try_finally_027.phpt new file mode 100644 index 0000000000..1e66479eb0 --- /dev/null +++ b/Zend/tests/try/try_finally_027.phpt @@ -0,0 +1,34 @@ +--TEST-- +Return in try with throw in finally, inside other finally +--FILE-- +<?php + +function test() { + try { + throw new Exception(1); + } finally { + try { + return 42; + } finally { + throw new Exception(2); + } + } +} + +try { + test(); +} catch (Exception $e) { + echo $e, "\n"; +} + +?> +--EXPECTF-- +Exception: 1 in %s:%d +Stack trace: +#0 %s(%d): test() +#1 {main} + +Next Exception: 2 in %s:%d +Stack trace: +#0 %s(%d): test() +#1 {main} diff --git a/Zend/tests/try/try_multicatch_001.phpt b/Zend/tests/try/try_multicatch_001.phpt new file mode 100644 index 0000000000..0dffd32c72 --- /dev/null +++ b/Zend/tests/try/try_multicatch_001.phpt @@ -0,0 +1,19 @@ +--TEST-- +Parsing test +--FILE-- +<?php + +require_once __DIR__ . '/exceptions.inc'; + +try { + echo 'TRY' . PHP_EOL; +} catch(Exception1 | Exception2 $e) { + echo 'Exception'; +} finally { + echo 'FINALLY' . PHP_EOL; +} + +?> +--EXPECT-- +TRY +FINALLY diff --git a/Zend/tests/try/try_multicatch_002.phpt b/Zend/tests/try/try_multicatch_002.phpt new file mode 100644 index 0000000000..0e70fec7eb --- /dev/null +++ b/Zend/tests/try/try_multicatch_002.phpt @@ -0,0 +1,21 @@ +--TEST-- +Catch first exception in the multicatch +--FILE-- +<?php + +require_once __DIR__ . '/exceptions.inc'; + +try { + echo 'TRY' . PHP_EOL; + throw new Exception1; +} catch(Exception1 | Exception2 | Exception3 $e) { + echo get_class($e) . PHP_EOL; +} finally { + echo 'FINALLY' . PHP_EOL; +} + +?> +--EXPECT-- +TRY +Exception1 +FINALLY diff --git a/Zend/tests/try/try_multicatch_003.phpt b/Zend/tests/try/try_multicatch_003.phpt new file mode 100644 index 0000000000..6aed1a2b09 --- /dev/null +++ b/Zend/tests/try/try_multicatch_003.phpt @@ -0,0 +1,21 @@ +--TEST-- +Catch second exception in the multicatch +--FILE-- +<?php + +require_once __DIR__ . '/exceptions.inc'; + +try { + echo 'TRY' . PHP_EOL; + throw new Exception2; +} catch(Exception1 | Exception2 | Exception3 $e) { + echo get_class($e) . PHP_EOL; +} finally { + echo 'FINALLY' . PHP_EOL; +} + +?> +--EXPECT-- +TRY +Exception2 +FINALLY diff --git a/Zend/tests/try/try_multicatch_004.phpt b/Zend/tests/try/try_multicatch_004.phpt new file mode 100644 index 0000000000..d8b245a767 --- /dev/null +++ b/Zend/tests/try/try_multicatch_004.phpt @@ -0,0 +1,21 @@ +--TEST-- +Catch last exception in the multicatch +--FILE-- +<?php + +require_once __DIR__ . '/exceptions.inc'; + +try { + echo 'TRY' . PHP_EOL; + throw new Exception3; +} catch(Exception1 | Exception2 | Exception3 $e) { + echo get_class($e) . PHP_EOL; +} finally { + echo 'FINALLY' . PHP_EOL; +} + +?> +--EXPECT-- +TRY +Exception3 +FINALLY diff --git a/Zend/tests/try/try_multicatch_005.phpt b/Zend/tests/try/try_multicatch_005.phpt new file mode 100644 index 0000000000..cc3fc890fa --- /dev/null +++ b/Zend/tests/try/try_multicatch_005.phpt @@ -0,0 +1,25 @@ +--TEST-- +Catch exception in the nested multicatch +--FILE-- +<?php + +require_once __DIR__ . '/exceptions.inc'; + +try { + try { + echo 'TRY' . PHP_EOL; + throw new Exception3; + } catch (Exception1 | Exception3 $e) { + echo get_class($e) . PHP_EOL; + } +} catch(Exception2 | Exception3 $e) { + echo 'Should never be executed'; +} finally { + echo 'FINALLY' . PHP_EOL; +} + +?> +--EXPECT-- +TRY +Exception3 +FINALLY diff --git a/Zend/tests/try/try_multicatch_006.phpt b/Zend/tests/try/try_multicatch_006.phpt new file mode 100644 index 0000000000..e4505f1b24 --- /dev/null +++ b/Zend/tests/try/try_multicatch_006.phpt @@ -0,0 +1,22 @@ +--TEST-- +Catch first exception in the second multicatch +--FILE-- +<?php +require_once __DIR__ . '/exceptions.inc'; + +try { + echo 'TRY' . PHP_EOL; + throw new Exception3; +} catch(Exception1 | Exception2 $e) { + echo get_class($e) . PHP_EOL; +} catch(Exception3 | Exception4 $e) { + echo get_class($e) . PHP_EOL; +} finally { + echo 'FINALLY' . PHP_EOL; +} +?> +--EXPECT-- +TRY +Exception3 +FINALLY + diff --git a/Zend/tests/try/try_multicatch_007.phpt b/Zend/tests/try/try_multicatch_007.phpt new file mode 100644 index 0000000000..aa073b0592 --- /dev/null +++ b/Zend/tests/try/try_multicatch_007.phpt @@ -0,0 +1,22 @@ +--TEST-- +Catch second exception in the second multicatch +--FILE-- +<?php +require_once __DIR__ . '/exceptions.inc'; + +try { + echo 'TRY' . PHP_EOL; + throw new Exception4; +} catch(Exception1 | Exception2 $e) { + echo get_class($e) . PHP_EOL; +} catch(Exception3 | Exception4 $e) { + echo get_class($e) . PHP_EOL; +} finally { + echo 'FINALLY' . PHP_EOL; +} +?> +--EXPECT-- +TRY +Exception4 +FINALLY + |
