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 crash when extending taken-over named class #84148

Merged

Conversation

KANAjetzt
Copy link
Contributor

@KANAjetzt KANAjetzt commented Oct 29, 2023

Added error handling in _prepare_compilation() to address cases where the base_type cannot be found, preventing a crash.
Added an additional if statement to find_class() to check the script path.

I expect this to not be the ideal solution, as I stated in my issue comment.
But this PR might be a better place to discuss code changes, and I'm looking forward to any feedback.

Nonetheless, I ran all the GDScript tests and tried this fix in the MRP and two bigger projects with success and no obvious side effects.

Cheers.


Note

This now only contains the crash prevention. Future debugging is needed. #83542 will stay open.

@KANAjetzt KANAjetzt requested a review from a team as a code owner October 29, 2023 16:11
@AThousandShips AThousandShips added bug topic:core crash topic:gdscript cherrypick:4.2 Considered for cherry-picking into a future 4.2.x release and removed topic:core labels Oct 29, 2023
@AThousandShips AThousandShips added this to the 4.3 milestone Oct 29, 2023
@AThousandShips AThousandShips added the cherrypick:4.1 Considered for cherry-picking into a future 4.1.x release label Oct 29, 2023
@adamscott
Copy link
Member

adamscott commented Jan 10, 2024

Thanks for your first PR!

If we merge this PR, I'm wondering if we should update the GDScriptCache when a path is taken over by a new script.

Copy link
Member

@adamscott adamscott left a comment

Choose a reason for hiding this comment

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

Seems fine!

Thank you again for your first PR!

@@ -2647,7 +2647,10 @@ Error GDScriptCompiler::_prepare_compilation(GDScript *p_script, const GDScriptP

GDScriptDataType base_type = _gdtype_from_datatype(p_class->base_type, p_script, false);

ERR_FAIL_COND_V_MSG(base_type.native_type == StringName(), ERR_BUG, vformat(R"(Failed to get base class for "%s")", p_script->path));
Copy link
Member

Choose a reason for hiding this comment

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

Should this be an engine-level error (ERR_BUG), or a user-level error (_set_error + returning the most relevant error code)?

Copy link
Member

Choose a reason for hiding this comment

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

I think it's fine, this should not happen as a result of user error, it's an analyzer/compiler bug.

Comment on lines 1105 to 1156
} else if (path.begins_with(get_root_script()->path)) {
class_names = path.trim_prefix(get_root_script()->path).split("::");
result = get_root_script();
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure, but the condition looks as always true to me. p_qualified_name is not used at all in the branch, this is weird.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for looking at this! I tried to explain what I found while debugging here.

If you can provide more details, I can dive back in.

"The condition looks as always true" - Which condition?
"p_qualified_name is not used" - What exactly do you mean by "not used" and what is weird about it?

Copy link
Member

Choose a reason for hiding this comment

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

Which condition?

path.begins_with(get_root_script()->path). path is the script path, for inner classes it may additionally contain suffixes. get_root_script() returns the topmost script that owns the inner class scripts.

What exactly do you mean by "not used" and what is weird about it?

This method checks p_qualified_name, every other branch has p_qualified_name, but not this one, that's weird. I haven't tested this, but it might break the _owner != nullptr branch.

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 haven't tested this, but it might break the _owner != nullptr branch.

How can I test for this?
Adding some inner classes to my MRP?
I run --test --test-suite="*GDScript*" and everything seems to be fine.
image

Copy link
Member

Choose a reason for hiding this comment

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

Successful tests do not guarantee the absence of regressions, since the tests do not cover all possible scenarios. I added this PR for discussion at the GDScript team meeting or maybe @vnen will review it offline.

Copy link
Member

Choose a reason for hiding this comment

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

It is odd to me as well. This is a function to find a class identified by p_qualified_name, so the branch not checking the argument cannot reliably find the requested class. As @dalexeev said, this will always be true (the path of an inner class will always start with the path of its root script), so the other conditions will never be checked.

Given that, I don't think this is a proper solution.

@KANAjetzt KANAjetzt force-pushed the fix_crash_take_over_path_named_class branch from 87db92f to ddea67e Compare March 18, 2024 08:56
vnen
vnen previously requested changes Apr 24, 2024
Copy link
Member

@vnen vnen left a comment

Choose a reason for hiding this comment

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

See comment

@pirey0
Copy link
Contributor

pirey0 commented May 5, 2024

@vnen i've been talking with Kana and further analyzed the root cause for this issue.
(see #83542 (comment) <- could use your input here)
This problem will not be fixed by changes to GDScript::find_class as suggested originally.

Should we close this PR, and address the root problem in a new PR?
(Or use this PR to merge only the suggested ERR_FAIL_COND_V_MSG?)
I feel like addressing the root problem here would only cause further confusion since the issue will go in a completely different direction

@vnen
Copy link
Member

vnen commented May 6, 2024

@pirey0 if the ERR_FAIL is enough to fix the crash, then we can merge it here. But yes, if you want to propose a new change it might be better to close this and open a new PR.

Added error handling in `_prepare_compilation()` to address cases where the `base_type` cannot be found, preventing a crash.
@KANAjetzt KANAjetzt force-pushed the fix_crash_take_over_path_named_class branch from fd6fcc6 to f4192aa Compare May 7, 2024 06:07
@KANAjetzt
Copy link
Contributor Author

@vnen @pirey0 I changed it to only include the ERR_FAIL. That prevents the crash and logs these two errors:
image

I hope we can find a proper fix for this 👍

@akien-mga
Copy link
Member

This seems good as crash prevention, but the bug still exsits. Do we have an open bug report with a MRP keeping track of it?

@akien-mga akien-mga dismissed vnen’s stale review May 7, 2024 09:15

Suggested changes done.

@pirey0
Copy link
Contributor

pirey0 commented May 7, 2024

This seems good as crash prevention, but the bug still exsits. Do we have an open bug report with a MRP keeping track of it?

#83542 (if this is not what you meant, let me know :) )

@akien-mga akien-mga merged commit 0404e3a into godotengine:master May 7, 2024
16 checks passed
@akien-mga
Copy link
Member

Thanks! And congrats for your first merged Godot contribution 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug cherrypick:4.1 Considered for cherry-picking into a future 4.1.x release cherrypick:4.2 Considered for cherry-picking into a future 4.2.x release crash topic:gdscript
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants