Bug 11045 – Pure functions are allowed to read/write global TypeInfo
Status
RESOLVED
Resolution
MOVED
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-09-15T13:35:15Z
Last change time
2023-03-27T12:22:13Z
Keywords
accepts-invalid
Assigned to
No Owner
Creator
Maxim Fomin
Comments
Comment #0 by maxim — 2013-09-15T13:35:15Z
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.
Comment #1 by razvan.nitu1305 — 2023-03-27T12:22:13Z
I cannot reproduce this because the examples has some errors in it.
If you can provide a compilable updated example, then please reopen this.