Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

openpgp/*: properly invoke .Close on errors #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions openpgp/clearsign/clearsign.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,9 +265,9 @@ func (d *dashEscaper) Write(data []byte) (n int, err error) {
return
}

func (d *dashEscaper) Close() (err error) {
func (d *dashEscaper) Close() (rerr error) {
if !d.atBeginningOfLine {
if err = d.buffered.WriteByte(lf); err != nil {
if err := d.buffered.WriteByte(lf); err != nil {
return
}
}
Expand All @@ -276,6 +276,11 @@ func (d *dashEscaper) Close() (err error) {
if err != nil {
return
}
defer func() {
if rerr != nil {
out.Close()
}
}()

t := d.config.Now()
for i, k := range d.privateKeys {
Expand All @@ -297,10 +302,7 @@ func (d *dashEscaper) Close() (err error) {
if err = out.Close(); err != nil {
return
}
if err = d.buffered.Flush(); err != nil {
return
}
return
return d.buffered.Flush()
}

// Encode returns a WriteCloser which will clear-sign a message with privateKey
Expand Down
7 changes: 6 additions & 1 deletion openpgp/packet/compressed.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,16 @@ func (cwc compressedWriteCloser) Close() (err error) {
// can be written and which MUST be closed on completion. If cc is
// nil, sensible defaults will be used to configure the compression
// algorithm.
func SerializeCompressed(w io.WriteCloser, algo CompressionAlgo, cc *CompressionConfig) (literaldata io.WriteCloser, err error) {
func SerializeCompressed(w io.WriteCloser, algo CompressionAlgo, cc *CompressionConfig) (literaldata io.WriteCloser, rerr error) {
compressed, err := serializeStreamHeader(w, packetTypeCompressed)
if err != nil {
return
}
defer func() {
if rerr != nil {
compressed.Close()
}
}()

_, err = compressed.Write([]byte{uint8(algo)})
if err != nil {
Expand Down
7 changes: 6 additions & 1 deletion openpgp/packet/literal.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (l *LiteralData) parse(r io.Reader) (err error) {
// SerializeLiteral serializes a literal data packet to w and returns a
// WriteCloser to which the data itself can be written and which MUST be closed
// on completion. The fileName is truncated to 255 bytes.
func SerializeLiteral(w io.WriteCloser, isBinary bool, fileName string, time uint32) (plaintext io.WriteCloser, err error) {
func SerializeLiteral(w io.WriteCloser, isBinary bool, fileName string, time uint32) (plaintext io.WriteCloser, rerr error) {
var buf [4]byte
buf[0] = 't'
if isBinary {
Expand All @@ -69,6 +69,11 @@ func SerializeLiteral(w io.WriteCloser, isBinary bool, fileName string, time uin
if err != nil {
return
}
defer func() {
if rerr != nil {
inner.Close()
}
}()

_, err = inner.Write(buf[:2])
if err != nil {
Expand Down
7 changes: 6 additions & 1 deletion openpgp/packet/symmetrically_encrypted.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ func (c noOpCloser) Close() error {
// to w and returns a WriteCloser to which the to-be-encrypted packets can be
// written.
// If config is nil, sensible defaults will be used.
func SerializeSymmetricallyEncrypted(w io.Writer, c CipherFunction, key []byte, config *Config) (contents io.WriteCloser, err error) {
func SerializeSymmetricallyEncrypted(w io.Writer, c CipherFunction, key []byte, config *Config) (contents io.WriteCloser, rerr error) {
if c.KeySize() != len(key) {
return nil, errors.InvalidArgumentError("SymmetricallyEncrypted.Serialize: bad key length")
}
Expand All @@ -262,6 +262,11 @@ func SerializeSymmetricallyEncrypted(w io.Writer, c CipherFunction, key []byte,
if err != nil {
return
}
defer func() {
if rerr != nil {
ciphertext.Close()
}
}()

_, err = ciphertext.Write([]byte{symmetricallyEncryptedVersion})
if err != nil {
Expand Down