Commit Graph

192 Commits

Author SHA1 Message Date
Nicolas Hillegeer
87fded5d2a internal/impl: ensure proto.HasExtension does not allocate
Extensions are unmarshaled lazily if protolegacy is true. The current
implementation of proto.HasExtension forces this unmarshaling to happen.
Change that.

Lazy message extensions are unmarshaled on first access, see
(*ExtensionField).Value. This leads to an (expensive) unmarshal
operation even if the user only wanted to know whether the extension is
present.

Granted, in most cases a HasExtension returning true will be followed by
a GetExtension. Due to memoization (see (*ExtensionField).lazyInit), the
cost will just shift from HasExtension to GetExtension. But, this CL
allows building cheaper functionality that only needs to know about
extension existence.

Why can this validation be removed?

 - All tests pass.
 - This check was added in CL 229558. The author (Joe Tsai) noted:

> Technically this shouldn't be needed, but I couldn't adequately reason
> whether a nil message value would ever be set through non-reflection
> means.

Like the author, I believe it's not needed:

 - `proto.SetExtension` does not allow setting invalid messages (see
   proto/extension.go).
 - Likewise, (*extensionMap).Set panics when attempting to set an
   invalid value.
 - Unmarshaling does not produce submessages for which `IsValid` is
   false.

The added test fails without the fix:

    $ go test -tags=protolegacy -test.v -test.run=TestHasExtensionNoAlloc proto/extension_test.go
    === RUN   TestHasExtensionNoAlloc
    === RUN   TestHasExtensionNoAlloc/Nil
    === RUN   TestHasExtensionNoAlloc/Eager
    === RUN   TestHasExtensionNoAlloc/Lazy
        extension_test.go:156: proto.HasExtension should not allocate, but allocated 3.00B per run
    --- FAIL: TestHasExtensionNoAlloc (0.00s)
        --- PASS: TestHasExtensionNoAlloc/Nil (0.00s)
        --- PASS: TestHasExtensionNoAlloc/Eager (0.00s)
        --- FAIL: TestHasExtensionNoAlloc/Lazy (0.00s)
    FAIL
    FAIL    command-line-arguments  0.018s

The tests are disabled in race mode because the race instrumentation for
closures et al. always allocates. The protolegacy tests were previously
only run in race mode. I added a non-race variant in
integration_test.go.

Change-Id: Idbc67c1cf0aea8833a2735ca7bfc8d2466ceaf44
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/575035
Reviewed-by: Nicolas Hillegeer <aktau@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Damien Neil <dneil@google.com>
Reviewed-by: Lasse Folger <lassefolger@google.com>
Auto-Submit: Nicolas Hillegeer <aktau@google.com>
2024-03-28 16:31:28 +00:00
Michael Stapelberg
4fd828fdbf proto: extend Unmarshal documentation, include an example
This example uses the same protobuf and wire format encoding
as the corresponding Marshal example added in commit
https://go.googlesource.com/protobuf/+/c69658e23457d4e09

Change-Id: Ifd64a93a14589595cbe9b218235b57fb15d423c2
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/574635
Reviewed-by: Lasse Folger <lassefolger@google.com>
Reviewed-by: Damien Neil <dneil@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2024-03-28 08:17:19 +00:00
Michael Stapelberg
c69658e234 proto: extend Marshal documentation, include an example
The example has been written so that it can be run, meaning it only uses
packages that are included in the protobuf module (durationpb specifically).

I included an Output: comment in the example so that pkgsite displays the
program output even without having to run it (but running it is of course
possible).

I added a brief tip to protoscope, which is often mentioned in the protobuf.dev
docs for illustrative purposes, and I think it really makes much clearer to the
reader how the protobuf wire format looks like and what information survives the
encoding process (field numbers and their values, but not field names like in
JSON).

The struct literal contains only one field so that we don’t need to marshal this
message deterministically for stable wire format, which is not the point of the
example and would be distracting.

