Skip to content

ref and out declarations can be initialized with null references #20518

Closed

Description

@dlangBugzillaToGithub

Walter Bright (@WalterBright) reported this on 2024-09-24T08:49:26Z

Transferred from https://issues.dlang.org/show_bug.cgi?id=24783

Description

@safe:

  void test(ref int r, out int o);

  void foo() {
    int* p;
    test(*p, *p);
  }

compiles without complaint. They should get a null check.

Activity

  1. added theissue type on Apr 25, 2026
  2. rikkimax commented on Apr 27, 2026

    @rikkimax
    Contributor

    It is now supported in ~master, turn on the null check with -check=nullderef=on

  3. Herringway commented on Apr 27, 2026

    @Herringway
    Contributor

    Wait, does that switch actually prevent this from happening? The only actual dereference happening here is on the out parameter, not the ref parameter.

  4. rikkimax commented on Apr 27, 2026

    @rikkimax
    Contributor
    core.exception.NullPointerError@testdfa\startd.d(40): Null pointer dereference
    ----------------
    0x00007FF642A89747
    0x00007FF642A816A4
    0x00007FF642A81054
    0x00007FF642A8100D
    0x00007FF642A8A353
    0x00007FF642A8A1AF
    0x00007FF642A8A2AF
    0x00007FF642A8A1AF
    0x00007FF642A8A0BE
    0x00007FF642A818A9
    0x00007FF642A810A4
    0x00007FF642ABBED0
    0x00007FFE82AC7374 in BaseThreadInitThunk
    0x00007FFE8451CC91 in RtlUserThreadStart
    

    Yes it catches it at runtime.

    And with fast dfa engine enabled -preview=fastdfa:

    testdfa\startd.d(42): Error: Dereference on null variable `p`
        test(*p, *p);
    
  5. Herringway commented on Apr 27, 2026

    @Herringway
    Contributor

    Just to be 100% clear:

    @safe:
    void test(ref int r) {}
    void main() {
      int* p;
      test(*p);
    }

    Results in a thrown Error?

  6. rikkimax commented on Apr 27, 2026

    @rikkimax
    Contributor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions