Skip to content

Commit 839da71

Browse files
rolandshoemakergopherbot
authored andcommitted
encoding/pem: properly calculate end indexes
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. Change-Id: If45c8cb473315623618f02cc7609f517a72d232d Reviewed-on: https://go-review.googlesource.com/c/go/+/714200 Auto-Submit: Roland Shoemaker <[email protected]> Reviewed-by: Damien Neil <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 39ed968 commit 839da71

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

src/encoding/pem/pem.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,11 @@ func Decode(data []byte) (p *Block, rest []byte) {
116116
var typeLine []byte
117117
var consumed int
118118
typeLine, rest, consumed = getLine(rest)
119+
endIndex -= consumed
120+
endTrailerIndex -= consumed
119121
if !bytes.HasSuffix(typeLine, pemEndOfLine) {
120122
continue
121123
}
122-
endIndex -= consumed
123-
endTrailerIndex -= consumed
124124
typeLine = typeLine[0 : len(typeLine)-len(pemEndOfLine)]
125125

126126
p = &Block{

src/encoding/pem/pem_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,3 +736,7 @@ func FuzzDecode(f *testing.F) {
736736
Decode(data)
737737
})
738738
}
739+
740+
func TestMissingEndTrailer(t *testing.T) {
741+
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})
742+
}

0 commit comments

Comments
 (0)