The value was chosen such that the wire format hex representation contains at
least one byte that is clearly identifiable as hexadecimal, to avoid confusion.

Change-Id: I86103abfd7d5b3f654aca3bfbb452f8ef7e49828
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/574455
Reviewed-by: Damien Neil <dneil@google.com>
Reviewed-by: Lasse Folger <lassefolger@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2024-03-27 09:10:56 +00:00
Lasse Folger
3ebf7dd8a5 proto: fix HasPresence for extensions and missing plugin response
Change-Id: I9ee81f7a88cf91f3041e89936cb03511bd224603
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/574375
Auto-Submit: Lasse Folger <lassefolger@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Michael Stapelberg <stapelberg@google.com>
2024-03-26 09:28:59 +00:00
Lasse Folger
7259b46773 all: implement proto2/proto3 as editions [1/2]
This change removes most usages of Syntax() from the repository and uses
edition features for instead. The appropriate edition feature defaults are
loaded for proto2/proto3 when the initialization of the descriptors
start.

All of these changes were tested on the Google corpus.

Change-Id: Ieca076a2b38ca8e50e084cd32e725b7b3dcb4171
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/572435
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Michael Stapelberg <stapelberg@google.com>
2024-03-19 09:40:08 +00:00
Michael Stapelberg
f56368575a all: use subtests to identify the message type
This makes it a little easier to track down test failures.

Also add a note to TestMarshalAppendAllocations to explain what it tests.

Change-Id: Ie35f3ddd7c7d5cb300294f0fe665c6711d45d186
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/569775
Auto-Submit: Michael Stapelberg <stapelberg@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Lasse Folger <lassefolger@google.com>
2024-03-07 15:21:11 +00:00
Lasse Folger
6bec1ef16e proto: move explicit test as a seed into the fuzz test
fuzztest seeds are executed as part of `go test` so there is no need for
an explicit test and having this as a seed in the fuzztest is sufficient
to get test coverage for this case.

Change-Id: Ia4a2d721e544e1a1ad0ad3ef9aa9d0af6bfe2db8
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/567755
Auto-Submit: Lasse Folger <lassefolger@google.com>
Reviewed-by: Michael Stapelberg <stapelberg@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2024-02-28 15:54:53 +00:00
Lasse Folger
da0e6634fb proto: add editions tests for invalid messages
Change-Id: I97dff474afa7465ffbf1ac554cad8127bd4717bf
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/565975
Reviewed-by: Michael Stapelberg <stapelberg@google.com>
Auto-Submit: Lasse Folger <lassefolger@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2024-02-22 08:59:49 +00:00
Lasse Folger
11de0a6409 all: add editions support for extensions
Before this change the implementation would ignore editions options set on
extension declarations.
This change also add much more coverage for valid messages.

Change-Id: I0e7029b1430a39859cdd4a93667ac20c7e0ff20e
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/565396
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Michael Stapelberg <stapelberg@google.com>
Auto-Submit: Lasse Folger <lassefolger@google.com>
2024-02-22 08:53:07 +00:00
Lasse Folger
7d98b0e8ab all: implement features.field_presence = LEGACY_REQUIRED support
Change-Id: Iaba3f4b81ac1b73f7542397a479642d081152c39
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/565656
Auto-Submit: Lasse Folger <lassefolger@google.com>
Reviewed-by: Michael Stapelberg <stapelberg@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2024-02-21 14:39:44 +00:00
Lasse Folger
6e0d73fd79 proto: add extension tests for editions protos
Change-Id: Ib3a00d8bf6fbf4841959ad9099fdc7b0d59ee0d3
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/565655
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Lasse Folger <lassefolger@google.com>
Reviewed-by: Michael Stapelberg <stapelberg@google.com>
2024-02-21 09:19:43 +00:00
Lasse Folger
194457763e proto: add more editions tests for proto.Size and proto.Equal
Change-Id: I8c7065d413b3a8d90ef25a2f5b9b9868c18906ec
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/565395
Auto-Submit: Lasse Folger <lassefolger@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Michael Stapelberg <stapelberg@google.com>
2024-02-21 08:36:29 +00:00
Lasse Folger
5e8da94149 proto: add decoding fuzz test for proto3 to editions conversion
Change-Id: Ic2536373eeb5c814059ddf5c7c0d675d0e06153c
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/564817
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Michael Stapelberg <stapelberg@google.com>
Auto-Submit: Lasse Folger <lassefolger@google.com>
2024-02-20 10:20:29 +00:00
Lasse Folger
8f6a6615d6 proto: add fuzztest for proto2 and equivalent editions proto
This fixes more minor issue when constructing the descriptor in that
delimited encoded message fields are not considered properly as groups.

