Skip to content

Commit 7459bfa

Browse files
compiler, runtime: allow slice to array pointer conversion
Panic if the slice is too short. For golang/go#395 Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/338630
1 parent 06d0437 commit 7459bfa

File tree

7 files changed

+159
-3
lines changed

7 files changed

+159
-3
lines changed

gcc/go/gofrontend/MERGE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
ad667e7c70cea9fa5730660d72ad891b5753eb62
1+
0a4d612e6b211780b294717503fc739bbd1f509c
22

33
The first line of this file holds the git revision number of the last
44
merge done from the gofrontend repository.

gcc/go/gofrontend/expressions.cc

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3866,11 +3866,12 @@ Type_conversion_expression::do_traverse(Traverse* traverse)
38663866
return TRAVERSE_CONTINUE;
38673867
}
38683868

3869-
// Convert to a constant at lowering time.
3869+
// Convert to a constant at lowering time. Also lower conversions
3870+
// from slice to pointer-to-array, as they can panic.
38703871

38713872
Expression*
38723873
Type_conversion_expression::do_lower(Gogo*, Named_object*,
3873-
Statement_inserter*, int)
3874+
Statement_inserter* inserter, int)
38743875
{
38753876
Type* type = this->type_;
38763877
Expression* val = this->expr_;
@@ -3958,6 +3959,54 @@ Type_conversion_expression::do_lower(Gogo*, Named_object*,
39583959
}
39593960
}
39603961

3962+
if (type->points_to() != NULL
3963+
&& type->points_to()->array_type() != NULL
3964+
&& !type->points_to()->is_slice_type()
3965+
&& val->type()->is_slice_type())
3966+
{
3967+
Temporary_statement* val_temp = NULL;
3968+
if (!val->is_multi_eval_safe())
3969+
{
3970+
val_temp = Statement::make_temporary(val->type(), NULL, location);
3971+
inserter->insert(val_temp);
3972+
val = Expression::make_set_and_use_temporary(val_temp, val,
3973+
location);
3974+
}
3975+
3976+
Type* int_type = Type::lookup_integer_type("int");
3977+
Temporary_statement* vallen_temp =
3978+
Statement::make_temporary(int_type, NULL, location);
3979+
inserter->insert(vallen_temp);
3980+
3981+
Expression* arrlen = type->points_to()->array_type()->length();
3982+
Expression* vallen =
3983+
Expression::make_slice_info(val, Expression::SLICE_INFO_LENGTH,
3984+
location);
3985+
vallen = Expression::make_set_and_use_temporary(vallen_temp, vallen,
3986+
location);
3987+
Expression* cond = Expression::make_binary(OPERATOR_GT, arrlen, vallen,
3988+
location);
3989+
3990+
vallen = Expression::make_temporary_reference(vallen_temp, location);
3991+
Expression* panic = Runtime::make_call(Runtime::PANIC_SLICE_CONVERT,
3992+
location, 2, arrlen, vallen);
3993+
3994+
Expression* nil = Expression::make_nil(location);
3995+
Expression* check = Expression::make_conditional(cond, panic, nil,
3996+
location);
3997+
3998+
if (val_temp == NULL)
3999+
val = val->copy();
4000+
else
4001+
val = Expression::make_temporary_reference(val_temp, location);
4002+
Expression* ptr =
4003+
Expression::make_slice_info(val, Expression::SLICE_INFO_VALUE_POINTER,
4004+
location);
4005+
ptr = Expression::make_unsafe_cast(type, ptr, location);
4006+
4007+
return Expression::make_compound(check, ptr, location);
4008+
}
4009+
39614010
return this;
39624011
}
39634012

gcc/go/gofrontend/runtime.def

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,11 @@ DEF_GO_RUNTIME(PANIC_EXTEND_SLICE3_C, "runtime.goPanicExtendSlice3C",
582582
DEF_GO_RUNTIME(PANIC_EXTEND_SLICE3_C_U, "runtime.goPanicExtendSlice3CU",
583583
P2(UINT64, INT), R0())
584584

585+
// Panic for conversion of slice to pointer-to-array if the slice is
586+
// too short.
587+
DEF_GO_RUNTIME(PANIC_SLICE_CONVERT, "runtime.goPanicSliceConvert",
588+
P2(INT, INT), R0())
589+
585590
// Remove helper macros.
586591
#undef ABFT6
587592
#undef ABFT2

gcc/go/gofrontend/types.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,13 @@ Type::are_convertible(const Type* lhs, const Type* rhs, std::string* reason)
842842
return true;
843843
}
844844

