541 Commits

Author SHA1 Message Date
Koichi Shiraishi
e62d8edb75 all: remove duplicate imports
protoreflect (as pref) and protoiface (as piface) are imported
duplicates in some files.
Respect package name, remove unnecessary aliased import statements.

Change-Id: Ie9897f17a50d19a462035964e366af72afed0e4d
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/405694
Reviewed-by: Kirill Kolyshkin <kolyshkin@gmail.com>
Reviewed-by: Lasse Folger <lassefolger@google.com>
Reviewed-by: Damien Neil <dneil@google.com>
2022-05-23 17:24:26 +00:00
Damien Neil
f8e9eb526f internal/encoding/text: fix -0 comparisons in tests
Correctly distinguish between 0 and -0 in tests.

Change-Id: I41f6702a903ca0cf9edf9e95be812a3a29930978
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/406917
Reviewed-by: Lasse Folger <lassefolger@google.com>
2022-05-19 09:42:31 +00:00
Kir Kolyshkin
bf9455640d all: fix typos
Brought to you by codespell v2.1.0, using the command

	codespell -S .cache,vendor -L ot,ba,fo,unparseable -w

Note that the misspelled "unparseable" comes from the
github.com/protocolbuffers/protobuf, where it is explicitly ignored
(see [1] and some explanation at [2]), so we ignore it here, too.

[1] https://github.com/protocolbuffers/protobuf/pull/7752
[2] https://github.com/protocolbuffers/protobuf/pull/7751#discussion_r460170422

Change-Id: Ie1ca705db4f11df8ec8b22fdc22b6a6ee667ae5b
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/406845
Reviewed-by: Damien Neil <dneil@google.com>
Reviewed-by: Lasse Folger <lassefolger@google.com>
2022-05-19 09:32:38 +00:00
Kir Kolyshkin
06eea5e15f internal/impl: CompressGZIP: simplify
This simplifies the code and fixes the following linter warning
reported by staticcheck:

        internal/impl/legacy_export.go:84:51: uint16(blockSize) ^ 0x0000 always equals uint16(blockSize) (SA4016)

Similar code:
 https://cs.opensource.google/go/go/+/master:src/compress/flate/huffman_bit_writer.go;l=413-414;drc=19309779ac5e2f5a2fd3cbb34421dafb2855ac21

Change-Id: Id099b0de635f668d44f57bacd89b564337560254
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/406841
Reviewed-by: Cassondra Foesch <cfoesch@gmail.com>
Reviewed-by: Lasse Folger <lassefolger@google.com>
Reviewed-by: Damien Neil <dneil@google.com>
Reviewed-by: Joseph Tsai <joetsai@digital-static.net>
2022-05-18 18:09:39 +00:00
Kir Kolyshkin
563f06fbeb internal/encoding/text/decode: limit errId length
Avoid very long errors returned by limiting the length of what errId
returns to 32 bytes (the value is chosen so that the error will not
be too long yet useful).

Append ellipsis to the returned value to denote that it was truncated.

Change-Id: I232d5192a2d9ad675daa0be0fe0c8518489c2953
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/406694
Reviewed-by: Damien Neil <dneil@google.com>
Reviewed-by: Lasse Folger <lassefolger@google.com>
2022-05-17 20:42:32 +00:00
Kir Kolyshkin
a0482351ba internal/encoding/text/decode: stop using regexp
This eliminates the last user of the regexp package, which should save
about 130K from the resulting stripped binary importing this package
(unless, of course, regexp is brought in directly of via another
dependency).

Added some new cases to TestDecoder to test the new function.

Benchmark (not included) shows the following results, comparing to
old implementation using regexp.Find:

name     old time/op    new time/op    delta
ErrId-4    1.93µs ± 1%    0.21µs ± 1%   -89.20%  (p=0.002 n=6+6)

name     old alloc/op   new alloc/op   delta
ErrId-4      128B ± 0%        0B       -100.00%  (p=0.002 n=6+6)

