php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #32993 implemented Iterator function current() don't throw exception
Submitted: 2005-05-10 06:17 UTC Modified: 2005-06-06 10:25 UTC
Votes:3
Avg. Score:4.3 ± 0.9
Reproduced:3 of 3 (100.0%)
Same Version:3 (100.0%)
Same OS:3 (100.0%)
From: vojtech at x dot cz Assigned: helly
Status: Closed Package: Scripting Engine problem
PHP Version: 5.0.4 OS: *
Private report: No CVE-ID:
 [2005-05-10 06:17 UTC] vojtech at x dot cz
Description:
------------
It's seems there is not correctly processed exception from current() and script ends up with fatal error.

Reproduce code:
---------------
class Test implements Iterator {

    public $arr = array();

    public function rewind()    { return reset($this->arr); }
    public function current()   { throw new Exception(); }
    public function key()       { return key($this->arr); }
    public function next()      { return next($this->arr); }
    public function valid()     { return (current($this->arr) !== false); }
}

$t = new Test();
$t->arr =  array(1, 2, 3);

try {
    foreach ($t as $v) {
        ; // do something
    }
} catch (Exception $e) {
    ; // handle exception
}


Actual result:
--------------
Fatal error: Couldn't execute method Test::key in Unknown on line 0

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-05-10 08:46 UTC] helly@php.net
Results in a SEGV in head
 [2005-05-31 11:21 UTC] tjerk dot meesters at gmail dot com
I've had a similar problem, this time with an exception in next():

class MyIterator implements Iterator
{
        function rewind() {}
        function next() { throw new Exception('next()'); }
        function valid() { return true; }
        function current() { return 'test'; }
        function key() { return 'test'; }
}

try {
        foreach (new MyIterator() as $x => $y) {
                // do something
        }
} catch (Exception $e) {
        echo "{$e->getMessage()}\n";
}

--- result ---
Fatal error: Couldn't execute method MyIterator::valid in Unknown on line 0

--- version ---
PHP 5.0.4 (cli) on Linux (Zend Engine v2.0.4-dev)
 [2005-06-06 10:25 UTC] dmitry@php.net
Fixed in CVS HEAD. It was already fixed in PHP_5_0.
 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Mon Apr 21 12:02:07 2014 UTC