]> Cypherpunks repositories - gostls13.git/commitdiff
[release-branch.go1.24] encoding/pem: properly calculate end indexes
authorRoland Shoemaker <roland@golang.org>
Thu, 23 Oct 2025 15:16:39 +0000 (08:16 -0700)
committerMichael Knyszek <mknyszek@google.com>
Wed, 29 Oct 2025 16:22:33 +0000 (09:22 -0700)
When a block is missing the END line trailer, calculate the indexes of
the end and end trailer _before_ continuing the loop, making the
reslicing at the start of the loop work as expected.

Fixes #76028

Change-Id: If45c8cb473315623618f02cc7609f517a72d232d
Reviewed-on: https://go-review.googlesource.com/c/go/+/714200
Auto-Submit: Roland Shoemaker <roland@golang.org>
Reviewed-by: Damien Neil <dneil@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
(cherry picked from commit 839da71f8907ac4434299db4353db31835c916df)
Reviewed-on: https://go-review.googlesource.com/c/go/+/714680
Reviewed-by: David Chase <drchase@google.com>
src/encoding/pem/pem.go
src/encoding/pem/pem_test.go

index 53e0069709d523fb6ab2f9493d66fae038425111..063df251512896187625471789137ef00260a144 100644 (file)
@@ -95,6 +95,9 @@ func Decode(data []byte) (p *Block, rest []byte) {
        for {
                // If we've already tried parsing a block, skip past the END we already
                // saw.
+               if endTrailerIndex < 0 || endTrailerIndex > len(rest) {
+                       return nil, data
+               }
                rest = rest[endTrailerIndex:]
 
                // Find the first END line, and then find the last BEGIN line before
@@ -116,11 +119,11 @@ func Decode(data []byte) (p *Block, rest []byte) {
                var typeLine []byte
                var consumed int
                typeLine, rest, consumed = getLine(rest)
+               endIndex -= consumed
+               endTrailerIndex -= consumed
                if !bytes.HasSuffix(typeLine, pemEndOfLine) {
                        continue
                }
-               endIndex -= consumed
-               endTrailerIndex -= consumed
                typeLine = typeLine[0 : len(typeLine)-len(pemEndOfLine)]
 
                p = &Block{
index 5bdc2f66a7b3abb377ec0680b89bfc74e9cccfb1..fa6e8ba62bdb8780c4d2fd1039c1a9ff44284948 100644 (file)
@@ -736,3 +736,7 @@ func FuzzDecode(f *testing.F) {
                Decode(data)
        })
 }
+
+func TestMissingEndTrailer(t *testing.T) {
+       Decode([]byte{0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x42, 0x45, 0x47, 0x49, 0x4e, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0xa, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x45, 0x4e, 0x44, 0x20})
+}