golang/go

encoding/pem: can't decode encoded message

dvyukov opened this issue · 3 comments

encoding/pem successfully decodes a message in the following program; but if the message is re-encoded, decoding fails:

package main

import "encoding/pem"

func main() {
    data := []byte("-----BEGIN foo-----\n\n-----END foo-----")
    b, _ := pem.Decode(data)
    if b == nil {
        return
    }
    enc := pem.EncodeToMemory(b)
    b1, _ := pem.Decode(enc)
    if b1 == nil {
        panic("can't decode encoded")
    }
}

Either the first Decode should fail, or the second Decode should succeed.

on commit 6551803

CL https://golang.org/cl/10487 mentions this issue.

EncodeToMemory produces "-----BEGIN foo-----\n-----END foo-----" (notice the single newline compared to the original input). The pem.Decode function mistakes the line starting with "\n-----END foo-----" for being a header line, and subsequently does not find an END marker for the block.

CL https://golang.org/cl/10516 mentions this issue.