]> Cypherpunks.ru repositories - gostls13.git/commitdiff
encoding: reject negative runes in Encoding.WithPadding
authorJoe Tsai <joetsai@digital-static.net>
Thu, 22 Jun 2023 18:03:17 +0000 (11:03 -0700)
committerGopher Robot <gobot@golang.org>
Thu, 17 Aug 2023 16:32:08 +0000 (16:32 +0000)
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 <joetsai@digital-static.net>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Joseph Tsai <joetsai@digital-static.net>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Run-TryBot: Ian Lance Taylor <iant@google.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
src/encoding/base32/base32.go
src/encoding/base64/base64.go

index 7cccbd17be3222f54a6cc6f3b26557d788a71a65..6e2360790ab92067c1fd9ec428ebbfd709e00da8 100644 (file)
@@ -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")
        }
 
index 5db72b91e26d35749e02c338514b2cbfaedf7817..28ed7a012375c6356207a7c5e31da025ed6698f2 100644 (file)
@@ -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")
        }