On Thursday, 17 October 2013 at 23:18:21 UTC, DDD wrote:


I tried this code and the compiler allowed it (runtime I get object.Error: Access Violation). What am I doing wrong?

Thanks I didn't notice

@safe
import std.stdio;
class A {
        int x  = 1;
}
@safe void main() {
        A a;
        a.x=9;
}

Yes, compiler allows it. But this is basic case.

Here is D' Bugs Hall Of Fame (collection of memory errors, type system breakages and other cases to shoot your foot provided by bugzilla issues, me and other people) :

// --- Case 1. Breaking all of type system through delegates --- //

extern(C) @system int printf(const char*, ...);

auto frame1() pure nothrow @safe
{
    immutable void delegate() pure nothrow @safe x;
    auto tmp = { return x; } ;
    return tmp;
}

auto frame2(T)(T t) pure nothrow @safe
{
    void delegate() @system x;
    x.funcptr = t;
    return { return x; } ;
}

void bar() { printf("pure nothrow @safe loophole\n"); } @system

void main() @safe pure nothrow
{
    //bar(); Error: ...
    auto fm1 = frame1();
    auto fm2 = frame2(&bar);
    fm1.ptr = fm2.ptr;
    fm1()();

    fm2 = frame2({ printf("pure nothrow @safe loophole\n"); });
    fm1.ptr = fm2.ptr;
    fm1()();
}


// ---- Case 2. Breaking immutability ---- //

import std.stdio;

class A
{
    int[] c = [3,3];
}

void main()
{
    int[] a = [2,2];
    int[] b = [2,2];
    a[0] = 33;
    assert(b[0] == 2);   // success

    A ca = new A; // assume that one of them is immutable
    A cb = new A;
    ca.c[0] = 44;
    assert(cb.c[0] == 3); // failure: value is 44
}

// ---- Case 3. Breaking type system via delegates ---- //

class A
{
   int i;
   void foo() { ++i; }
}

void main()
{
   immutable a = new immutable A;
   //a.foo();
   (&a.foo)();
}

// --- Case 4. Mutating immutable -- //
immutable int i;

void f(ref int n = i)
{
    ++n; // hello to those who thinks immutable never changes
}

import std.stdio;

void main()
{
   f();
   writeln(i);
}

// -- Case 5. Memory corruption via lazy --- //

extern(C) int printf(const char*,...) @safe;

alias int T;

auto foo(lazy T i) @safe
{
   return { return i; } ;
}

auto bar() @safe
{
   T i = 4;
   return foo(i);
}

void baz() @safe
{
   double[2] i = 3.14;
}

void main() @safe
{
   auto x = bar();
   baz();
   printf("%d\n", x());
}

Reply via email to