1
0
Fork 0

Don't leak a database if deserialization fails

Fixes https://github.com/rqlite/rqlite/issues/837.
master
Philip O'Toole 3 years ago
parent 1e4e1e06e3
commit 911df4c03b

@ -139,7 +139,7 @@ func LoadInMemoryWithDSN(dbPath, dsn string) (*DB, error) {
// DeserializeInMemoryWithDSN loads an in-memory database with that contained
// in the byte slide, with the specified DSN. The byte slice must not be changed
// or garbage-collected until after this function returns.
func DeserializeInMemoryWithDSN(b []byte, dsn string) (*DB, error) {
func DeserializeInMemoryWithDSN(b []byte, dsn string) (retDB *DB, retErr error) {
tmpDB, err := OpenInMemoryWithDSN(dsn)
if err != nil {
return nil, fmt.Errorf("DeserializeInMemoryWithDSN: %s", err.Error())
@ -173,6 +173,12 @@ func DeserializeInMemoryWithDSN(b []byte, dsn string) (*DB, error) {
if err != nil {
return nil, fmt.Errorf("DeserializeInMemoryWithDSN: %s", err.Error())
}
defer func() {
// Don't leak a database if deserialization fails.
if retErr != nil {
db.Close()
}
}()
if err := copyDatabase(db, tmpDB); err != nil {
return nil, fmt.Errorf("DeserializeInMemoryWithDSN: %s", err.Error())

Loading…
Cancel
Save