From d7b970a4c4325fd93a0c5347b18d7f40b7a253c4 Mon Sep 17 00:00:00 2001 From: Paul Holzinger Date: Tue, 28 Nov 2023 13:48:30 +0100 Subject: [PATCH] sqlite: fix issue in ValidateDBConfig() If a transaction is started it must either be committed or rolled back. The function uses defer to call `tx.Rollback()` if there is an error returned. However it also called `tx.Commit()` and afterwards further errors can be returned which means it tries to roll back a already committed transaction which cannot work. This fix is to make sure tx.Commit() is the last call in that function. see https://github.com/containers/podman/issues/20731 [NO NEW TESTS NEEDED] Signed-off-by: Paul Holzinger --- libpod/sqlite_state.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/libpod/sqlite_state.go b/libpod/sqlite_state.go index cdd0d25c88..45dde749e7 100644 --- a/libpod/sqlite_state.go +++ b/libpod/sqlite_state.go @@ -373,10 +373,6 @@ func (s *SQLiteState) ValidateDBConfig(runtime *Runtime) (defErr error) { return fmt.Errorf("retrieving DB config: %w", err) } - if err := tx.Commit(); err != nil { - return fmt.Errorf("committing database validation row: %w", err) - } - checkField := func(fieldName, dbVal, ourVal string) error { if dbVal != ourVal { return fmt.Errorf("database %s %q does not match our %s %q: %w", fieldName, dbVal, fieldName, ourVal, define.ErrDBBadConfig) @@ -407,6 +403,12 @@ func (s *SQLiteState) ValidateDBConfig(runtime *Runtime) (defErr error) { return err } + if err := tx.Commit(); err != nil { + return fmt.Errorf("committing database validation row: %w", err) + } + // Do not return any error after the commit call because the defer will + // try to roll back the transaction which results in an logged error. + return nil }