name     old allocs/op  new allocs/op  delta
ErrId-4      13.0 ± 0%       0.0       -100.00%  (p=0.002 n=6+6)

Change-Id: I5569a47580f41cc60f92c444e8d43bb3f26faa4e
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/402774
Reviewed-by: Cassondra Foesch <cfoesch@gmail.com>
Reviewed-by: Damien Neil <dneil@google.com>
Reviewed-by: Lasse Folger <lassefolger@google.com>
2022-05-16 20:59:57 +00:00
Lasse Folger
4c88e077ec all: start v1.28.0-devel
Change-Id: Ie9bd9f62312ff3f50787abd60067956b2911c59a
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/391415
Reviewed-by: Joseph Tsai <joetsai@digital-static.net>
Reviewed-by: Damien Neil <dneil@google.com>
2022-03-22 07:56:25 +00:00
Lasse Folger
32051b4f86 all: release v1.28.0
Change-Id: Ied1d78a3cc0d417fbbd1d10d30730173af3e20d4
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/391414
Reviewed-by: Joseph Tsai <joetsai@digital-static.net>
Reviewed-by: Damien Neil <dneil@google.com>
2022-03-22 07:56:23 +00:00
Lasse Folger
3992ea83a2 all: implement depth limit for unmarshaling
+ This change introduce a default and configurable depth limit for
  proto.Unmarshal. If a message is nested deeper than the limit,
  unmarshaling will fail. There are two ways to nest messages. Either by
  having fields which are message types itself or by using groups.
+ The default limit is 10,000 for now. This might change in the future
  to align it with other language implementation (C++ and Java use 100
  as limit).