Change-Id: I714a227a0f8d256fa7430c526844e8e94acdda33
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/564816
Reviewed-by: Michael Stapelberg <stapelberg@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Lasse Folger <lassefolger@google.com>
2024-02-20 09:18:14 +00:00
Lasse Folger
f102ec6532 all: add more test for editions and fix some bugs in the implementation
There are several minor fixes:
* fix the unmarshaler for the field options (it used the wrong field numbers).
* fix the unmarshaler to always initialize the file options with the appropriate defaults.
* fix the coder selection to work properly for fields with implicit presence tracking (they used the coders for explicit tracking).
* fix the dynamic message created from a descriptor for an editions using proto to use HasPresence instead of a syntax check for proto3

Change-Id: Ic13bc22a71aa9f93b476e5edd650d4d2ab5dcb98
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/564455
Auto-Submit: Michael Stapelberg <stapelberg@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Michael Stapelberg <stapelberg@google.com>
2024-02-16 14:47:55 +00:00
Joe Tsai
f9212a8dfa all: modernize documentation
Modernize the documentation across the entire module
to make use of the newer ability to linkify declarations.

Change-Id: I440f9a3f025ec6fadfd9cba59b1dfb57028bf3f1
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/309430
Reviewed-by: Michael Stapelberg <stapelberg@google.com>
Reviewed-by: Damien Neil <dneil@google.com>
2023-09-05 14:55:28 +00:00
yahuichen
fc47fdd3d3 proto: store the size of tag to avoid multiple calculations
Change-Id: I001c2c6996e92d69234431f3463b5558840da948
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/500695
Reviewed-by: Lasse Folger <lassefolger@google.com>
Reviewed-by: Michael Stapelberg <stapelberg@google.com>
2023-06-06 12:13:21 +00:00
Oleksandr Redko
49eaa78c6c all: update links to Protocol Buffer documentation
https://developers.google.com/protocol-buffers/ states that
this site will be deprecated January 2023 and provides
https://protobuf.dev/ as a link to the new location.

Generated .pb.go files are created by generate.bash using Go 1.18,
so leave them unchanged for now.

Fixes golang/protobuf#1520.

Change-Id: I1ddf6f329c96ba05d7c7d8562ad2945f479ee09b
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/466375
Reviewed-by: Michael Stapelberg <stapelberg@google.com>
Reviewed-by: Lasse Folger <lassefolger@google.com>
2023-02-08 13:52:20 +00:00
Christian Höppner
e831c33179 proto: document Equal behavior of invalid messages
The doc comment of proto.Equal now explicitly describes the behavior
in the case when one message is valid and other is invalid.

Change-Id: Ifeba40277c7da25193d46ad496e910f9b63eb795
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/464275
Reviewed-by: Joseph Tsai <joetsai@digital-static.net>
Reviewed-by: Chressie Himpel <chressie@google.com>
2023-02-07 08:52:30 +00:00
Joe Tsai
b2a7dfe48b reflect/protoreflect: add Value.Equal method
The Value.Equal method compares Values in a way that is
analogous to the reflect.DeepEqual function.

Most of the implementation is moved from the "proto" package,
with proto.Equal implemented in terms of protoreflect.Value.Equal.

