fuel / orm

Fuel PHP Framework - Fuel v1.x ORM
http://fuelphp.com/docs/packages/orm/intro.html
151 stars 95 forks source link

Memory exhaustion in ORM\Model->to_array #406

Closed tored closed 7 years ago

tored commented 7 years ago

When doing a to_array on a model object, related models will be revisited circularly and exhaust memory. Our models worked prior upgrading from FuelPHP 1.7.2 to FuelPHP 1.8.0

We think we have identified the problem to a static array called $to_array_references that keeps track of already visited models.

In the 1.9 branch on line 2143 a visited model class name will be popped, however this is done within a foreach-loop and therefore will pop more elements that was previously added on line 2139

This will pop already visited models and therefore they can be visited again.

By moving line 2143 outside of the foreach loop solves the problem and is consistent with how non-array relationships work on line 2158 and 2160

Behavior for this reference counting was changed in ORM\Model between version 1.7.2 & 1.7.3, so this problem exists in branches 1.7, 1.8 and 1.9.

WanWizard commented 7 years ago

Just to make sure I understand you, this is about https://github.com/fuel/orm/commit/7478c1b56b2fbc299d579b7d46dc422bcc4a074b ?

And it should be

foreach ($rel as $id => $r)
{
    $array[$name][$id] = $r->to_array($custom, true, $eav);
}
array_pop(static::$to_array_references);

Correct?

tored commented 7 years ago

Yes.

WanWizard commented 7 years ago

Ok, thanks.