Safe method wont check dangling pointer?
Steven Schveighoffer
schveiguy at yahoo.com
Mon Apr 14 16:44:48 PDT 2014
On Mon, 14 Apr 2014 19:28:06 -0400, lzzll <ownrepos at gmail.com> wrote:
> Looks like dangling point is not checked even in method mark as safe.
> Example:
> ---
> import std.stdio;
>
> class A {
> int value;
> void set_value(int value) @safe {
> this.value = value;
> }
> }
>
> void test_safe(A a) @safe {
> a.set_value(1);
> }
>
> int main(string[] args) {
> A a = new A();
> test_safe(a);
> test_safe(null);
> test_safe(*(&a+100));
>
> writeln("done.");
> return 0;
> }
> ---
> test_safe(null);
> and
> test_safe(*(&a+100));
> will cause segmentation fault.
Safe cannot verify its inputs. main() is not marked as safe, therefore it
will not help.
But even so, dereferencing null is @safe, since it does not corrupt
memory. Your *(&a + 100) will definitely not compile if main is marked
@safe.
-Steve
More information about the Digitalmars-d
mailing list