Thread overview |
---|
February 03, 2014 hack on @safe functions | ||||
---|---|---|---|---|
| ||||
struct T { ubyte[0x12FDB5/*address of local x in main + 1*/] x; } @safe void test() { T* t = null; t.x[$-1] = 99; } void main() { ubyte x = 0; writeln("\n", &x, ' ', x); test(); writeln("\n", x); } |
February 03, 2014 Re: hack on @safe functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Pavel | On Monday, 3 February 2014 at 12:41:30 UTC, Pavel wrote:
> struct T {
> ubyte[0x12FDB5/*address of local x in main + 1*/] x;
> }
>
> @safe void test() {
> T* t = null;
> t.x[$-1] = 99;
> }
>
> void main() {
> ubyte x = 0;
> writeln("\n", &x, ' ', x);
> test();
> writeln("\n", x);
> }
This has been recently mentioned if thread about null dereference "safety" :)
|
February 04, 2014 Re: hack on @safe functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Pavel | Some suggesting to compiler checking for that case. If talking about Linux OS it reserves first page (4kb on 32bit cpu, 8kb on 64) for null fault case (try to dereference pointer in that memory addresses will cause segmentation fault or smth like this). So compiler can check (at compile time) each structure field access for dereference with ofset > OS page size add code for check pointer of structure for null. Or when trying to get pointer of field that is in the range of page size, but field type size + ofset of this field > page size. In this solution there will be very little overhead cause it is very rare case that structures have size > page size. Sorry for my bad english. :) |
February 04, 2014 Re: hack on @safe functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Pavel | On Tuesday, 4 February 2014 at 06:08:59 UTC, Pavel wrote:
> Some suggesting to compiler checking for that case.
>
> If talking about Linux OS it reserves first page (4kb on 32bit cpu, 8kb on 64) for null fault case (try to dereference pointer in that memory addresses will cause segmentation fault or smth like this).
>
> So compiler can check (at compile time) each structure field access for dereference with ofset > OS page size add code for check pointer of structure for null. Or when trying to get pointer of field that is in the range of page size, but field type size + ofset of this field > page size.
>
> In this solution there will be very little overhead cause it is very rare case that structures have size > page size.
>
> Sorry for my bad english. :)
In that case there will be no need for dissallowing null pointers in safe code.
|
Copyright © 1999-2021 by the D Language Foundation