1
0
mirror of https://github.com/golang/go synced 2024-11-12 05:30:21 -07:00

fix unsafe.Sizeof("abc")

R=rsc
OCL=25105
CL=25105
This commit is contained in:
Ken Thompson 2009-02-17 13:10:57 -08:00
parent 575257d503
commit 3c0fc400fb

View File

@ -1525,7 +1525,7 @@ unsafenmagic(Node *l, Node *r)
{ {
Node *n; Node *n;
Sym *s; Sym *s;
Type *t; Type *t, *tr;
long v; long v;
Val val; Val val;
@ -1541,9 +1541,12 @@ unsafenmagic(Node *l, Node *r)
if(strcmp(s->name, "Sizeof") == 0) { if(strcmp(s->name, "Sizeof") == 0) {
walktype(r, Erv); walktype(r, Erv);
if(r->type == T) tr = r->type;
if(r->op == OLITERAL && r->val.ctype == CTSTR)
tr = types[TSTRING];
if(tr == T)
goto no; goto no;
v = r->type->width; v = tr->width;
goto yes; goto yes;
} }
if(strcmp(s->name, "Offsetof") == 0) { if(strcmp(s->name, "Offsetof") == 0) {
@ -1555,16 +1558,21 @@ unsafenmagic(Node *l, Node *r)
} }
if(strcmp(s->name, "Alignof") == 0) { if(strcmp(s->name, "Alignof") == 0) {
walktype(r, Erv); walktype(r, Erv);
if (r->type == T) tr = r->type;
if(r->op == OLITERAL && r->val.ctype == CTSTR)
tr = types[TSTRING];
if(tr == T)
goto no; goto no;
// make struct { byte; T; } // make struct { byte; T; }
t = typ(TSTRUCT); t = typ(TSTRUCT);
t->type = typ(TFIELD); t->type = typ(TFIELD);
t->type->type = types[TUINT8]; t->type->type = types[TUINT8];
t->type->down = typ(TFIELD); t->type->down = typ(TFIELD);
t->type->down->type = r->type; t->type->down->type = tr;
// compute struct widths // compute struct widths
dowidth(t); dowidth(t);
// the offset of T is its required alignment // the offset of T is its required alignment
v = t->type->down->width; v = t->type->down->width;
goto yes; goto yes;