Change-Id: Ie9d5f6c073dc49fa59f78385c441db8137de5ec5
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/450775
TryBot-Bypass: Heschi Kreinick <heschi@google.com>
Reviewed-by: Damien Neil <dneil@google.com>
Reviewed-by: Oleksii Prokopchuk <prokopchuk@google.com>
Reviewed-by: Christian Höppner <hoeppi@google.com>
Run-TryBot: Lasse Folger <lassefolger@google.com>
2022-12-01 15:36:18 +00:00
Damien Neil
f0e23c7a8f proto: revert expose package-private equalField
This reverts commit 4d6d2135eb.

Post-submit discussion on CL 448876 suggests that this API should
change; revert it pending a decision.

Change-Id: I2e81be847c62ea7de5dfef36bb10ab193132e612
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/450955
Reviewed-by: Michael Stapelberg <stapelberg@google.com>
Run-TryBot: Damien Neil <dneil@google.com>
Reviewed-by: Joseph Tsai <joetsai@digital-static.net>
Reviewed-by: Lasse Folger <lassefolger@google.com>
Run-TryBot: Lasse Folger <lassefolger@google.com>
TryBot-Bypass: Michael Stapelberg <stapelberg@google.com>
2022-11-16 08:31:27 +00:00
Oleksii Prokopchuk
4d6d2135eb proto: expose package-private equalField.
There is a demand for comparing protobuf.Value-s.
One use case is when one wants to compare just one field via reflection.
Another is when one wants to iterate over fields and check for diffs.
Existing diff packages are recommended only for tests as they panic on error.
Exposing equalField will prevent excessive branching of proto/equal.go code.

Change-Id: Iec8843dae96c9ae3c45858e8b97e7aa963473ce2
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/448876
Reviewed-by: Michael Stapelberg <stapelberg@google.com>
Run-TryBot: Michael Stapelberg <stapelberg@google.com>
TryBot-Bypass: Michael Stapelberg <stapelberg@google.com>
2022-11-14 16:01:29 +00:00
Damien Neil
b0a944684d all: reformat with go1.19 gofmt
Apply go1.19 gofmt to non-generated files.

Generated .pb.go files are created by generate.bash using Go 1.18,
so leave them unchanged for now.

Change-Id: Ied36c83cf99704988d059bf0412e677f0fbc71b0
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/418676
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Lasse Folger <lassefolger@google.com>
2022-07-21 16:01:13 +00:00
Damien Neil
5f429f7042 proto: fix compilation failure in tests
Change-Id: I61a84dffcf4ccc9f81dc8b9f932c315713e81142
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/418674
Reviewed-by: Lasse Folger <lassefolger@google.com>
2022-07-21 06:22:01 +00:00
Koichi Shiraishi
fc44d00d5a proto: use reflect.Ptr for backward compatibility
reflect.Pointer introduced on go1.18. Support 1.17 and earlier.

Change-Id: I62dcdb580a7976068e86df432bc44ee21c2cda81
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/412354
Reviewed-by: Damien Neil <dneil@google.com>
Reviewed-by: Joseph Tsai <joetsai@digital-static.net>
Reviewed-by: Lasse Folger <lassefolger@google.com>
2022-06-17 06:55:02 +00:00
Mitko Asenov
380c339ec1 proto: short-circuit Equal when inputs are identical
I added benchmarks (measured on Intel(R) Xeon(R) CPU E5-1650 v4 @ 3.60GHz) that show the difference:

