Skip to content

Commit

Permalink
internal/impl: make errInvalidUTF8 be a proto.Error
Browse files Browse the repository at this point in the history
Fixes golang/protobuf#1174.

Change-Id: If95f9f79fff76dc0c44f7ee96e484f59fa287e58
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/244718
Reviewed-by: Joe Tsai <joetsai@google.com>
  • Loading branch information
neild committed Jul 24, 2020
1 parent 1ecb1f4 commit a0f95d5
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 0 deletions.
2 changes: 2 additions & 0 deletions internal/impl/codec_field.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"sync"

"google.golang.org/protobuf/encoding/protowire"
"google.golang.org/protobuf/internal/errors"
"google.golang.org/protobuf/proto"
pref "google.golang.org/protobuf/reflect/protoreflect"
preg "google.golang.org/protobuf/reflect/protoregistry"
Expand All @@ -20,6 +21,7 @@ type errInvalidUTF8 struct{}

func (errInvalidUTF8) Error() string { return "string field contains invalid UTF-8" }
func (errInvalidUTF8) InvalidUTF8() bool { return true }
func (errInvalidUTF8) Unwrap() error { return errors.Error }

// initOneofFieldCoders initializes the fast-path functions for the fields in a oneof.
//
Expand Down
3 changes: 3 additions & 0 deletions proto/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/testing/protopack"

"google.golang.org/protobuf/internal/errors"
testpb "google.golang.org/protobuf/internal/testprotos/test"
test3pb "google.golang.org/protobuf/internal/testprotos/test3"
)
Expand Down Expand Up @@ -82,6 +83,8 @@ func TestDecodeInvalidMessages(t *testing.T) {
got := want.ProtoReflect().New().Interface()
if err := opts.Unmarshal(test.wire, got); err == nil {
t.Errorf("Unmarshal unexpectedly succeeded\ninput bytes: [%x]\nMessage:\n%v", test.wire, prototext.Format(got))
} else if !errors.Is(err, proto.Error) {
t.Errorf("Unmarshal error is not a proto.Error: %v", err)
}
})
}
Expand Down
4 changes: 4 additions & 0 deletions proto/encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"google.golang.org/protobuf/proto"
pref "google.golang.org/protobuf/reflect/protoreflect"

"google.golang.org/protobuf/internal/errors"
orderpb "google.golang.org/protobuf/internal/testprotos/order"
testpb "google.golang.org/protobuf/internal/testprotos/test"
test3pb "google.golang.org/protobuf/internal/testprotos/test3"
Expand Down Expand Up @@ -136,6 +137,9 @@ func TestEncodeInvalidMessages(t *testing.T) {
if err == nil {
t.Fatalf("Marshal unexpectedly succeeded\noutput bytes: [%x]\nMessage:\n%v", got, prototext.Format(m))
}
if !errors.Is(err, proto.Error) {
t.Fatalf("Marshal error is not a proto.Error: %v", err)
}
})
}
}
Expand Down

0 comments on commit a0f95d5

Please sign in to comment.