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

Fix access of undefined array index file when internally analyzing stack frames #50

Merged
merged 3 commits into from
Dec 15, 2022

Conversation

reinfi
Copy link
Contributor

@reinfi reinfi commented Aug 25, 2022

Q A
Documentation no
Bugfix yes
BC Break no
New Feature no
RFC no
QA no

Description

When triggering events from within a controller plugin in laminas mvc the index is not a file because it is an invokable function.

4 = {array} [5] function = "__invoke" class = "Application\Controller\Plugin\ForwardToPlugin" object = {Application\Controller\Plugin\ForwardToPlugin} [3] type = "->" args = {array} [2]

I just checked if the index exists and return an empty string.

Copy link
Member

@Ocramius Ocramius left a comment

Choose a reason for hiding this comment

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

Needs test additions

@reinfi
Copy link
Contributor Author

reinfi commented Dec 13, 2022

Needs test additions

Yeah would love to add a test but it is quite difficult because there are no existing tests for the class and mocking the native buildin functions is quite hard.

Any suggestions how to start this?

@Ocramius
Copy link
Member

I think a backtrace is not available when the code is either internal, or eval()'d?

A test could do something like:

$provider = new EventContextProvider();

$file = eval(<<<'PHP'
return (function (): void {
    return $provider->getEventTriggerFile();
})();
PHP
);

self::assertSame('', $file);

Alternatively using array_map() could perhaps get the same result.

@reinfi
Copy link
Contributor Author

reinfi commented Dec 13, 2022

I added a crazy testcase for this specific case 🙈

@Ocramius
Copy link
Member

Test case makes sense, now we need to make CI green :D

Also: 100% sure this makes PHPUnit fail when your patch is not applied?

@samsonasik
Copy link
Member

Target branch need to be changed to 2.7.x or 2.8.x and rebase.

@froschdesign
Copy link
Member

Target branch need to be changed to 2.7.x or 2.8.x and rebase.

A bugfix goes to the current release branch (2.7.x):

What branch to issue the pull request against?

Which branch should you issue a pull request against?

  • For fixes against the stable release, issue the pull request against the release branch matching the minor release you want to fix.
  • For new features, or fixes that introduce new elements to the public API (such as new public methods or properties), issue the pull request against the default branch.

https://github.com/laminas/.github/blob/a1debce9a0842e3000ae9e01fbc60e32db62901d/CONTRIBUTING.md#what-branch-to-issue-the-pull-request-against

@reinfi
Copy link
Contributor Author

reinfi commented Dec 14, 2022

Test case makes sense, now we need to make CI green :D

Also: 100% sure this makes PHPUnit fail when your patch is not applied?

Undefined array key "file" /Users/maddin/sites/laminas-developer-tools/src/EventLogging/EventContextProvider.php:124

Happens if I remove my patch.

Target branch need to be changed to 2.7.x or 2.8.x and rebase.

A bugfix goes to the current release branch (2.7.x):

What branch to issue the pull request against?

Which branch should you issue a pull request against?

  • For fixes against the stable release, issue the pull request against the release branch matching the minor release you want to fix.
  • For new features, or fixes that introduce new elements to the public API (such as new public methods or properties), issue the pull request against the default branch.

https://github.com/laminas/.github/blob/a1debce9a0842e3000ae9e01fbc60e32db62901d/CONTRIBUTING.md#what-branch-to-issue-the-pull-request-against

Will try to solve this as best as possible :-D

@reinfi
Copy link
Contributor Author

reinfi commented Dec 14, 2022

If I try to fix psalm and add use function call_user_func; then the test case fails again because there is no more a inline call for whatever reason.

I think I'll just close this cause creating a test case for it is quite annoying 🙈

@reinfi reinfi changed the base branch from 2.4.x to 2.8.x December 14, 2022 21:00
@reinfi
Copy link
Contributor Author

reinfi commented Dec 14, 2022

Okay I think I'll not be able to solve the psalm errors without breaking the test case again. I'll get some sleep and may be next week I have a solution for the testcase 👼

@Ocramius
Copy link
Member

Hmm, something went wrong with the rebase here - I can take over, if it's not clear :)

@reinfi
Copy link
Contributor Author

reinfi commented Dec 15, 2022

was too late yesterday 🙈 Would be happy if you help cause I got somewhere stuck with the rebase, tought it was right but it seems not.

@Ocramius Ocramius force-pushed the fix-access-to-unknown-index branch from a011f3f to 9ec53cb Compare December 15, 2022 12:03
@Ocramius Ocramius self-assigned this Dec 15, 2022
@Ocramius Ocramius changed the base branch from 2.8.x to 2.7.x December 15, 2022 12:19
@Ocramius
Copy link
Member

Currently checking this locally: interestingly, if use function call_user_func; is used, then PHP optimizes the call away, and the test no longer works as expected :D

@Ocramius
Copy link
Member

Rewriting some of the test to use array_map() fixes that though 👍

`call_user_func()` is automatically optimized away by PHP-SRC starting from
early PHP 7 releases, and therefore cannot be used to generate artificial
stack frames that are detected in PHP-SRC itself.

With this change, we use `array_map()` instead, which (for now) is not optimized
away (yet).
@Ocramius Ocramius force-pushed the fix-access-to-unknown-index branch from 9ec53cb to 229ca7a Compare December 15, 2022 14:07
@Ocramius Ocramius force-pushed the fix-access-to-unknown-index branch from 229ca7a to 7c1f50d Compare December 15, 2022 14:09
@Ocramius
Copy link
Member

I rewrote most of the patch, and GIT somehow lostsome ownership of the patch here, sorry :-(

Still, final result is much cleaner, I'd say.

@Ocramius Ocramius changed the title fix access of undefined array index file Fix access of undefined array index file when internally analyzing stack frames Dec 15, 2022
@Ocramius Ocramius merged commit 7f34f50 into laminas:2.7.x Dec 15, 2022
@Ocramius
Copy link
Member

Thanks @reinfi!

@Ocramius Ocramius modified the milestones: 2.8.0, 2.7.1 Dec 15, 2022
@froschdesign
Copy link
Member

@reinfi
Thank you for your time and this contribution! And also for your library reinfi/zf-dependency-injection 👍🏻

@reinfi
Copy link
Contributor Author

reinfi commented Dec 15, 2022

thank you guys <3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants