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

[cpp] Null Check Interfaces #11743

Merged
merged 6 commits into from
Aug 2, 2024
Merged

Conversation

Aidan63
Copy link
Contributor

@Aidan63 Aidan63 commented Jul 31, 2024

Adds a null check to the interface object before we start casting, dereferencing, and doing anything which will cause an access violation.

interface IFoo {
    function foo():Void;
}

function main() {
    final o : IFoo = null;

    try {
        o.foo();
    catch (exn:Any) {
        trace(exn);
    }

    o.foo();
}

With the current behaviour null interface access cannot be caught and you will not get stack traces, adding the null check means it can now be caught and you will get stack traces. The used NullReference function is also used by the hxcpp debugger, so null interfaces will no longer crash the debugger.

@rainyt
Copy link

rainyt commented Jul 31, 2024

Very nice, i will try it.

@rainyt
Copy link

rainyt commented Aug 1, 2024

Is there any way for me to directly download the pre constructed Mac executable file? I encountered difficulties constructing haxe from the code source.

@rainyt
Copy link

rainyt commented Aug 1, 2024

OK, I found it, thank you

@rainyt
Copy link

rainyt commented Aug 1, 2024

I cannot directly obtain version 4.3.5, it seems that I can only construct from the source.

@kLabz kLabz added this to the 4.3 Hotfix candidates milestone Aug 1, 2024
kLabz added a commit that referenced this pull request Aug 1, 2024
@Simn
Copy link
Member

Simn commented Aug 1, 2024

Is there a meaningful test we could add for this?

@kLabz
Copy link
Contributor

kLabz commented Aug 1, 2024

Is there a meaningful test we could add for this?

Yeah, snippet in PR description segfaults without this fix, and exception can now be caught

@Aidan63
Copy link
Contributor Author

Aidan63 commented Aug 1, 2024

I've added a test to make sure accessing a null interface does throw.

Also updated the generated code a bit so the null check is guarded behind the HXCPP_CHECK_POINTER define. For reference this is the null checking code when accessing class objects.

      #ifdef HXCPP_CHECK_POINTER
      if (!mPtr) NullReference("Object", true);
      // The handler might have fixed up the null value
      if (!mPtr) NullReference("Object", false);
      #ifdef HXCPP_GC_CHECK_POINTER
         GCCheckPointer(mPtr);
      #endif
      #endif
      return mPtr;

I have not done the double NullReference call as that boolean parameter "fixup" isn't used so I'm guessing its a hold over from a old implementation.

@Simn Simn merged commit 62854a7 into HaxeFoundation:development Aug 2, 2024
50 checks passed
kLabz pushed a commit that referenced this pull request Aug 2, 2024
* Add a IsNull check

* escape quote

* Add new line and remove unneeded dynamic

* Guard interface null checks behind the same defines as class access

* Add null interface exception test

* move target guard to the right place...
@Aidan63 Aidan63 deleted the cpp_null_iface branch August 2, 2024 08:32
@kLabz kLabz removed this from the 4.3 Hotfix milestone Aug 7, 2024
@skial skial mentioned this pull request Aug 7, 2024
1 task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants