www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - SafeD doesn't prevent me from dereferencing a null reference

reply Yutori <yutori fedora.email> writes:
```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
parent reply Tejas <notrealemail gmail.com> writes:
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
parent reply Yutori <yutori fedora.email> writes:
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
next sibling parent Walter Bright <newshound2 digitalmars.com> writes:
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
prev sibling parent reply Johan <j j.nl> writes:
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
Aug 18 2022
parent reply IGotD- <nise nise.com> writes:
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?
Aug 18 2022
parent reply Adam D Ruppe <destructionator gmail.com> writes:
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
parent IGotD- <nise nise.com> writes:
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