name                                  old time/op  new time/op  delta
EqualWithSmallEmpty-12                 241ns ± 6%   242ns ± 6%     ~     (p=0.796 n=10+10)
EqualWithIdenticalPtrEmpty-12          241ns ± 3%     7ns ± 4%  -97.19%  (p=0.000 n=10+10)
EqualWithLargeEmpty-12                2.68µs ± 3%  2.59µs ± 3%   -3.27%  (p=0.000 n=10+10)
EqualWithDeeplyNestedEqual-12         73.9µs ± 3%  71.8µs ± 1%   -2.91%   (p=0.000 n=10+9)
EqualWithDeeplyNestedDifferent-12     20.0µs ± 5%  19.4µs ± 5%   -3.06%  (p=0.029 n=10+10)
EqualWithDeeplyNestedIdenticalPtr-12  73.9µs ± 4%   0.0µs ± 2%  -99.99%  (p=0.000 n=10+10)

Change-Id: I1b83fa477d6432eafd355b322f507cf90b9a6751
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/411377
Reviewed-by: Lasse Folger <lassefolger@google.com>
Reviewed-by: Joseph Tsai <joetsai@digital-static.net>
Reviewed-by: Michael Stapelberg <stapelberg@google.com>
2022-06-15 11:39:31 +00:00
Koichi Shiraishi
784c482554 all: remove shorthand import aliases
Change-Id: I62ec79419354a2c65ac69e4bcc1cc864a6a40a1a
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/407934
Reviewed-by: Lasse Folger <lassefolger@google.com>
Reviewed-by: Damien Neil <dneil@google.com>
2022-05-24 20:05:50 +00:00
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
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
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
Joe Tsai
174b9ecfe3 all: document that Unmarshal must be a mutable message
Fixes #937

Change-Id: I40b2678eba0195ed01676167f8e01e2fedea293b
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/302329
Trust: Joe Tsai <joetsai@digital-static.net>
Reviewed-by: Damien Neil <dneil@google.com>
2021-03-16 20:06:34 +00:00
Tzu-Chiao Yeh
f62736dede proto/equal: reduce equal scalar value allocation
Instead of lifting values to interface and then do equality test,
compare scalar values with defined types to reduce allocation
from interface.

Change-Id: Ieb777fbd1a48c83d896d0ebe6ad605433f44c16c
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/253100
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
Trust: Damien Neil <dneil@google.com>
Trust: Joe Tsai <thebrokentoaster@gmail.com>
2020-10-08 23:42:17 +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
1ecb1f411c proto: add MessageName helper
The MessageName returns the full name of a message.
It is a shorthand for:

	var name protoreflect.FullName
	if m != nil {
		name = m.ProtoReflect().Descriptor().FullName()
	}

Generally, we avoid the addition of helper functions unless
their use is justified. Arguments for this helper:

• The legacy proto.MessageName is widely used.
  For example, inside Google, there are thousands of usages of it.
  It is commonly used in logging and error construction to
  report the name of a protobuf message.

• The fact that a nil-check may be neccessary means that users
  either forget the nil-check and risk a nil-panic in production code,
  or users have to write cumbersome logic to check for it.

  For example, compare use with the helper:
      return fmt.Errorf("invalid message type: %v", proto.MessageName(m))
  to use without the helper:
      var messageName protoreflect.FullName
      if m != nil {
          messageName = m.ProtoReflect().Descriptor().FullName()
      }
      return fmt.Errorf("invalid message type: %v", messageName)

A point of consideration is whether this should return a string
or a protoreflect.FullName. This CL returns the latter type:

• Most uses of it are for logging via log.Printf or error construction
  via fmt.Errorf where it does not matter what the exact string type is
  since its formatted the same way.

• Another use of it is to check for message type equality by doing
  something similar to:
      if proto.MessageName(got) != proto.MessageName(want) {
          ...
      }
  in which case it still does not matter what the exact string type is.

• The other major use of proto.MessageName is to call proto APIs
  that actually do expect a protoreflect.FullName
  (e.g., protoregistry.GlobalTypes.FindExtensionByNumber) where
  the user currently type-casts the legacy proto.MessageName
  to a protoreflect.FullName anyways.

As such, there does not seem to be much justified use for MessageName
as a string. The rare cases that need a string can trivially type cast
it to a string.

