internal/impl: add message validator
This adds a experimental function to the internal/impl package which
validates a wire-format message against a message type. The validator
reports whether the message can be successfully unmarshaled, and whether
the result is initialized (all required fields are set). In some cases,
the validator returns ambiguous results when full validation would be
expensive.
The validator is unused outside of tests. In the future, it may be used
to permit lazy unmarshaling of some data. It is being added now for
testing; in particular, the wire fuzzer now checks the validator output
for consistency with the unmarshaler.
The validator adds a small amount of unused per-MessageType state. If
this becomes a concern, we could conditionalize it with a build tag.
Change-Id: I4216ef81d6a9ed975302eed189b02d08608858b4
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/212302
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
diff --git a/internal/impl/codec_message.go b/internal/impl/codec_message.go
index 6cfb5c7..e487a26 100644
--- a/internal/impl/codec_message.go
+++ b/internal/impl/codec_message.go
@@ -30,10 +30,12 @@
extensionOffset offset
needsInitCheck bool
isMessageSet bool
+ numRequiredFields uint8
}
type coderFieldInfo struct {
funcs pointerCoderFuncs // fast-path per-field functions
+ validation validationInfo // information used by message validation
num pref.FieldNumber // field number
offset offset // struct field offset
wiretag uint64 // field tag (number + wire type)
@@ -76,11 +78,12 @@
funcs = fieldCoder(fd, ft)
}
cf := &coderFieldInfo{
- num: fd.Number(),
- offset: fieldOffset,
- wiretag: wiretag,
- tagsize: wire.SizeVarint(wiretag),
- funcs: funcs,
+ num: fd.Number(),
+ offset: fieldOffset,
+ wiretag: wiretag,
+ tagsize: wire.SizeVarint(wiretag),
+ funcs: funcs,
+ validation: newFieldValidationInfo(mi, si, fd, ft),
isPointer: (fd.Cardinality() == pref.Repeated ||
fd.Kind() == pref.MessageKind ||
fd.Kind() == pref.GroupKind ||