Thread overview
SafeD doesn't prevent me from dereferencing a null reference
Aug 18, 2022
Yutori
Aug 18, 2022
Tejas
Aug 18, 2022
Yutori
Aug 18, 2022
Walter Bright
Aug 18, 2022
Johan
Aug 18, 2022
IGotD-
Aug 18, 2022
Adam D Ruppe
Aug 18, 2022
IGotD-
August 18, 2022
void main(immutable string[] argv) @safe @live {
        class Test {
                string a;
        }
        auto testInstance = new Test;
        testInstance.a = "Test String";
        import std.stdio;
        testInstance.a.writeln;
        testInstance = null;
        testInstance.a.writeln;
}
/* Terminal output:
        Test String
        Segmentation fault
*/

Of course, this code must spit out a segmentation fault, as dereferencing null is illegal. However, the problem is, that it lets me compile the program in @safe.
I'm not entirely sure about what testInstance is defined as, but I assume it's a reference. I don't think dereferencing null should be allowed in SafeD. Or is this a design decision of D?

August 18, 2022

On Thursday, 18 August 2022 at 14:23:40 UTC, Yutori wrote:

>
void main(immutable string[] argv) @safe @live {
        class Test {
                string a;
        }
        auto testInstance = new Test;
        testInstance.a = "Test String";
        import std.stdio;
        testInstance.a.writeln;
        testInstance = null;
        testInstance.a.writeln;
}
/* Terminal output:
        Test String
        Segmentation fault
*/

Of course, this code must spit out a segmentation fault, as dereferencing null is illegal. However, the problem is, that it lets me compile the program in @safe.
I'm not entirely sure about what testInstance is defined as, but I assume it's a reference. I don't think dereferencing null should be allowed in SafeD. Or is this a design decision of D?

I believe this is a design decision, since you're not invoking UB in @safe code. Dereferencing a null pointer is assumed to crash your program, so it's allowed to be done in @safe code as well, since the semantics are deterministic.

August 18, 2022

On Thursday, 18 August 2022 at 15:27:17 UTC, Tejas wrote:

>

I believe this is a design decision, since you're not invoking UB in @safe code. Dereferencing a null pointer is assumed to crash your program, so it's allowed to be done in @safe code as well, since the semantics are deterministic.

Oh, I understand it now. So I guess that @safe in D is supposed to prevent only UB, not all memory errors that could possibly happen, and since dereferencing null will always result in a page fault, it isn't UB.

August 18, 2022
On 8/18/2022 8:37 AM, Yutori wrote:
> Oh, I understand it now. So I guess that @safe in D is supposed to prevent only UB, not all memory errors that could possibly happen, and since dereferencing null will always result in a page fault, it isn't UB.

That's right.
August 18, 2022

On Thursday, 18 August 2022 at 15:37:54 UTC, Yutori wrote:

>

and since dereferencing null will always result in a page fault

This is not true, but the D community treats it as true.

-Johan

August 18, 2022

On Thursday, 18 August 2022 at 21:04:56 UTC, Johan wrote:

>

On Thursday, 18 August 2022 at 15:37:54 UTC, Yutori wrote:

>

and since dereferencing null will always result in a page fault

This is not true, but the D community treats it as true.

-Johan

Please explain. Is it because of null + offset this might lead to corrupted data but is it even allowed in safe mode?

August 18, 2022

On Thursday, 18 August 2022 at 21:12:35 UTC, IGotD- wrote:

>

Please explain.

There's several platforms that don't even have page faults.

Webassembly, for example, uses the address 0 as a normal address just like any others.

August 18, 2022

On Thursday, 18 August 2022 at 21:17:30 UTC, Adam D Ruppe wrote:

>

There's several platforms that don't even have page faults.

Webassembly, for example, uses the address 0 as a normal address just like any others.

Are you trying to make my point that raw pointers should be banned from safeD and that we need opaque managed pointers and D3?