From: Joe Tsai Date: Thu, 22 Jun 2023 18:03:17 +0000 (-0700) Subject: encoding: reject negative runes in Encoding.WithPadding X-Git-Tag: go1.22rc1~1245 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=6c431fab3701b5f07357a358ab232062353f5bed;p=gostls13.git encoding: reject negative runes in Encoding.WithPadding A negative rune (other than NoPadding) makes no semantic sense. Doing so relies on integer overflow of converting a rune to a byte and would thus be equivalent to passing the positive byte value of byte(padding). This may cause existing code to panic. An alternative is treat negative runes as equivalent to NoPadding. However, the code already panics to report erroneous padding values, so this is in line with the existing API. Change-Id: I02499705519581598adc0c8525d90e25278dc056 Reviewed-on: https://go-review.googlesource.com/c/go/+/505236 Auto-Submit: Joseph Tsai TryBot-Result: Gopher Robot Run-TryBot: Joseph Tsai Reviewed-by: Ian Lance Taylor Run-TryBot: Ian Lance Taylor Reviewed-by: Dmitri Shuralyov --- diff --git a/src/encoding/base32/base32.go b/src/encoding/base32/base32.go index 7cccbd17be..6e2360790a 100644 --- a/src/encoding/base32/base32.go +++ b/src/encoding/base32/base32.go @@ -79,13 +79,13 @@ var HexEncoding = NewEncoding(encodeHex) // WithPadding creates a new encoding identical to enc except // with a specified padding character, or NoPadding to disable padding. -// The padding character must not be '\r' or '\n', must not -// be contained in the encoding's alphabet and must be a rune equal or -// below '\xff'. +// The padding character must not be '\r' or '\n', +// must not be contained in the encoding's alphabet, +// must not be negative, and must be a rune equal or below '\xff'. // Padding characters above '\x7f' are encoded as their exact byte value // rather than using the UTF-8 representation of the codepoint. func (enc Encoding) WithPadding(padding rune) *Encoding { - if padding == '\r' || padding == '\n' || padding > 0xff { + if padding < NoPadding || padding == '\r' || padding == '\n' || padding > 0xff { panic("invalid padding") } diff --git a/src/encoding/base64/base64.go b/src/encoding/base64/base64.go index 5db72b91e2..28ed7a0123 100644 --- a/src/encoding/base64/base64.go +++ b/src/encoding/base64/base64.go @@ -82,13 +82,13 @@ func NewEncoding(encoder string) *Encoding { // WithPadding creates a new encoding identical to enc except // with a specified padding character, or NoPadding to disable padding. -// The padding character must not be '\r' or '\n', must not -// be contained in the encoding's alphabet and must be a rune equal or -// below '\xff'. +// The padding character must not be '\r' or '\n', +// must not be contained in the encoding's alphabet, +// must not be negative, and must be a rune equal or below '\xff'. // Padding characters above '\x7f' are encoded as their exact byte value // rather than using the UTF-8 representation of the codepoint. func (enc Encoding) WithPadding(padding rune) *Encoding { - if padding == '\r' || padding == '\n' || padding > 0xff { + if padding < NoPadding || padding == '\r' || padding == '\n' || padding > 0xff { panic("invalid padding") }