php-ds / ext-ds

An extension providing efficient data structures for PHP 7

Home Page:https://medium.com/p/9dda7af674cd

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Segmentation fault when Map is used with IteratorIterator (v1.3.0)

davidburtontr opened this issue · comments

On php 7.2 (specifically PHP 7.2.34) with version v1.3.0 this example code:

<?php

//require __DIR__ . '/vendor/autoload.php';

use Ds\Map;

class Messages implements IteratorAggregate
{
    private $messages;

    public function __construct()
    {
        $this->messages = new Map;
    }

    public function addMessage(string $key, string $message)
    {
        $this->messages->put($key, (object) ['key' => $key, 'message' => $message]);
    }

    public function getIterator(): Iterator
    {
        return new IteratorIterator($this->messages);
    }
}

$messages = new Messages();

$messages->addMessage('first', 'message');

foreach ($messages as $message) {
    print_r($message);
}

Results in a Segmentation fault (core dumped).

Using polyfill php-ds/php-ds (with the require uncommented)
Results in

stdClass Object
(
    [key] => first
    [message] => message
)

As expected.

We found a server that still had v1.2.9 on, so can confirm this issue was introduced with v1.3.0.

Slightly related to #166 - I think that fixing that issue might fix this as well as a side-effect.

Updated to confirm that this does not occur on v1.2.9.
This may be a duplicate of #159.

I just tested this on 1.4.0:

stdClass Object
(
    [key] => first
    [message] => message
)

Since this commit, the base collection class implements IteratorAggregate, not Traversable.
f4fee1a#diff-20e6612cc8f192e2682cdb770c6da2266df5566a636974e2cdc87227eda5e89d