all: remove non-fatal UTF-8 validation errors (and non-fatal in general)

Immediately abort (un)marshal operations when encountering invalid UTF-8
data in proto3 strings. No other proto implementation supports non-UTF-8
data in proto3 strings (and many reject it in proto2 strings as well).
Producing invalid output is an interoperability threat (other
implementations won't be able to read it).

The case where existing string data is found to contain non-UTF8 data is
better handled by changing the field to the `bytes` type, which (aside
from UTF-8 validation) is wire-compatible with `string`.

Remove the errors.NonFatal type, since there are no remaining cases
where it is needed. "Non-fatal" errors which produce results and a
non-nil error are problematic because they compose poorly; the better
approach is to take an option like AllowPartial indicating which
conditions to check for.

Change-Id: I9d189ec6ffda7b5d96d094aa1b290af2e3f23736
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/183098
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
diff --git a/internal/encoding/json/decode.go b/internal/encoding/json/decode.go
index 27dff34..ae2ee9a 100644
--- a/internal/encoding/json/decode.go
+++ b/internal/encoding/json/decode.go
@@ -69,9 +69,8 @@
 		return d.value, d.err
 	}
 
-	var nerr errors.NonFatal
 	value, err := d.parseNext()
-	if !nerr.Merge(err) {
+	if err != nil {
 		return Value{}, err
 	}
 	n := value.size
@@ -145,7 +144,7 @@
 	if d.value.typ == comma {
 		return d.Read()
 	}
-	return value, nerr.E
+	return value, nil
 }
 
 // Any sequence that looks like a non-delimiter (for error reporting).
@@ -193,12 +192,11 @@
 		return d.newValue(Number, in, n), nil
 
 	case '"':
-		var nerr errors.NonFatal
 		s, n, err := d.parseString(in)
-		if !nerr.Merge(err) {
+		if err != nil {
 			return Value{}, err
 		}
-		return d.newStringValue(in, n, s), nerr.E
+		return d.newStringValue(in, n, s), nil
 
 	case '{':
 		return d.newValue(StartObject, in, 1), nil
diff --git a/internal/encoding/json/decode_test.go b/internal/encoding/json/decode_test.go
index daf3f7c..c29d508 100644
--- a/internal/encoding/json/decode_test.go
+++ b/internal/encoding/json/decode_test.go
@@ -121,10 +121,7 @@
 		{
 			// Invalid UTF-8 error is returned in ReadString instead of Read.
 			input: "\"\xff\"",
-			want: []R{
-				{T: json.String, E: `invalid UTF-8 detected`, V: string("\xff")},
-				{T: json.EOF},
-			},
+			want:  []R{{E: `syntax error (line 1:1): invalid UTF-8 in string`}},
 		},
 		{
 			input: `"` + string(utf8.RuneError) + `"`,
diff --git a/internal/encoding/json/encode_test.go b/internal/encoding/json/encode_test.go
index 3f85f65..b98f98b 100644
--- a/internal/encoding/json/encode_test.go
+++ b/internal/encoding/json/encode_test.go
@@ -349,19 +349,6 @@
 		]
 	]
 }`,
-		},
-		{
-			desc: "string contains rune error",
-			write: func(e *json.Encoder) {
-				// WriteString returns non-fatal error for invalid UTF sequence, but
-				// should still output the written value. See TestWriteStringError
-				// below that checks for this.
-				e.StartObject()
-				e.WriteName("invalid rune")
-				e.WriteString("abc\xff")
-				e.EndObject()
-			},
-			wantOut: "{\"invalid rune\":\"abc\xff\"}",
 		}}
 
 	for _, tc := range tests {
diff --git a/internal/encoding/json/string.go b/internal/encoding/json/string.go
index 1d89aca..b63dd5c 100644
--- a/internal/encoding/json/string.go
+++ b/internal/encoding/json/string.go
@@ -16,15 +16,13 @@
 )
 
 func appendString(out []byte, in string) ([]byte, error) {
-	var nerr errors.NonFatal
 	out = append(out, '"')
 	i := indexNeedEscapeInString(in)
 	in, out = in[i:], append(out, in[:i]...)
 	for len(in) > 0 {
 		switch r, n := utf8.DecodeRuneInString(in); {
 		case r == utf8.RuneError && n == 1:
-			nerr.AppendInvalidUTF8("")
-			in, out = in[1:], append(out, in[0]) // preserve invalid byte
+			return out, errors.InvalidUTF8("")
 		case r < ' ' || r == '"' || r == '\\':
 			out = append(out, '\\')
 			switch r {
@@ -52,11 +50,10 @@
 		}
 	}
 	out = append(out, '"')
-	return out, nerr.E
+	return out, nil
 }
 
 func (d *Decoder) parseString(in []byte) (string, int, error) {
-	var nerr errors.NonFatal
 	in0 := in
 	if len(in) == 0 {
 		return "", 0, io.ErrUnexpectedEOF
@@ -70,14 +67,13 @@
 	for len(in) > 0 {
 		switch r, n := utf8.DecodeRune(in); {
 		case r == utf8.RuneError && n == 1:
-			nerr.AppendInvalidUTF8("")
-			in, out = in[1:], append(out, in[0]) // preserve invalid byte
+			return "", 0, d.newSyntaxError("invalid UTF-8 in string")
 		case r < ' ':
 			return "", 0, d.newSyntaxError("invalid character %q in string", r)
 		case r == '"':
 			in = in[1:]
 			n := len(in0) - len(in)
-			return string(out), n, nerr.E
+			return string(out), n, nil
 		case r == '\\':
 			if len(in) < 2 {
 				return "", 0, io.ErrUnexpectedEOF