+ If pure groups (groups that don't contain message fields) are nested
  deeper than the default limit the unmarshaling fails with:
  proto: cannot parse invalid wire-format data
+ Note: the configured limit does not apply to pure groups.
+ This change is introduced to improve security and robustness. Because
  unmarshaling is implemented using recursion it can lead to stack overflows
  for certain inputs. The introduced limit protects against this.
+ A secondary motivation for this limit is the alignment with other
  languages. Protocol buffers are a language interoperability mechanism
  and thus either all implementations should accept the input or all
  implementation should reject the input.

Change-Id: I14bdb44d06e4bd1aa90d6336c2cf6446003b2037
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/385854
Trust: Dmitri Shuralyov <dmitshur@golang.org>
Reviewed-by: Damien Neil <dneil@google.com>
Trust: Damien Neil <dneil@google.com>
Reviewed-by: Nicolas Hillegeer <aktau@google.com>
Reviewed-by: Chressie Himpel <chressie@google.com>
2022-02-17 17:07:31 +00:00
Damien Neil
3a9e1dc314 all: gofmt all
Change-Id: I1d42c8d784440a0b7e40b4f0b8da54be0fb338a6
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/370054
Trust: Damien Neil <dneil@google.com>
Run-TryBot: Damien Neil <dneil@google.com>
Reviewed-by: Joseph Tsai <joetsai@digital-static.net>
Reviewed-by: Florian Zenker <floriank@google.com>
2022-01-04 22:57:54 +00:00
Emmanuel T Odeke
26e8bcb3c7 all: remove unnecessary string([]byte) conversion in fmt.Sprintf with %s
Change-Id: I64aab811cbcbfa410817894f1cd1d83f88f27bf6
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/365874
Reviewed-by: Damien Neil <dneil@google.com>
Trust: Damien Neil <dneil@google.com>
Trust: Emmanuel Odeke <emmanuel@orijtech.com>
2021-11-29 18:55:28 +00:00
Joe Tsai
b03064a95c all: start v1.27.1-devel
Change-Id: Ifaeb330729a8d6f7fc403926db50d50e127859e9
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/331290
Trust: Joe Tsai <joetsai@digital-static.net>
Reviewed-by: Damien Neil <dneil@google.com>
2021-06-28 18:25:47 +00:00
Joe Tsai
b92717ecb6 all: release v1.27.1
Change-Id: I9acc84df59805ba789ef42ee2dc43e14ce56818b
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/331289
Trust: Joe Tsai <joetsai@digital-static.net>
Reviewed-by: Damien Neil <dneil@google.com>
2021-06-28 18:25:36 +00:00
Joe Tsai
aa432c0868 cmd/protoc-gen-go: fix generation of enum defaults
Previously, this call strconv.FormatInt with base 32,
when the intention is to call it with base 10.

Change-Id: I31cdd2415b3a80936cdcdeb5612a486204404ecb
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/331149
Trust: Joe Tsai <joetsai@digital-static.net>
Reviewed-by: Damien Neil <dneil@google.com>
2021-06-27 06:07:42 +00:00
Joe Tsai
49b6f723d3 all: start v1.27.0-devel
Change-Id: I7f2f94ed40c71582838fe6ffcb103b9e98a49d1c
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/330871
Trust: Joe Tsai <joetsai@digital-static.net>
Reviewed-by: Damien Neil <dneil@google.com>
2021-06-25 18:50:12 +00:00
Joe Tsai
3f51f05e40 all: release v1.27.0
Change-Id: Id3587f2020fa58a41574e5f2669c9e50568f06e2
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/330870
Trust: Joe Tsai <joetsai@digital-static.net>
Reviewed-by: Damien Neil <dneil@google.com>
2021-06-25 18:49:55 +00:00
Joe Tsai
acaef6adb7 cmd/protoc-gen-go: avoid referencing remote enum values by name
The Go generator has historically always prefixed an enum value
with the name of the enum type, when it was unnecessary to do so.

For example:
	enum Status {
		STATUS_FAILED = 0;
		STATUS_PASSED = 1;
	}
would be generated as:
	type Status int32
	const (
		Status_STATUS_FAILED Status = 0
		Status_STATUS_PASSED Status = 1
	)

It is common for the enum values to be manually prefixed by the
enum type since protobuf enums use C++ namespace rules where
enum types and enum values are in the same namespace scope.
Thus, having the Go generator add a prefix is redundant.
See https://github.com/golang/protobuf/issues/513.

Some custom Go generators like protoc-gen-gogo allow removing
the prefix with the gogoproto.goproto_enum_prefix feature.
However, this leads to interoperability issues between
protoc-gen-go and protoc-gen-gogo, where the enum value names
cannot be accurately inferred.

Avoid this problem by just hard-coding the enum value number
for values declared in other packages. This provides benefits
in interoperability at the small cost of enum values possibly
being stale if their value were ever changed in a remote package.
However, this would only occur with use of proto2 enums and
default values, which seems to be an exceptionally rare situation.

Before:
	Default_MyMessage_MyField = remotepb.FooEnum_FOO_ENUM
After:
	Default_MyMessage_MyField = remotepb.FooEnum(4) // remotepb.FooEnum_FOO_ENUM

Before:
	func (x *MyMessage) GetField() remotepb.FooEnum {
		...
		return remotepb.FooEnum_FOO_ZERO
	}
After:
	func (x *MyMessage) GetField() remotepb.FooEnum {
		...
		return remotepb.FooEnum(0) // always 0 for proto3 and often 0 for proto2
	}

Change-Id: I3a06cd553f2eaf6124666f6c36c196d500d35718
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/319649
Trust: Joe Tsai <joetsai@digital-static.net>
Reviewed-by: Damien Neil <dneil@google.com>
2021-05-24 22:21:56 +00:00
Damien Neil
24d799b3c1 internal/impl: support typed nil source for Merge of aberrant messages
When merging aberrant messages with legacy Marshal and Unmarshal
methods, check for a typed nil source before calling Marshal.

Add an aberrant message with Marshal/Unmarshal methods to
internal/testprotos/nullable and use it to test the internal/impl
support for these methods.

Fixes golang/protobuf#1324

Change-Id: Ib6ce85b30b46e3392a226ca6abe411932a371f02
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/321529
Trust: Damien Neil <dneil@google.com>
Run-TryBot: Damien Neil <dneil@google.com>
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2021-05-20 18:23:20 +00:00
Joe Tsai
fb30439f55 testing/protopack: add Message.UnmarshalAbductive
The protobuf wire format is insufficiently self-decribing such that
it is impossible to know whether for sure whether an unknown bytes value
is a sub-message or not. However, protopack is primarily used for debugging
where a best-effort guess is still very useful.

The Message.UnmarshalAbductive unmarshals an unknown bytes value as a message
if it is syntactically well-formed. Otherwise, it is left as is.

Change-Id: I5e2b4b995e2b5eb60942a242558bf4cea1da9891
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/309669
Trust: Joe Tsai <joetsai@digital-static.net>
Reviewed-by: Damien Neil <dneil@google.com>
2021-04-14 20:38:42 +00:00
Joe Tsai
e4fcb9f7e7 reflect: add protopath and protorange packages
The protopath package provides a means to programmatically represent
a sequence of protobuf reflection operations.
The protorange package traverses through a message and
calls a user-provided function as it iterates.

This feature sets the groundwork for the often requested feature
of being able to exclude certain fields when merging or serializing.

package protopath
    type Path []Step
    type Step struct{ ... }
        func Root(protoreflect.MessageDescriptor) Step
        func FieldAccess(protoreflect.FieldDescriptor) Step
        func UnknownAccess() Step
        func ListIndex(int) Step
        func MapIndex(protoreflect.MapKey) Step
        func AnyExpand(protoreflect.MessageDescriptor) Step
        func (Step) Kind() StepKind
        func (Step) FieldDescriptor() protoreflect.FieldDescriptor
        func (Step) MessageDescriptor() protoreflect.MessageDescriptor
        func (Step) ListIndex() int
        func (Step) MapIndex() protoreflect.MapKey
        func (Step) String() string
    type StepKind int
        const RootStep StepKind
        const FieldAccessStep StepKind
        const UnknownAccessStep StepKind
        const ListIndexStep StepKind
        const MapIndexStep StepKind
        const AnyExpandStep StepKind
    type Values struct {
        Path   Path
        Values []protoreflect.Value
    }
    func (Values) Index(int) (out struct{ ... })
    func (Values) Len() int
    func (Values) String() string

package protorange
    var Break error
    var Terminate error
    func Range(protoreflect.Message, func(protopath.Values) error) error
    type Options struct {
        Stable bool
        Resolver interface { ... }
    }
    func (Options) Range(m protoreflect.Message, push, pop func(protopath.Values) error) error

Change-Id: I29cbd5142fe169d78367d54a95d37801888b64f4
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/236540
Trust: Joe Tsai <joetsai@digital-static.net>
Reviewed-by: Damien Neil <dneil@google.com>
2021-04-01 23:09:19 +00:00
Joe Tsai
31610fd913 cmd/protoc-gen-go: remove generation of the ExtensionRangeArray method
The ExtensionRangeArray was a pseudo-internal API that was used by
the protobuf runtime implementation to know what the extension ranges are.
For some time now, the runtime implementation does not make use of this.

According to all the latest modules known by the module proxy,
no code depends directly on this method being present.

In the extremely rare case where some user code is depending on this method,
the user can place a .go file next to the generated .pb.go file that
injects this method.

Change-Id: Iae40a9a33b8c9b5a243d48db14f25b05ca24e3dc
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/305574
Trust: Joe Tsai <joetsai@digital-static.net>
Reviewed-by: Damien Neil <dneil@google.com>
2021-03-29 20:25:28 +00:00
Joe Tsai
f821cae922 all: start v1.26.0-devel
Change-Id: I544bd53c41794d5da2d381d3b182293a465f3c4b
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/302753
Trust: Joe Tsai <joetsai@digital-static.net>
Reviewed-by: Damien Neil <dneil@google.com>
2021-03-18 00:28:12 +00:00
Joe Tsai
f2d1f6cbe1 all: release v1.26.0
Change-Id: I15ecd5b768516fd2382cfbc8b337ad3d8817deab
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/302752
Trust: Joe Tsai <joetsai@digital-static.net>
Reviewed-by: Damien Neil <dneil@google.com>
2021-03-18 00:27:53 +00:00
Joe Tsai
41ef85a559 all: add weak dependency on github.com/golang/protobuf
Add back the weak dependency on github.com/golang/protobuf.

Change-Id: I2e6fabbe2cac3b694fb56a86bb3aa85cc8615b42
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/262684
Trust: Joe Tsai <joetsai@digital-static.net>
Reviewed-by: Damien Neil <dneil@google.com>
2021-03-18 00:24:57 +00:00
Joe Tsai
bdf6e19f23 all: start v1.26.0-rc.1.devel
Change-Id: I46da1bbedc2a0e53de1b02b07c5d6b76a18e1894
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/302751
Trust: Joe Tsai <joetsai@digital-static.net>
Reviewed-by: Damien Neil <dneil@google.com>
2021-03-17 22:45:59 +00:00
Joe Tsai
1c57cacb03 all: release v1.26.0-rc.1
Change-Id: I849df1819bf980fea006516beba4a551696e3811
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/302750
Trust: Joe Tsai <joetsai@digital-static.net>
Reviewed-by: Damien Neil <dneil@google.com>
2021-03-17 22:45:34 +00:00
Joe Tsai
b7197c14de all: remove weak dependency on github.com/golang/protobuf
Temporarily remove the dependency on github.com/golang/protobuf
and add it back in a subsequent CL. This allows us to break some of
the infinite cycles of old versions in the go.sum file.

Change-Id: Ifb5c2c160713d47a43ba2bcedc18ebe2ce33dcea
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/262683
Trust: Joe Tsai <joetsai@digital-static.net>
Reviewed-by: Damien Neil <dneil@google.com>
2021-03-17 21:55:02 +00:00
Joe Tsai
23ccb359e1 internal/impl: add runtime support for aberrant messages
Implement support in the protobuf runtime to better understand
message types that are not generated by the official generator.

In particular:
* Add a best-effort implementation of protobuf reflection for
"non-nullable" fields which are supposed to be represented by *T,
but are instead represented by a T. "Non-nullable" message fields
report presence based on whether the message is the zero Go value.
* We do NOT implement support for "non-nullable" fields in the
table-driven implementation since we assume that the aberrant messages
that we care about have a Marshal and Unmarshal method.
* We better handle custom messages that implement Marshal and Unmarshal,
but do NOT implement Merge. In that case, we implement merge in terms of
a back-to-back marshal and unmarshal.
* We better tolerate the situations where a protobuf message field
cannot be mapped to a Go struct field since the latter is missing.
In such cases, reflection treats the field as if it were unpopulated.
Setting such fields will panic.

This change allows the runtime to handle all message types declared
in the "go.etcd.io/etcd" and "k8s.io" modules where protobuf reflection,
Marshal, Unmarshal, Reset, Merge, and Equal all work.

The only types that still do not fully work are:
	* "k8s.io/api/authentication/v1".ExtraValue
	* "k8s.io/api/authentication/v1beta1".ExtraValue
	* "k8s.io/api/authorization/v1".ExtraValue
	* "k8s.io/api/authorization/v1beta1".ExtraValue
	* "k8s.io/api/certificates/v1".ExtraValue
	* "k8s.io/api/certificates/v1beta1".ExtraValue
	* "k8s.io/apimachinery/pkg/apis/meta/v1".MicroTime
	* "k8s.io/apimachinery/pkg/apis/meta/v1".Time
	* "k8s.io/apimachinery/pkg/apis/meta/v1".Verbs
While Marshal, Unmarshal, Reset, and Merge continue to work,
protobuf reflection and any functionality that depends on it
(e.g., prototext, protojson, Equal, etc.) will not work.

Change-Id: I67a9d2f1bec35248045ad0c16220d02fc2e0e172
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/300869
Trust: Joe Tsai <joetsai@digital-static.net>
Trust: Joe Tsai <thebrokentoaster@gmail.com>
Reviewed-by: Damien Neil <dneil@google.com>
2021-03-16 02:41:12 +00:00
Joe Tsai
839ce43689 internal/filedesc: remove ProtoLegacyRawDesc method
This method existed in the past to avoid a cyclic dependency
on descriptorpb from the legacy proto package.
This restriction no longer applies.

Change-Id: I5c61fa7de370063a126d8ea15c376b0882417b43
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/259900
Reviewed-by: Damien Neil <dneil@google.com>
Trust: Joe Tsai <thebrokentoaster@gmail.com>
2021-03-03 02:26:38 +00:00
Joe Tsai
85f47621e0 all: update protobuf toolchain dependency
This updates the dependency on the protobuf toolchain to an unreleased
version that has the go_package option for relevant .proto files in
that repository to reference this module.

Change-Id: Ie1ac8f81a323285efbd14a17d02c7105e810af2d
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/235283
Trust: Joe Tsai <joetsai@digital-static.net>
Reviewed-by: Damien Neil <dneil@google.com>
2021-03-03 01:59:11 +00:00
Justin SB
9d4d1ca5ed internal/descfmt: always include type name in FormatList
FormatList previously formatted output inconsistently when called on
EnumDescriptors, ServiceDescriptors and ExtensionDescriptors: other
types included the type name before {{; these types did not.

Add a default case that includes the type in these (and any other)
cases also.

Expanded tests to demonstrate and verify the new behaviour.

Change-Id: I9f53cb6548c4448a12c17d519639357ebdfa809b
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/286132
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
Trust: Joe Tsai <joetsai@google.com>
Trust: Matthew Dempsky <mdempsky@google.com>
2021-02-22 23:39:21 +00:00
cybrcodr
f1ac97a4c3 internal/encoding/json: fix comments
Change-Id: Ia1414d1a6c4edcacc141f0c927ad1f9f0012843a
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/285552
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
Trust: Herbie Ong <herbie@google.com>
2021-01-21 23:58:14 +00:00
Damien Neil
81d297c66c internal/fuzz: support coverage builds with oss-fuzz
Use OSS-Fuzz's new compile_go_fuzzer script, which enables code
coverage.

Change-Id: Iff07ac09d054e8c881d819b4ce6d016ad2808978
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/272768
Trust: Damien Neil <dneil@google.com>
Run-TryBot: Damien Neil <dneil@google.com>
Reviewed-by: Joe Tsai <joetsai@google.com>
2020-11-24 20:15:59 +00:00
Joe Tsai
d347099942 internal/cmd/generate-types: use ConsumeBytes instead of ConsumeString
The protowire.{ConsumeBytes,ConsumeString} funcs are identical except
that the latter allocates a string by implicitly converting the []byte.
Avoid using ConsumeString since we can do the conversion ourselves
at a latter point and sometimes avoid the allocation.

Change-Id: Idf31edc013b72ee5ee8461a68d10e57ad461d95c
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/263628
Trust: Joe Tsai <joetsai@digital-static.net>
Reviewed-by: Damien Neil <dneil@google.com>
2020-10-20 20:17:50 +00:00
Joe Tsai
41134b7e06 internal/cmd/generate-types: name the options variable
This trivially adds a name for marshalOptions and unmarshalOptions
instead of always having it be underscore.
This assists the use of patches that rely on this variable.

Change-Id: I4c23901c93f0362aff412339912522f92749d659
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/263627
Trust: Joe Tsai <joetsai@digital-static.net>
Reviewed-by: Damien Neil <dneil@google.com>
2020-10-20 19:16:57 +00:00
Joe Tsai
44626bc40b internal/cmd/generate-types: move variable declaration
Move the declaration of the slice variable
as close to its actual use as possible.

Change-Id: Ibe26460b5ca81edbed1073b32faf1c5f5d2c922a
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/263719
Trust: Joe Tsai <joetsai@digital-static.net>
Trust: Joe Tsai <joetsai@google.com>
Reviewed-by: Damien Neil <dneil@google.com>
2020-10-20 18:59:00 +00:00
Joe Tsai
f0affd4123 internal/cmd/generate-types: add outfile flag
Add an outfile flag that specifies that the contents of
the provided file should generated to stdout.
When using this mode, it only generates one file at a time
and does not modify any files on disk.

Change-Id: I80ba1764a7f0ceb180b8b54f4197dd57fec86434
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/263625
Trust: Joe Tsai <joetsai@digital-static.net>
Reviewed-by: Patrik Nyblom <pnyb@google.com>
Reviewed-by: Damien Neil <dneil@google.com>
2020-10-20 18:45:18 +00:00
Damien Neil
711224230b internal/encoding/text: escape Unicode control characters in strings
Escape not only ASCII control characters, but Unicode as well.

Change-Id: I5f5791ae51fc5624599f66ce012ecef364e7ea97
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/262682
Trust: Damien Neil <dneil@google.com>
Run-TryBot: Damien Neil <dneil@google.com>
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
Reviewed-by: Joe Tsai <joetsai@google.com>
2020-10-15 23:10:26 +00:00
Damien Neil
b1cd8e38bc internal/encoding/text: escape \x7f (DEL) in strings
We already escape ASCII control characters in the range [0x00,0x20).
Escape the one control character outside this range as well.

Change-Id: If954da0d4a178b36128d1a53d25397d1b3fd2e17
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/262681
Trust: Damien Neil <dneil@google.com>
Run-TryBot: Damien Neil <dneil@google.com>
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2020-10-15 21:51:19 +00:00
Joe Tsai
9712361eda all: rely on dynamic dependency check for genproto
The genproto module incurs an unfortunate amount of dependency bloat
since it has a relatively large set of transitive dependencies.
Even though these are brought in as a weak dependency
(i.e., there is no hard code dependency on genproto),
it complicates dependency inspection since users need to vet
modules that they may not actually be using.

Avoid this weak dependency and instead rely on a dynamic check
to ensure that a sufficiently new version of genproto is used.
While it can't statically prevent old versions of genproto from being
linked into a binary, it guarantees that it will panic at init
with a helpful message on what's wrong.

Change-Id: I2b5a9759a5cd4c4c57aced54278502b1b6352ba7
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/262679
Trust: Joe Tsai <thebrokentoaster@gmail.com>
Reviewed-by: Damien Neil <dneil@google.com>
2020-10-15 21:46:17 +00:00
Joe Tsai
ac374a2335 internal/encoding/messageset: fix spelling typo
Change-Id: Ic397fba6b382fbbe72779aa919f9109c23c8f209
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/259898
Reviewed-by: Damien Neil <dneil@google.com>
Trust: Joe Tsai <thebrokentoaster@gmail.com>
2020-10-07 22:15:00 +00:00
Joe Tsai
1a3946737f internal/encoding/text: remove dead code
The newline field is unused. Remove it.

Change-Id: I8a6c6ea64eff718920102b87aa1fc66d8e736ac7
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/248297
Reviewed-by: Herbie Ong <herbie@google.com>
2020-08-12 21:05:01 +00:00
Joe Tsai
e96d591a68 internal/msgfmt: fix formatting of negative Duration
If the seconds is zero, but the nanoseconds are negative,
there should still be a leading negative sign.

Change-Id: Ia72a26bc3455a80e572b6bf2ff41395381f811c7
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/247457
Reviewed-by: Herbie Ong <herbie@google.com>
Reviewed-by: Damien Neil <dneil@google.com>
2020-08-08 01:07:43 +00:00
Joe Tsai
5d63473da8 internal/impl: add runtime support for *[]byte unknown representation
This CL adds runtime support for unknown fields to be represented
as *[]byte in addition to the current representation as []byte.
This CL does not change generated code to use *[]byte.

Comparison between using *[]byte and []byte:

• Every message supports unknown fields, so use of []byte
  expands a message size by 24B (for 64-bit systems).
  In contrast, *[]byte only expands a message by 8B.
  This has significant memory implications for small messages.

• If unknown fields are encountered, *[]byte has extra overhead
  allocating the 24B slice header. However, it is assumed
  that messages rarely see any unknown fields at runtime,
  or generally do so for a temporary period of time.

Change-Id: I81935e4ea7394166e61ff4579f76f59fa792dfc9
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/244937
Reviewed-by: Damien Neil <dneil@google.com>
2020-07-28 16:36:39 +00:00
Joe Tsai
4194195e12 internal/impl: rename legacyProtoMethods as aberrantProtoMethods
This variable is only used by the aberrantMessage type,
rename it to properly reflect what it is used for.

Change-Id: I081e8954eb1b282f01f6d2d764bffaf31c6677e9
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/245077
Reviewed-by: Damien Neil <dneil@google.com>
2020-07-27 20:52:20 +00:00
Damien Neil
a0f95d5b14 internal/impl: make errInvalidUTF8 be a proto.Error
Fixes golang/protobuf#1174.

Change-Id: If95f9f79fff76dc0c44f7ee96e484f59fa287e58
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/244718
Reviewed-by: Joe Tsai <joetsai@google.com>
2020-07-24 21:43:34 +00:00
Joe Tsai
42cc4c592f reflect/protoreflect: improve source information usability
Added API:
  SourceLocations.ByPath
  SourceLocations.ByDescriptor
  SourceLocation.Next
  SourcePath.String
  SourcePath.Equal

We modify compiler/protogen to use SourceLocations.ByDescriptor.
In retrospect, if this had existed during the development of protogen,
we would not have exposed protogen.Location and related fields.

Change-Id: I58f17e59f90b9ba16f0982c4b71c2542e4ff6e75
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/238000
Reviewed-by: Damien Neil <dneil@google.com>
2020-07-23 21:50:46 +00:00
Joe Tsai
1a290e9a0e reflect/protoreflect: add MessageFieldTypes
The MessageFieldTypes interface (if implemented by a MessageType)
provides Go type information about the fields if they are
an enum or message type.

Change-Id: I68b20f5726377f6b0f2c20a8b6e45f9802b43f67
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/236777
Reviewed-by: Damien Neil <dneil@google.com>
2020-07-23 21:24:58 +00:00
Damien Neil
b5523e32b3 all: return less-specific, but more informative wire unmarshal errors
When proto.Unmarshal fails, it is almost always due to being passed
input which is not a wire-format message. (A text-format message, a
message with framing left intact, and so forth.) An error like
"variable length integer overflow" can be confusing in this case, since
it implies the problem is the varint rather than the input being
entirely wrong.

Replace all Unmarshal parse errors with "cannot parse invalid
wire-format data".

Change-Id: Id97253bd39ac604e569df71778194f37b3c86c28
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/244297
Reviewed-by: Joe Tsai <joetsai@google.com>
2020-07-23 20:09:00 +00:00
Joe Tsai
e7c3f89377 cmd/protoc-gen-go: remove name mangling for MessageSet extensions
MessageSet are a proto1 feature only used by Google.
We no longer rely on the name mangling logic performed here,
so remove it.

Change-Id: I5d66ebd86875894632f0d0c1e9816ae47ee0d5f4
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/242657
Reviewed-by: Herbie Ong <herbie@google.com>
2020-07-14 22:22:58 +00:00