Defer statements could possibly void stored error values (#798)

This commit is contained in:
Frederik Ring authored and GitHub committed 2026-06-16 09:28:56 +02:00
1 parent d5a5746f17
commit 10f588dbc6
5 files changed
+11 -7

No files matched your search

+2 -1
View File
@@ -8,6 +8,7 @@ package main
import (
"archive/tar"
"errors"
"fmt"
"io"
"os"
@@ -164,7 +165,7 @@ func writeTarball(path string, tarWriter *tar.Writer, prefix string) (returnErr
return
}
defer func() {
returnErr = file.Close()
returnErr = errors.Join(returnErr, file.Close())
}()
_, err = io.Copy(tarWriter, file)
+2 -1
View File
@@ -3,6 +3,7 @@ package dropbox
import (
"bytes"
"context"
"errors"
"fmt"
"net/url"
"os"
@@ -111,7 +112,7 @@ func (b *dropboxStorage) Copy(file string) (returnErr error) {
return
}
defer func() {
returnErr = r.Close()
returnErr = errors.Join(returnErr, r.Close())
}()
// Start new upload session and get session id
+2 -1
View File
@@ -5,6 +5,7 @@ package googledrive
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
@@ -97,7 +98,7 @@ func (b *googleDriveStorage) Copy(file string) (returnErr error) {
return
}
defer func() {
returnErr = f.Close()
returnErr = errors.Join(returnErr, f.Close())
}()
driveFile := &drive.File{Name: name}
+1 -1
View File
@@ -155,7 +155,7 @@ func copyFile(src, dst string) (returnErr error) {
return
}
defer func() {
returnErr = in.Close()
returnErr = errors.Join(returnErr, in.Close())
}()
out, err := os.Create(dst)
+4 -3
View File
@@ -110,7 +110,8 @@ func (b *sshStorage) Name() string {
// Copy copies the given file to the SSH storage backend.
func (b *sshStorage) Copy(file string) (returnErr error) {
if err := b.sftpClient.MkdirAll(b.DestinationPath); err != nil {
return errwrap.Wrap(err, "error ensuring destination directory")
returnErr = errwrap.Wrap(err, "error ensuring destination directory")
return
}
source, err := os.Open(file)
@@ -120,7 +121,7 @@ func (b *sshStorage) Copy(file string) (returnErr error) {
return
}
defer func() {
returnErr = source.Close()
returnErr = errors.Join(returnErr, source.Close())
}()
destination, err := b.sftpClient.Create(path.Join(b.DestinationPath, name))
@@ -129,7 +130,7 @@ func (b *sshStorage) Copy(file string) (returnErr error) {
return
}
defer func() {
returnErr = destination.Close()
returnErr = errors.Join(returnErr, destination.Close())
}()
chunk := make([]byte, 1e9)