Commit 5383e28e authored by Russ Cox's avatar Russ Cox

change string([]byte) to pass array, rather than &a[0],

to string convert.  if the byte array has length 0,
the computation of &a[0] throws an index bounds error.

for fixed size arrays, this ends up invoking arrays2d
unnecessarily, but it works.

R=ken
DELTA=304  (44 added, 28 deleted, 232 changed)
OCL=15674
CL=15678
parent c13c03c2
......@@ -24,6 +24,7 @@ func slicestring(string, int32, int32) string;
func indexstring(string, int32) byte;
func intstring(int64) string;
func byteastring(*byte, int32) string;
func arraystring(*[]byte) string;
func ifaceT2I(sigi *byte, sigt *byte, elem any) (ret any);
func ifaceI2T(sigt *byte, iface any) (ret any);
......@@ -100,6 +101,7 @@ export
indexstring
intstring
byteastring
arraystring
// interface
ifaceT2I
......
This diff is collapsed.
......@@ -1824,25 +1824,11 @@ stringop(Node *n, int top)
break;
case OARRAY:
// byteastring(*byte, int32) string;
t = n->left->type;
l = bytearraysz(t);
// &a[0]
c = nodintconst(0);
r = nod(OINDEX, n->left, c);
r = nod(OADDR, r, N);
if(l >= 0) {
// static size
c = nodintconst(l);
} else {
// dynamic size
c = nod(OLEN, n->left, N);
}
r = list(r, c);
on = syslook("byteastring", 0);
// arraystring(*[]byte) string;
r = n->left;
if(!isptr[r->type->etype])
r = nod(OADDR, r, N);
on = syslook("arraystring", 0);
r = nod(OCALL, on, r);
break;
}
......
......@@ -164,3 +164,12 @@ sys·byteastring(byte *a, int32 l, string s)
mcpy(s->str, a, l);
FLUSH(&s);
}
void
sys·arraystring(Array *b, string s)
{
s = mal(sizeof(s->len)+b->nel);
s->len = b->nel;
mcpy(s->str, b->array, s->len);
FLUSH(&s);
}
......@@ -9,5 +9,17 @@ package main
func main() {
var b [0]byte;
s := string(b); // out of bounds trap
if s != "" {
panic("bad convert")
}
var b1 = [5]byte{'h', 'e', 'l', 'l', 'o'};
if string(b1) != "hello" {
panic("bad convert 1")
}
var b2 = new([]byte, 5)
for i := 0; i < 5; i++ { b2[i] = b1[i] }
if string(b2) != "hello" {
panic("bad convert 2")
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment