D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 3769 - Regression: Segfault(constfold.c) array literals and case statements
Summary: Regression: Segfault(constfold.c) array literals and case statements
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D1 (retired)
Hardware: Other Windows
: P2 critical
Assignee: No Owner
URL:
Keywords: ice-on-valid-code, patch
Depends on:
Blocks:
 
Reported: 2010-02-04 05:36 UTC by Don
Modified: 2014-02-16 15:26 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Don 2010-02-04 05:36:47 UTC
This code was correctly rejected in DMD1.010, but segfaults in 1.020 and later, including 1.056. I'm intentionally not marking it as regression since it is ancient and cannot exist in old code. Although this test case is D1 only, there are related bugs in D2.
-------
const int[ 19 ] buggy_3763 = [ 2 ];

void bugzilla3763()
{
  switch(2) {
    case buggy_3763[1]:
  }
}
Comment 1 Don 2010-02-04 13:41:49 UTC
This is a terrible one. It only happens when DMD is compiled with the optimizer on, so it doesn't happen in the debug version of DMD.

It's crashing inside constfold.c Index(), around line 1206; it's called from IndexExp::optimize() The code is:

	else if (e1->op == TOKarrayliteral && !e1->checkSideEffect(2))
	{   ArrayLiteralExp *ale = (ArrayLiteralExp *)e1;
	    e = (Expression *)ale->elements->data[i];
	    e->type = type;
	}
It crashes on the first mention of ale->elements. I wonder if this could be a DMC bug?
Comment 2 Don 2010-02-05 00:12:17 UTC
The root cause is in init.c, Expression *ArrayInitializer::toExpression(), around line 439.
The Expressions array which holds all of the members of the array literal, does not get initialized. But the later part of this function assumes that all of the entries are null.
Here's a patch which fixes it:

    elements = new Expressions();
    elements->setDim(edim);
+   elements->zero();
    for (size_t i = 0, j = 0; i < value.dim; i++, j++)
    {
	if (index.data[i])
	    j = ((Expression *)index.data[i])->toInteger();

BUT... this kind of bug is ridiculous, IMHO. I think in root/array.c, 
void Array::reserve(unsigned nentries) should be initializing the data it gets from realloc.
I bet this isn't the only place in the compiler where this landmine is waiting.
Absolutely horrid.
Comment 3 Don 2010-02-05 02:17:54 UTC
Better test case, works for both D1 and D2:
---
const char[][ 89 ] ENUM_NAME = [ 1:"N0" ];

void bug3769()
{	
	switch(`Hi`.dup) {
		case ENUM_NAME[1]:
	}
}
----
Comment 4 Walter Bright 2010-02-05 20:37:36 UTC
Changeset 372
Comment 5 Kosmonaut 2010-02-05 23:37:43 UTC
(In reply to comment #4)
> Changeset 372

http://www.dsource.org/projects/dmd/changeset/372
Comment 6 Walter Bright 2010-03-08 22:23:07 UTC
Fixed dmd 1.057 and 2.041