Defer close calls in save data reader functions

fasthttp
maru 2024-04-10 15:39:53 -04:00
parent 1b5da99cda
commit 9a0d4a2698
No known key found for this signature in database
GPG Key ID: 37689350E9CD0F0D
1 changed files with 8 additions and 0 deletions

View File

@ -19,11 +19,15 @@ func readSystemSaveData(uuid []byte) (defs.SystemSaveData, error) {
return system, fmt.Errorf("failed to open save file: %s", err) return system, fmt.Errorf("failed to open save file: %s", err)
} }
defer file.Close()
zstdDecoder, err := zstd.NewReader(file) zstdDecoder, err := zstd.NewReader(file)
if err != nil { if err != nil {
return system, fmt.Errorf("failed to create zstd decoder: %s", err) return system, fmt.Errorf("failed to create zstd decoder: %s", err)
} }
defer zstdDecoder.Close()
err = gob.NewDecoder(zstdDecoder).Decode(&system) err = gob.NewDecoder(zstdDecoder).Decode(&system)
if err != nil { if err != nil {
return system, fmt.Errorf("failed to deserialize save: %s", err) return system, fmt.Errorf("failed to deserialize save: %s", err)
@ -45,11 +49,15 @@ func readSessionSaveData(uuid []byte, slotID int) (defs.SessionSaveData, error)
return session, fmt.Errorf("failed to open save file: %s", err) return session, fmt.Errorf("failed to open save file: %s", err)
} }
defer file.Close()
zstdDecoder, err := zstd.NewReader(file) zstdDecoder, err := zstd.NewReader(file)
if err != nil { if err != nil {
return session, fmt.Errorf("failed to create zstd decoder: %s", err) return session, fmt.Errorf("failed to create zstd decoder: %s", err)
} }
defer zstdDecoder.Close()
err = gob.NewDecoder(zstdDecoder).Decode(&session) err = gob.NewDecoder(zstdDecoder).Decode(&session)
if err != nil { if err != nil {
return session, fmt.Errorf("failed to deserialize save: %s", err) return session, fmt.Errorf("failed to deserialize save: %s", err)