From 151b2f15094168946993448f7f4a5e2a8441bd76 Mon Sep 17 00:00:00 2001 From: Luuk van Dijk Date: Wed, 9 Nov 2011 11:17:06 +0100 Subject: [PATCH] gc: Fail on implicit assigment to non-exported fields in copy and append. Fixes #1387. R=rsc CC=golang-dev https://golang.org/cl/5348046 --- src/cmd/gc/typecheck.c | 5 +++++ test/fixedbugs/bug378.go | 27 +++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 test/fixedbugs/bug378.go diff --git a/src/cmd/gc/typecheck.c b/src/cmd/gc/typecheck.c index f154db9eb48..f84f8440c4d 100644 --- a/src/cmd/gc/typecheck.c +++ b/src/cmd/gc/typecheck.c @@ -1032,6 +1032,9 @@ reswitch: yyerror("first argument to append must be slice; have %lT", t); goto error; } + if(!exportassignok(t->type, "append")) + goto error; + if(n->isddd) { if(args->next == nil) { yyerror("cannot use ... on first argument to append"); @@ -1098,6 +1101,8 @@ reswitch: yyerror("arguments to copy have different element types: %lT and %lT", n->left->type, n->right->type); goto error; } + if(!exportassignok(n->left->type->type, "copy")) + goto error; goto ret; case OCONV: diff --git a/test/fixedbugs/bug378.go b/test/fixedbugs/bug378.go new file mode 100644 index 00000000000..91975f2e931 --- /dev/null +++ b/test/fixedbugs/bug378.go @@ -0,0 +1,27 @@ +// errchk $G $D/$F.go + +// Copyright 2011 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Issue 1387 +package foo + +import "bytes" + +func i() { + a := make([]bytes.Buffer, 1) + b := a[0] // ERROR "unexported field" +} + +func f() { + a := make([]bytes.Buffer, 1) + a = append(a, a...) // ERROR "unexported field" +} + + +func g() { + a := make([]bytes.Buffer, 1) + b := make([]bytes.Buffer, 1) + copy(b, a) // ERROR "unexported field" +}