Skip to content

Commit

Permalink
Fix segfault on infinite recursion into circular model relations
Browse files Browse the repository at this point in the history
In cases where related models were set before save on *both* sides of a
hasMany/belongsTo relationship, `Model::save()` would go into an infinite
loop as its pre and post-save checks for relations to save never checked
if the related model had already been saved:

    Save A
     \_ A hasMany B
      \_ Save B
       \_ B belongsTo A
        \_ Save A
         \_ ...

For me this was causing a segfault, though with xdebug installed it would
error before crashing at the max_nesting_level.
  • Loading branch information
stecman committed Oct 14, 2018
1 parent 28886f3 commit 11d0edc
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion phalcon/mvc/model.zep
Original file line number Diff line number Diff line change
Expand Up @@ -2793,8 +2793,9 @@ abstract class Model implements EntityInterface, ModelInterface, ResultInterface

/**
* If dynamic update is enabled, saving the record must not take any action
* Only save if the model is dirty to prevent circular relations causing an infinite loop
*/
if !record->save() {
if record->_dirtyState != Model::DIRTY_STATE_PERSISTENT && !record->save() {

/**
* Get the validation messages generated by the referenced model
Expand Down

0 comments on commit 11d0edc

Please sign in to comment.