Skip to content

Commit 0a4d612

Browse files
compiler, runtime: allow slice to array pointer conversion
Panic if the slice is too short. For golang/go#395 Change-Id: I184f87d0207dcee4be6b36ae446b84e9583b356d Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/338630 Trust: Ian Lance Taylor <[email protected]> Reviewed-by: Cherry Mui <[email protected]>
1 parent ad667e7 commit 0a4d612

File tree

5 files changed

+72
-2
lines changed

5 files changed

+72
-2
lines changed

go/expressions.cc

+51-2
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

go/runtime.def

+5
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

go/types.cc

+7
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()

libgo/go/runtime/error.go

+2
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

+7
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)