Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.1] Make sure unguarded() does not change unguarded state on exception #13186

Merged
merged 1 commit into from
Apr 18, 2016

Conversation

jeroennoten
Copy link
Contributor

This makes sure that when an exception is thrown in the callback passed to Model::unguarded(), the Model is still guarded afterwards. It uses the try .. finally construct. I also added a unit test.

A use case when this can happen is in FactoryBuilder::makeInstance, which throws an exception when it cannot find the model factory. In tests, this can result in strange errors in other tests, because all models are unguarded afterwards (also in other test cases) when this exception is thrown. So then tests will fail when they shouldn't. This PR prevents this kind of situations.

$result = $callback();
} finally {
static::reguard();
}

return $result;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this now be undefined if an exception is thrown and $result is never set?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeh, this code is broken atm.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change it to:

try {
    return $callback();
} finally {
    static::reguard();
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it does not matter. When the callback throws an exception, this method throws that exception and thus does not return anything. A method either returns or throws an exception. Not both. Or am I wrong now?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that is true, but writing the return inside the try like that is definitely preferred please

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, that looks indeed better. I will change it a.s.a.p.

@jeroennoten
Copy link
Contributor Author

Updated as proposed by @GrahamCampbell: moved the return statement into the try block.

@GrahamCampbell
Copy link
Member

👍

@JosephSilber
Copy link
Member

I don't like returning before a finally. Messes with my brain.

But that's just me.

@jeroennoten
Copy link
Contributor Author

Yeah.. It's subjective.

@taylorotwell taylorotwell merged commit 712332c into laravel:5.1 Apr 18, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants