D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 4089 - crash when creating JSON output for incomplete struct
Summary: crash when creating JSON output for incomplete struct
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: Other Windows
: P2 normal
Assignee: No Owner
URL:
Keywords: ice-on-valid-code, patch
Depends on:
Blocks:
 
Reported: 2010-04-14 00:01 UTC by Rainer Schuetze
Modified: 2015-06-09 05:14 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Rainer Schuetze 2010-04-14 00:01:04 UTC
This one-line code:

/// test.d
struct X;

causes a crash when executing

dmd -c -X test.d


Here's a patch

Index: json.c
===================================================================
--- json.c	(revision 432)
+++ json.c	(working copy)
@@ -307,16 +307,19 @@
         }
     }
 
-    JsonString(buf, Pmembers);
-    buf->writestring(" : [\n");
-    size_t offset = buf->offset;
-    for (int i = 0; i < members->dim; i++)
-    {   Dsymbol *s = (Dsymbol *)members->data[i];
-        if (offset != buf->offset)
-        {   buf->writestring(",\n");
-            offset = buf->offset;
+    if(members)
+    {
+        JsonString(buf, Pmembers);
+        buf->writestring(" : [\n");
+        size_t offset = buf->offset;
+        for (int i = 0; i < members->dim; i++)
+        {   Dsymbol *s = (Dsymbol *)members->data[i];
+            if (offset != buf->offset)
+            {   buf->writestring(",\n");
+                offset = buf->offset;
+            }
+            s->toJsonBuffer(buf);
         }
-        s->toJsonBuffer(buf);
     }
     JsonRemoveComma(buf);
     buf->writestring("]\n");
Comment 1 Rainer Schuetze 2010-04-14 00:44:11 UTC
sorry, the patch produced wrong brackets. Here's a better version:

Index: json.c
===================================================================
--- json.c	(revision 432)
+++ json.c	(working copy)
@@ -307,19 +307,23 @@
         }
     }
 
-    JsonString(buf, Pmembers);
-    buf->writestring(" : [\n");
-    size_t offset = buf->offset;
-    for (int i = 0; i < members->dim; i++)
-    {   Dsymbol *s = (Dsymbol *)members->data[i];
-        if (offset != buf->offset)
-        {   buf->writestring(",\n");
-            offset = buf->offset;
+    if(members)
+    {
+        JsonString(buf, Pmembers);
+        buf->writestring(" : [\n");
+        size_t offset = buf->offset;
+        for (int i = 0; i < members->dim; i++)
+        {   Dsymbol *s = (Dsymbol *)members->data[i];
+            if (offset != buf->offset)
+            {   buf->writestring(",\n");
+                offset = buf->offset;
+            }
+            s->toJsonBuffer(buf);
         }
-        s->toJsonBuffer(buf);
+        JsonRemoveComma(buf);
+        buf->writestring("]\n");
     }
     JsonRemoveComma(buf);
-    buf->writestring("]\n");
 
     buf->writestring("}\n");
 }
Comment 2 Rainer Schuetze 2010-04-21 11:09:05 UTC
An almost identical patch is also needed for incomplete EnumDeclarations:

Index: json.c
===================================================================
--- json.c	(revision 433)
+++ json.c	(working copy)
@@ -386,19 +386,23 @@
     if (memtype)
         JsonProperty(buf, "base", memtype->toChars());
 
-    JsonString(buf, Pmembers);
-    buf->writestring(" : [\n");
-    size_t offset = buf->offset;
-    for (int i = 0; i < members->dim; i++)
-    {   Dsymbol *s = (Dsymbol *)members->data[i];
-        if (offset != buf->offset)
-        {   buf->writestring(",\n");
-            offset = buf->offset;
+    if(members)
+    {
+        JsonString(buf, Pmembers);
+        buf->writestring(" : [\n");
+        size_t offset = buf->offset;
+        for (int i = 0; i < members->dim; i++)
+        {   Dsymbol *s = (Dsymbol *)members->data[i];
+            if (offset != buf->offset)
+            {   buf->writestring(",\n");
+                offset = buf->offset;
+            }
+            s->toJsonBuffer(buf);
         }
-        s->toJsonBuffer(buf);
+        JsonRemoveComma(buf);
+        buf->writestring("]\n");
     }
     JsonRemoveComma(buf);
-    buf->writestring("]\n");
 
     buf->writestring("}\n");
 }
Comment 3 Don 2010-05-05 19:10:18 UTC
Fixed DMD2.044