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>
This commit is contained in:
Kir Kolyshkin 2022-05-16 13:43:12 -07:00 committed by Damien Neil
parent a0482351ba
commit 563f06fbeb
2 changed files with 8 additions and 0 deletions

View File

@ -655,7 +655,11 @@ func consume(b []byte, n int) []byte {
// errId extracts a byte sequence that looks like an invalid ID
// (for the purposes of error reporting).
func errId(seq []byte) []byte {
const maxLen = 32
for i := 0; i < len(seq); {
if i > maxLen {
return append(seq[:i:i], "…"...)
}
r, size := utf8.DecodeRune(seq[i:])
if r > utf8.RuneSelf || (r != '/' && isDelim(byte(r))) {
if i == 0 {

View File

@ -404,6 +404,10 @@ func TestDecoder(t *testing.T) {
in: "\uFFFDxxx",
want: []R{{E: "invalid field name: \uFFFD"}},
},
{
in: "-a234567890123456789012345678901234567890abc",
want: []R{{E: "invalid field name: -a2345678901234567890123456789012…"}},
},
{
in: "[type]",
want: []R{