digitalmars.D - SafeD doesn't prevent me from dereferencing a null reference
- Yutori (23/23) Aug 18 2022 ```d
- Tejas (5/28) Aug 18 2022 I believe this is a design decision, since you're not invoking UB
- Yutori (5/9) Aug 18 2022 Oh, I understand it now. So I guess that @safe in D is supposed
- Walter Bright (2/5) Aug 18 2022 That's right.
- Johan (3/4) Aug 18 2022 This is not true, but the D community treats it as true.
- IGotD- (3/7) Aug 18 2022 Please explain. Is it because of null + offset this might lead to
- Adam D Ruppe (4/5) Aug 18 2022 There's several platforms that don't even have page faults.
- IGotD- (3/6) Aug 18 2022 Are you trying to make my point that raw pointers should be
```d 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?
Aug 18 2022
On Thursday, 18 August 2022 at 14:23:40 UTC, Yutori wrote:```d 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.
Aug 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.
Aug 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.
Aug 18 2022
On Thursday, 18 August 2022 at 15:37:54 UTC, Yutori wrote:and since dereferencing null will always result in a page faultThis is not true, but the D community treats it as true. -Johan
Aug 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:Please explain. Is it because of null + offset this might lead to corrupted data but is it even allowed in safe mode?and since dereferencing null will always result in a page faultThis is not true, but the D community treats it as true. -Johan
Aug 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.
Aug 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?
Aug 18 2022