2990 – TypeInfo.init() returns invalid array

D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 2990 - TypeInfo.init() returns invalid array
Summary: TypeInfo.init() returns invalid array
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D1 (retired)
Hardware: x86 Linux
: P2 normal
Assignee: No Owner
URL:
Keywords: wrong-code
Depends on:
Blocks:
Reported: 2009年05月16日 06:16 UTC by nfxjfg
Modified: 2014年04月18日 09:12 UTC (History)
4 users (show)

See Also:


Attachments
Add an attachment (proposed patch, testcase, etc.)

Note You need to log in before you can comment on or make changes to this issue.
Description nfxjfg 2009年05月16日 06:16:43 UTC
Sometimes, the array TypeInfo.init() returns has a length, but ptr is null. Accessing an element of such an array causes segfaults. This can't be right. TypeInfo.init() should just return an empty array.
Example follows. This outputs "ptr=0000 length=4".
import std.stdio;
struct X {
 int y;
}
void main() {
 TypeInfo ti = typeid(X);
 writefln("ptr=%s length=%s", ti.init.ptr, ti.init.length);
}
Comment 1 Steven Schveighoffer 2011年01月07日 06:46:51 UTC
This is really a documentation issue. The init() is correct, as I discovered when fixing a recent bug.
Essentially, when the ptr is null, but the length is non-zero, the meaning is that the init value for that type is init().length bytes of zero. This cuts down on having to store an array of zeros in the binary.
The documentation for init() says:
"Return default initializer, null if default initialize to 0"
Which seems to indicate init() should return null if default initializer should be zero. But a null array has length == 0.
I think the right solution to this is to change the documentation:
"Return default initializer. If the type should be initialized to all zeros, an array with a null ptr and a length equal to the type size will be returned"
Sound good?
Comment 2 Walter Bright 2011年04月05日 11:31:07 UTC
This is deliberate, see the typinf.c code:
 // void[] init;
 dtsize_t(pdt, sd->structsize); // init.length
 if (sd->zeroInit)
 dtsize_t(pdt, 0); // NULL for 0 initialization
 else
 dtxoff(pdt, sd->toInitializer(), 0, TYnptr); // init.ptr
I'll fix the documentation.


AltStyle によって変換されたページ (->オリジナル) /