Change-Id: I840758df828eef72e7e63620569363a496366afa
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/242377
Reviewed-by: Damien Neil <dneil@google.com>
2020-07-23 23:43:59 +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
467a9cdc10 all: fix tests for 32-bit architectures
Fix the tests to work on GOARCH=386.
Also add another test suite that tests on GOARCH=386.

Change-Id: I41290998053dc059347adb8af70012dc5cc31811
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/241537
Reviewed-by: Herbie Ong <herbie@google.com>
2020-07-08 22:37:09 +00:00
Joe Tsai
92679665d7 internal/order: add a package for ordered iteration over messages and maps
The order package replaces the mapsort and fieldsort packages.
It presents a common API for ordered iteration over message fields
and map fields.

It has a number of pre-defined orderings.

Change-Id: Ie6cd423da30b4757864c352cb04454f21fe07ee2
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/239837
Reviewed-by: Herbie Ong <herbie@google.com>
2020-07-01 18:57:38 +00:00
Joe Tsai
e0b77db13b internal/genid: add new package for generated identifiers
The genid package unifies the genname, fieldnum, and detectknown
packages into a single package.

Whenever possible use the generated constants rather than
hard-coded literals. This makes it easier to search the entire
module for special logic that deal with well-known types.

Change-Id: I13beff1f4149444a0c0b9e607ebf759657f000f4
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/235301
Reviewed-by: Herbie Ong <herbie@google.com>
2020-05-29 07:08:23 +00:00
Joe Tsai
118baf6390 all: funnel similar functionality through a single function
Some companies (e.g., Google) run a profiling service where they may
choose to special-case certain symbols in a binary to classify
commonly used libraries like protobufs.

This CL funnels similar functionality through a single function
so that they can be more easily identified. This is by no means a
firm statement that these identifiers will never change names,
but at least the code documents warnings to avoid changing the
name of certain identifiers.

This CL provides the following semi-stable symbol names:
	"google.golang.org/protobuf/proto".MarshalOptions.size
	"google.golang.org/protobuf/proto".MarshalOptions.marshal
	"google.golang.org/protobuf/proto".UnmarshalOptions.unmarshal
	"google.golang.org/protobuf/encoding/prototext".MarshalOptions.marshal
	"google.golang.org/protobuf/encoding/prototext".UnmarshalOptions.unmarshal
	"google.golang.org/protobuf/encoding/protojson".MarshalOptions.marshal
	"google.golang.org/protobuf/encoding/protojson".UnmarshalOptions.unmarshal

Merge and Clone are not part of the above set since there is a
possibility that MergeOptions will be added in the future.

We use an unexported method so that we have the freedom to change the
method however we want since profilers do not care about that.

Change-Id: Ia79af260d00125f48139420e1e18a86482bd1829
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/234079
Reviewed-by: Damien Neil <dneil@google.com>
2020-05-15 18:21:49 +00:00
Joe Tsai
1f5b6fe645 all: improve panic messages for better debugability
Change-Id: If3e505e715d5ce2c9a81249c868d26226a25b724
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/232339
Reviewed-by: Damien Neil <dneil@google.com>
2020-05-05 21:10:54 +00:00
Joe Tsai
d0a499bc65 internal/impl: validate UTF-8 for proto3 optional strings
Change-Id: I090e7c5adac47818831c63d3d999cb7fea5ac696
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/231357
Reviewed-by: Damien Neil <dneil@google.com>
2020-05-01 17:23:32 +00:00
Joe Tsai
387873dd53 all: implement support for proto3 optional semantics
In the upcoming 3.12.x release of protoc, the proto3 language will be
amended to support true presence for scalars. This CL adds support
to both the generator and runtime to support these semantics.

Newly added public API:
	protogen.Plugin.SupportedFeatures
	protoreflect.FieldDescriptor.HasPresence
	protoreflect.FieldDescriptor.HasOptionalKeyword
	protoreflect.OneofDescriptor.IsSynthetic

