proto: fix MarshalAppend fast path

Was overwriting the output buffer rather than appending to it.

Change-Id: I6ffb72a440f464f4259cfebc42c1dc75b73fb5ae
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/171117
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
diff --git a/proto/encode_test.go b/proto/encode_test.go
index b9e04b9..30722e0 100644
--- a/proto/encode_test.go
+++ b/proto/encode_test.go
@@ -9,6 +9,8 @@
 	protoV1 "github.com/golang/protobuf/proto"
 	"github.com/golang/protobuf/v2/proto"
 	"github.com/google/go-cmp/cmp"
+
+	test3pb "github.com/golang/protobuf/v2/internal/testprotos/test3"
 )
 
 func TestEncode(t *testing.T) {
@@ -105,3 +107,17 @@
 		}
 	}
 }
+
+func TestMarshalAppend(t *testing.T) {
+	want := []byte("prefix")
+	got := append([]byte(nil), want...)
+	got, err := proto.MarshalOptions{}.MarshalAppend(got, &test3pb.TestAllTypes{
+		OptionalString: "value",
+	})
+	if err != nil {
+		t.Fatal(err)
+	}
+	if !bytes.HasPrefix(got, want) {
+		t.Fatalf("MarshalAppend modified prefix: got %v, want prefix %v", got, want)
+	}
+}