January 21, 2011 [Issue 1914] Array initialisation from const array yeilds memory trample | ||||
---|---|---|---|---|
| ||||
Posted in reply to d-bugmail | http://d.puremagic.com/issues/show_bug.cgi?id=1914 --- Comment #9 from Don <clugdbug@yahoo.com.au> 2011-01-21 13:41:50 PST --- The patch (which has been included in D2, but not D1) was not correct -- it didn't fix the case of an all-zero initializer, and also failed for multi-dimensional array literals. This patch, in todt.c, TypeSArray::toDtElem(), line 985, fixes this bug and also fixes bug 3198. The first lines work by making sure we don't have an array of arrays. 3198 is fixed by removing the special case for structs in line 997: - if (tbn->ty == Tstruct) - tnext->toDt(pdt); - else The main part of the patch is that we need to divide by the literal size. And finally, undo the bad original patch. PATCH: todt.c line 985 pdt = &((*pdt)->DTnext); Type *tnext = next; Type *tbn = tnext->toBasetype(); - while (tbn->ty == Tsarray) + // Count all dimensions, but stop if e is an array literal of + // arrays of the same type + while (tbn->ty == Tsarray && (!e || tbn != e->type->nextOf())) { TypeSArray *tsa = (TypeSArray *)tbn; len *= tsa->dim->toInteger(); tnext = tbn->nextOf(); tbn = tnext->toBasetype(); } if (!e) // if not already supplied e = tnext->defaultInit(); // use default initializer - if (tbn->ty == Tstruct) - tnext->toDt(pdt); - else - e->toDt(pdt); + e->toDt(pdt); dt_optimize(*pdt); + if (e->op == TOKstring) + len /= ((StringExp *)e)->len; + if (e->op == TOKarrayliteral) + len /= ((ArrayLiteralExp *)e)->elements->dim; if ((*pdt)->dt == DT_azeros && !(*pdt)->DTnext) { (*pdt)->DTazeros *= len; pdt = &((*pdt)->DTnext); } else if ((*pdt)->dt == DT_1byte && (*pdt)->DTonebyte == 0 && !(*pdt)->DTnext) { (*pdt)->dt = DT_azeros; (*pdt)->DTazeros = len; pdt = &((*pdt)->DTnext); } - else if (e->op != TOKstring && e->op != TOKarrayliteral) + else { for (i = 1; i < len; i++) { ====================================== TEST CASES: struct Bug1914a { const char[10] i = [1,0,0,0,0,0,0,0,0,0]; char[10] x = i; int y=5; } struct Bug1914b { const char[10] i = [0,0,0,0,0,0,0,0,0,0]; char[10] x = i; int y=5; } struct Bug1914c { const char[2] i = ['a', 'b']; const char[2][3] j = [['x', 'y'], ['p', 'q'], ['r', 's']]; const char[2][3] k = ["cd", "ef", "gh"]; const char[2][3] l = [['x', 'y'], ['p'], ['h', 'k']]; char[2][3] x = i; int y = 5; char[2][3] z = j; char[2][3] w = k; int v=27; char[2][3] u = l; int t = 718; } struct T3198 { int g = 1; } class Foo3198 { int[5] x = 6; T3198[5] y = T3198(4); } void main() { Bug1914a a; assert(a.y==5, "bug 1914, non-zero init"); Bug1914b b; assert(b.y==5, "bug 1914, zero init"); Bug1914c c; assert(c.y==5, "bug 1914, multilevel init"); assert(c.v==27, "bug 1914, multilevel init2"); assert(c.x[2][1]=='b'); assert(c.t==718, "bug 1914, multi3"); assert(c.u[1][0]=='p'); assert(c.u[1][1]==char.init); auto f = new Foo3198(); assert(f.x[0]==6); assert(f.y[0].g==4, "bug 3198"); } -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
February 06, 2011 [Issue 1914] Array initialisation from const array yeilds memory trample | ||||
---|---|---|---|---|
| ||||
Posted in reply to d-bugmail | http://d.puremagic.com/issues/show_bug.cgi?id=1914 Don <clugdbug@yahoo.com.au> changed: What |Removed |Added ---------------------------------------------------------------------------- Status|NEW |RESOLVED Resolution| |FIXED --- Comment #10 from Don <clugdbug@yahoo.com.au> 2011-02-06 13:47:54 PST --- Fixed https://github.com/D-Programming-Language/dmd/commit/0c7bd71609735814f5bb410f903e454643ac260f -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
Copyright © 1999-2021 by the D Language Foundation