Change-Id: I7c86bf66d0ae56642109beb5f2132184593747ad
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/230698
Reviewed-by: Damien Neil <dneil@google.com>
2020-04-29 20:02:24 +00:00
Joe Tsai
b57aae9def all: improve extension validation
Changes made:
* Ensure protoreflect.ExtensionType.IsValidInterface never panics,
especially if given a nil interface value.
* Have protoreflect.ExtensionType.IsValid{Interface,Value} only
perform type-checks. It does not do value checks (i.e., whether the
value itself is valid). Value validity is left to when an actual
protoreflect.Message.Set operation is performed.
* Add special-casing on proto.SetExtension to treat an invalid
message or list as functionally equivalent to Clear. This is to
be more consistent with the legacy SetExtension implementation
which never panicked when given such values.
* Add special-casing on proto.HasExtension to treat a mismatched
extension descriptor as simply not being present in the message.
This is also to be more consistent with the legacy HasExtension
implementation which did the same thing.

Change-Id: Idf0419abf27b9f85d9b92bd2ff8088e25b7990cc
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/229558
Reviewed-by: Damien Neil <dneil@google.com>
2020-04-23 06:01:13 +00:00
Joe Tsai
33f8c03eac proto: add RangeExtensions and adjust HasExtension and GetExtension
Two changes:
* Add RangeExtensions as a more suitable replacement for legacy
proto.ClearExtensions and proto.ExtensionDescs functions.
* Make HasExtension and GetExtension treat nil message interface
as an empty message to more consistently match legacy behavior.

Change-Id: I8eb1887a33d0737f2f80a2b80358cc296087ba3b
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/229157
Reviewed-by: Damien Neil <dneil@google.com>
2020-04-21 00:46:21 +00:00
Joe Tsai
a732b3c77a proto: never return nil []byte from Marshal when successful
It is a sufficiently common pattern to do something like the following:
	m.Proto2BytesField, ... = proto.Marshal(m2)

where the user is relying on Marshal to never return a nil byte slice,
otherwise it subtly changes the semantics of how the generated API
handles whether a proto2 "optional bytes" fields is populated or not.

Change-Id: Ie7508dbcdcc5f3295885609a91907c6eb4f04c1e
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/228838
Reviewed-by: Damien Neil <dneil@google.com>
2020-04-20 23:39:16 +00:00
Joe Tsai
8cfc14f022 all: consistently treat nil message interface as an empty read-only message
To assist users in migrating from github.com/golang/protobuf
to google.golang.org/protobuf, make it such that functiionality like
proto.Marshal doesn't panic on nil interfaces.

Similar to how the new implementation treats a typed nil message
as an empty message, we treat a nil interface as being equivalent
to an "untyped" empty message.

Change-Id: Ic037f386f855b122f732b34d370e524b7c0d76f1
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/228837
Reviewed-by: Damien Neil <dneil@google.com>
2020-04-20 23:12:56 +00:00
Joe Tsai
cfd80493c5 testing/protopack: make package publicly available
Change-Id: I342ed27df17867f18c58e60880bcac5a31a3096b
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/219837
Reviewed-by: Damien Neil <dneil@google.com>
2020-03-20 18:05:51 +00:00
Joe Tsai
cd108d00a8 encoding/protowire: make package publicly available
Change-Id: I95e293c208e787a91d50e29817620535dfeaa7f2
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/219838
Reviewed-by: Damien Neil <dneil@google.com>
2020-03-20 07:42:18 +00:00
Damien Neil
d3874051d7 internal/encoding/messageset: don't modify input data when unmarshaling
When combining multiple message fields in a MessageSet item (a case
which should never happen in practice), unmarshal could modify the input
data. Fix it to not do so. Add a general check to ensure that unmarshal
operations don't modify the input.

Change-Id: Idde46e6132a1dc96c374f9146efff81783c3bef3
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/223818
Reviewed-by: Joe Tsai <joetsai@google.com>
2020-03-18 05:27:34 +00:00