845+
// A slice may be converted to a pointer-to-array.
846+
if (rhs->is_slice_type()
847+
&& lhs->points_to() != NULL
848+
&& lhs->points_to()->array_type() != NULL
849+
&& !lhs->points_to()->is_slice_type())
850+
return true;
851+
845852
// An unsafe.Pointer type may be converted to any pointer type or to
846853
// a type whose underlying type is uintptr, and vice-versa.
847854
if (lhs->is_unsafe_pointer_type()
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// run
2+
3+
// Copyright 2020 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
// Test conversion from slice to array pointer.
8+
9+
package main
10+
11+
func wantPanic(fn func(), s string) {
12+
defer func() {
13+
err := recover()
14+
if err == nil {
15+
panic("expected panic")
16+
}
17+
if got := err.(error).Error(); got != s {
18+
panic("expected panic " + s + " got " + got)
19+
}
20+
}()
21+
fn()
22+
}
23+
24+
func main() {
25+
s := make([]byte, 8, 10)
26+
if p := (*[8]byte)(s); &p[0] != &s[0] {
27+
panic("*[8]byte conversion failed")
28+
}
29+
wantPanic(
30+
func() {
31+
_ = (*[9]byte)(s)
32+
},
33+
"runtime error: cannot convert slice with length 8 to pointer to array with length 9",
34+
)
35+
36+
var n []byte
37+
if p := (*[0]byte)(n); p != nil {
38+
panic("nil slice converted to *[0]byte should be nil")
39+
}
40+
41+
z := make([]byte, 0)
42+
if p := (*[0]byte)(z); p == nil {
43+
panic("empty slice converted to *[0]byte should be non-nil")
44+
}
45+
46+
// Test with named types
47+
type Slice []int
48+
type Int4 [4]int
49+
type PInt4 *[4]int
50+
ii := make(Slice, 4)
51+
if p := (*Int4)(ii); &p[0] != &ii[0] {
52+
panic("*Int4 conversion failed")
53+
}
54+
if p := PInt4(ii); &p[0] != &ii[0] {
55+
panic("PInt4 conversion failed")
56+
}
57+
}
58+
59+
// test static variable conversion
60+
61+
var (
62+
ss = make([]string, 10)
63+
s5 = (*[5]string)(ss)
64+
s10 = (*[10]string)(ss)
65+
66+
ns []string
67+
ns0 = (*[0]string)(ns)
68+
69+
zs = make([]string, 0)
70+
zs0 = (*[0]string)(zs)
71+
)
72+
73+
func init() {
74+
if &ss[0] != &s5[0] {
75+
panic("s5 conversion failed")
76+
}
77+
if &ss[0] != &s10[0] {
78+
panic("s5 conversion failed")
79+
}
80+
if ns0 != nil {
81+
panic("ns0 should be nil")
82+
}
83+
if zs0 == nil {
84+
panic("zs0 should not be nil")
85+
}
86+
}

libgo/go/runtime/error.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ const (
175175
boundsSlice3B // s[?:x:y], 0 <= x <= y failed (but boundsSlice3A didn't happen)
176176
boundsSlice3C // s[x:y:?], 0 <= x <= y failed (but boundsSlice3A/B didn't happen)
177177

178+
boundsConvert // (*[x]T)(s), 0 <= x <= len(s) failed
178179
// Note: in the above, len(s) and cap(s) are stored in y
179180
)
180181

@@ -190,6 +191,7 @@ var boundsErrorFmts = [...]string{
190191
boundsSlice3Acap: "slice bounds out of range [::%x] with capacity %y",
191192
boundsSlice3B: "slice bounds out of range [:%x:%y]",
192193
boundsSlice3C: "slice bounds out of range [%x:%y:]",
194+
boundsConvert: "cannot convert slice with length %y to pointer to array with length %x",
193195
}
194196

195197
// boundsNegErrorFmts are overriding formats if x is negative. In this case there's no need to report y.

libgo/go/runtime/panic.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import (
3838
//go:linkname goPanicSlice3BU
3939
//go:linkname goPanicSlice3C
4040
//go:linkname goPanicSlice3CU
41+
//go:linkname goPanicSliceConvert
4142
//go:linkname panicshift
4243
//go:linkname panicdivide
4344
//go:linkname panicmem
@@ -175,6 +176,12 @@ func goPanicSlice3CU(x uint, y int) {
175176
panic(boundsError{x: int64(x), signed: false, y: y, code: boundsSlice3C})
176177
}
177178

179+
// failures in the conversion (*[x]T)s, 0 <= x <= y, x == cap(s)
180+
func goPanicSliceConvert(x int, y int) {
181+
panicCheck1(getcallerpc(), "slice length too short to convert to pointer to array")
182+
panic(boundsError{x: int64(x), signed: true, y: y, code: boundsConvert})
183+
}
184+
178185
var shiftError = error(errorString("negative shift amount"))
179186

180187
func panicshift() {

0 commit comments

Comments
 (0)