Skip to content

Commit 6c431fa

Browse files
dsnetgopherbot
authored andcommitted
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 <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Joseph Tsai <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]>
1 parent 56076c3 commit 6c431fa

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

src/encoding/base32/base32.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,13 @@ var HexEncoding = NewEncoding(encodeHex)
7979

8080
// WithPadding creates a new encoding identical to enc except
8181
// with a specified padding character, or NoPadding to disable padding.
82-
// The padding character must not be '\r' or '\n', must not
83-
// be contained in the encoding's alphabet and must be a rune equal or
84-
// below '\xff'.
82+
// The padding character must not be '\r' or '\n',
83+
// must not be contained in the encoding's alphabet,
84+
// must not be negative, and must be a rune equal or below '\xff'.
8585
// Padding characters above '\x7f' are encoded as their exact byte value
8686
// rather than using the UTF-8 representation of the codepoint.
8787
func (enc Encoding) WithPadding(padding rune) *Encoding {
88-
if padding == '\r' || padding == '\n' || padding > 0xff {
88+
if padding < NoPadding || padding == '\r' || padding == '\n' || padding > 0xff {
8989
panic("invalid padding")
9090
}
9191

src/encoding/base64/base64.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,13 @@ func NewEncoding(encoder string) *Encoding {
8282

8383
// WithPadding creates a new encoding identical to enc except
8484
// with a specified padding character, or NoPadding to disable padding.
85-
// The padding character must not be '\r' or '\n', must not
86-
// be contained in the encoding's alphabet and must be a rune equal or
87-
// below '\xff'.
85+
// The padding character must not be '\r' or '\n',
86+
// must not be contained in the encoding's alphabet,
87+
// must not be negative, and must be a rune equal or below '\xff'.
8888
// Padding characters above '\x7f' are encoded as their exact byte value
8989
// rather than using the UTF-8 representation of the codepoint.
9090
func (enc Encoding) WithPadding(padding rune) *Encoding {
91-
if padding == '\r' || padding == '\n' || padding > 0xff {
91+
if padding < NoPadding || padding == '\r' || padding == '\n' || padding > 0xff {
9292
panic("invalid padding")
9393
}
9494

0 commit comments

Comments
 (0)