www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 11045] New: Pure functions are allowed to read/write global TypeInfo

http://d.puremagic.com/issues/show_bug.cgi?id=11045

           Summary: Pure functions are allowed to read/write global
                    TypeInfo
           Product: D
           Version: unspecified
          Platform: All
        OS/Version: All
            Status: NEW
          Keywords: accepts-invalid
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: maxim maxim-fomin.ru


--- Comment #0 from Maxim Fomin <maxim maxim-fomin.ru> 2013-09-15 13:35:15 PDT
---
Apparently dmd does not protect TypeInfos from reading/mutating from pure
functions.

import core.stdc.string, std.stdio;

pure hijack(T)(T value) if (is(T == class)) 
{
    byte[] init_mem = new byte[T.classinfo.init.length];
    memcpy(init_mem.ptr, cast(byte*)value, T.classinfo.init.length);
    T.classinfo.init = init_mem;
}

class A
{
   int a;
}

pure foo(int val) 
{
   A a = new A;
   a.a++;
   hijack(a);
   return a.a + val;
}

void main()
{
   writeln(0.foo, 0.foo, 0.foo);
}

Prints there different results for the same arguments.

However on the other hand, it makes sense to allow reading some read-only
typeinfo values - it can be treated the same as reading module-level immutable
variable.

Why TypeInfo should be mutable per se is a good question too.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Sep 15 2013