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

Make cast_to a static member of Object. #10581

Merged
merged 2 commits into from
Aug 25, 2017
Merged

Conversation

hpvb
Copy link
Member

@hpvb hpvb commented Aug 23, 2017

Currently we rely on some undefined behavior when Object->cast_to() gets
called with a Null pointer. This used to work fine with GCC < 6 but
newer versions of GCC remove all codepaths in which the this pointer is
Null. However, the non-static cast_to() was supposed to be null safe.

This patch makes cast_to() Null safe and removes the now redundant Null
checks where they existed.

The following demos have been imported and run without issues on Fedora 26/GCC 7.1.1:

  • 2d/lights_and_shadows
  • 2d/physics_platformer
  • 2d/platformer
  • 3d/kinematic_character
  • 3d/material_testers
  • 3d/platformer

This validates that it at least addresses #10517 and #10515 also. These were already fixed by this commit removes those null checks again and the crashes are still gone. This suggests to me that this will likely fix other random crashes on GCC6+ also.

@Zireael07
Copy link
Contributor

Man I think you messed up git somehow, what are all those commits doing there?

@hpvb hpvb closed this Aug 23, 2017
@hpvb hpvb reopened this Aug 23, 2017
@hpvb
Copy link
Member Author

hpvb commented Aug 23, 2017

@Zireael07 yeah, I accidentally rebased on my fork rather than master, sorry :) fixed now

@akien-mga akien-mga added this to the 3.0 milestone Aug 23, 2017
@RandomShaper
Copy link
Member

I've not checked the diff thoroughly, but your idea is nice.

But then I wonder if commits adding null checks just because of the UB issue should be reverted, as no longer meaningful.

@akien-mga
Copy link
Member

This commit already removes the redundant null checks, e.g. 0f4d1ad#diff-6435cbec06d56101c9bcc0061478fae7L276

@RandomShaper
Copy link
Member

Terrific. :)

@Zylann
Copy link
Contributor

Zylann commented Aug 23, 2017

Just a note, I thought dereferencing a null pointer was fine as long as this isn't accessed? I last saw this trick in the Qt codebase

@hpvb hpvb force-pushed the fix-gcc6+ branch 2 times, most recently from 83126c7 to 40421e8 Compare August 23, 2017 16:46
@hpvb
Copy link
Member Author

hpvb commented Aug 23, 2017

@Zylann I think we're hitting the problem explained in this blogpost: https://www.viva64.com/en/b/0226/

@hpvb
Copy link
Member Author

hpvb commented Aug 23, 2017

@Zylann This post copy/pastes some part of the relevant standard: https://www.viva64.com/en/w/V704/

@hpvb hpvb force-pushed the fix-gcc6+ branch 2 times, most recently from 94ced6b to 389182b Compare August 23, 2017 19:46
@hpvb hpvb force-pushed the fix-gcc6+ branch 3 times, most recently from e9443c2 to 7e4f5da Compare August 24, 2017 21:06
@hpvb hpvb changed the title [WIP] Make cast_to a static member of Object. Make cast_to a static member of Object. Aug 24, 2017
hpvb added 2 commits August 24, 2017 23:08
This is to prepare to replace all instances of the member version of
cast_to().
Currently we rely on some undefined behavior when Object->cast_to() gets
called with a Null pointer. This used to work fine with GCC < 6 but
newer versions of GCC remove all codepaths in which the this pointer is
Null. However, the non-static cast_to() was supposed to be null safe.

This patch makes cast_to() Null safe and removes the now redundant Null
checks where they existed.

It is explained in this article: https://www.viva64.com/en/b/0226/
@akien-mga akien-mga merged commit 490aef9 into godotengine:master Aug 25, 2017
@akien-mga
Copy link
Member

I can't wait not to notice the 50 potential bugs this will have magically fixed :P

hpvb added a commit to hpvb/godot that referenced this pull request Aug 26, 2017
These Null checks were removed in godotengine#10581 but actually changed the
logic of the functions in this case.

This fixes godotengine#10654
@hpvb hpvb deleted the fix-gcc6+ branch August 31, 2017 08:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants