shithub: mc

Download patch

ref: c7606d65d33f37e853755930e5559c7c4aa4a554
parent: a61e4d3a951e7f1833de4ac8a9b16aa15439a35a
author: Ori Bernstein <[email protected]>
date: Thu Aug 2 14:25:56 EDT 2012

Don't put in a bogus offset when casting slices.

    How did this EVER work?

--- a/6/simp.c
+++ b/6/simp.c
@@ -626,11 +626,13 @@
 static Node *slicebase(Simp *s, Node *n, Node *off)
 {
     Node *t, *u, *v;
+    Type *ty;
     int sz;
 
     t = rval(s, n, NULL);
     u = NULL;
-    switch (exprtype(n)->type) {
+    ty = tybase(exprtype(n));
+    switch (ty->type) {
         case Typtr:     u = n; break;
         case Tyarray:   u = addr(t, base(exprtype(n))); break;
         case Tyslice:   u = load(addr(t, mktyptr(n->line, base(exprtype(n))))); break;
@@ -637,10 +639,14 @@
         default: die("Unslicable type %s", tystr(n->expr.type));
     }
     /* safe: all types we allow here have a sub[0] that we want to grab */
-    off = ptrsized(s, off);
-    sz = tysize(n->expr.type->sub[0]);
-    v = mul(off, disp(n->line, sz));
-    return add(u, v);
+    if (off) {
+      off = ptrsized(s, off);
+      sz = tysize(n->expr.type->sub[0]);
+      v = mul(off, disp(n->line, sz));
+      return add(u, v);
+    } else {
+      return u;
+    }
 }
 
 static Node *slicelen(Simp *s, Node *sl)
@@ -713,7 +719,6 @@
 
 static Node *simpcast(Simp *s, Node *val, Type *to)
 {
-    Node *sz;
     Node *r;
     Type *t;
     int issigned;
@@ -733,9 +738,7 @@
                     if (t->type == Typtr)
                         fatal(val->line, "Bad cast from %s to %s",
                               tystr(exprtype(val)), tystr(to));
-                    sz = mkintlit(val->line, Ptrsz);
-                    sz->expr.type = exprtype(val);
-                    r = slicebase(s, val, sz);
+                    r = slicebase(s, val, NULL);
                     break;
                 case Tyint8: case Tyint16: case Tyint32: case Tyint64:
                 case Tyint: case Tylong: