proto: validate UTF-8 in proto3 strings

Change-Id: I6a495730c3f438e7b2c4ca86edade7d6f25aa47d
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/171700
Reviewed-by: Herbie Ong <herbie@google.com>
diff --git a/encoding/jsonpb/decode_test.go b/encoding/jsonpb/decode_test.go
index 941e416..cb99bcd 100644
--- a/encoding/jsonpb/decode_test.go
+++ b/encoding/jsonpb/decode_test.go
@@ -5,6 +5,7 @@
 package jsonpb_test
 
 import (
+	"bytes"
 	"math"
 	"testing"
 
@@ -2130,14 +2131,14 @@
   "value": "` + "abc\xff" + `"
 }`,
 		wantMessage: func() proto.Message {
-			m := &knownpb.StringValue{Value: "abc\xff"}
+			m := &knownpb.StringValue{Value: "abcd"}
 			b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
 			if err != nil {
 				t.Fatalf("error in binary marshaling message for Any.value: %v", err)
 			}
 			return &knownpb.Any{
 				TypeUrl: "google.protobuf.StringValue",
-				Value:   b,
+				Value:   bytes.Replace(b, []byte("abcd"), []byte("abc\xff"), -1),
 			}
 		}(),
 		wantErr: true,
@@ -2216,14 +2217,14 @@
   "value": "` + "abc\xff" + `"
 }`,
 		wantMessage: func() proto.Message {
-			m := &knownpb.Value{Kind: &knownpb.Value_StringValue{"abc\xff"}}
+			m := &knownpb.Value{Kind: &knownpb.Value_StringValue{"abcd"}}
 			b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
 			if err != nil {
 				t.Fatalf("error in binary marshaling message for Any.value: %v", err)
 			}
 			return &knownpb.Any{
 				TypeUrl: "google.protobuf.Value",
-				Value:   b,
+				Value:   bytes.Replace(b, []byte("abcd"), []byte("abc\xff"), -1),
 			}
 		}(),
 		wantErr: true,
@@ -2369,7 +2370,7 @@
   }
 }`,
 		wantMessage: func() proto.Message {
-			m1 := &knownpb.StringValue{Value: "abc\xff"}
+			m1 := &knownpb.StringValue{Value: "abcd"}
 			b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m1)
 			if err != nil {
 				t.Fatalf("error in binary marshaling message for Any.value: %v", err)
@@ -2385,7 +2386,7 @@
 			}
 			return &knownpb.Any{
 				TypeUrl: "pb2.KnownTypes",
-				Value:   b,
+				Value:   bytes.Replace(b, []byte("abcd"), []byte("abc\xff"), -1),
 			}
 		}(),
 		wantErr: true,
diff --git a/encoding/jsonpb/encode_test.go b/encoding/jsonpb/encode_test.go
index 1a2858e..4277a3d 100644
--- a/encoding/jsonpb/encode_test.go
+++ b/encoding/jsonpb/encode_test.go
@@ -5,6 +5,7 @@
 package jsonpb_test
 
 import (
+	"bytes"
 	"encoding/hex"
 	"math"
 	"strings"
@@ -1687,14 +1688,14 @@
 			Resolver: preg.NewTypes((&knownpb.StringValue{}).ProtoReflect().Type()),
 		},
 		input: func() proto.Message {
-			m := &knownpb.StringValue{Value: "abc\xff"}
+			m := &knownpb.StringValue{Value: "abcd"}
 			b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
 			if err != nil {
 				t.Fatalf("error in binary marshaling message for Any.value: %v", err)
 			}
 			return &knownpb.Any{
 				TypeUrl: "google.protobuf.StringValue",
-				Value:   b,
+				Value:   bytes.Replace(b, []byte("abcd"), []byte("abc\xff"), -1),
 			}
 		}(),
 		want: `{
@@ -1765,14 +1766,14 @@
 			Resolver: preg.NewTypes((&knownpb.Value{}).ProtoReflect().Type()),
 		},
 		input: func() proto.Message {
-			m := &knownpb.Value{Kind: &knownpb.Value_StringValue{"abc\xff"}}
+			m := &knownpb.Value{Kind: &knownpb.Value_StringValue{"abcd"}}
 			b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
 			if err != nil {
 				t.Fatalf("error in binary marshaling message for Any.value: %v", err)
 			}
 			return &knownpb.Any{
 				TypeUrl: "type.googleapis.com/google.protobuf.Value",
-				Value:   b,
+				Value:   bytes.Replace(b, []byte("abcd"), []byte("abc\xff"), -1),
 			}
 		}(),
 		want: `{
diff --git a/encoding/textpb/encode_test.go b/encoding/textpb/encode_test.go
index 3397d66..41cae94 100644
--- a/encoding/textpb/encode_test.go
+++ b/encoding/textpb/encode_test.go
@@ -5,6 +5,7 @@
 package textpb_test
 
 import (
+	"bytes"
 	"encoding/hex"
 	"math"
 	"strings"
@@ -1248,7 +1249,7 @@
 		},
 		input: func() proto.Message {
 			m := &pb3.Nested{
-				SString: "abc\xff",
+				SString: "abcd",
 			}
 			b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
 			if err != nil {
@@ -1256,7 +1257,7 @@
 			}
 			return &knownpb.Any{
 				TypeUrl: string(m.ProtoReflect().Type().FullName()),
-				Value:   b,
+				Value:   bytes.Replace(b, []byte("abcd"), []byte("abc\xff"), -1),
 			}
 		}(),
 		want: `[pb3.Nested]: {