@@ -414,24 +414,41 @@ static int dump_db(const char *file, const dump_mode mode, const char *tskey_fil
414414 MDB_dbi dbi ;
415415 MDB_cursor * cursor = NULL ;
416416
417- if (0 != (r = mdb_env_create (& env ))
418- || 0 != (r = mdb_env_open (env , file , MDB_NOSUBDIR | MDB_RDONLY , 0644 ))
419- || 0 != (r = mdb_txn_begin (env , NULL , MDB_RDONLY , & txn ))
420- || 0 != (r = mdb_open (txn , NULL , 0 , & dbi ))
421- || 0 != (r = mdb_cursor_open (txn , dbi , & cursor )))
417+ r = mdb_env_create (& env );
418+ if (r != 0 )
422419 {
423- if (env != NULL )
424- {
425- if (txn != NULL )
426- {
427- if (cursor != NULL )
428- {
429- mdb_cursor_close (cursor );
430- }
431- mdb_txn_abort (txn );
432- }
433- mdb_env_close (env );
434- }
420+ return dump_report_error (r );
421+ }
422+
423+ /* If this function fails, mdb_env_close() must be called to discard the MDB_env handle. */
424+ r = mdb_env_open (env , file , MDB_NOSUBDIR | MDB_RDONLY , 0644 );
425+ if (r != 0 )
426+ {
427+ mdb_env_close (env );
428+ return dump_report_error (r );
429+ }
430+
431+ r = mdb_txn_begin (env , NULL , MDB_RDONLY , & txn );
432+ if (r != 0 )
433+ {
434+ mdb_env_close (env );
435+ return dump_report_error (r );
436+ }
437+
438+ /* If the transaction is aborted the database handle will be closed automatically. */
439+ r = mdb_open (txn , NULL , 0 , & dbi );
440+ if (r != 0 )
441+ {
442+ mdb_txn_abort (txn );
443+ mdb_env_close (env );
444+ return dump_report_error (r );
445+ }
446+
447+ r = mdb_cursor_open (txn , dbi , & cursor );
448+ if (r != 0 )
449+ {
450+ mdb_txn_abort (txn );
451+ mdb_env_close (env );
435452 return dump_report_error (r );
436453 }
437454
@@ -465,8 +482,6 @@ static int dump_db(const char *file, const dump_mode mode, const char *tskey_fil
465482 return dump_report_error (r );
466483 }
467484 mdb_cursor_close (cursor );
468- mdb_close (env , dbi );
469-
470485 mdb_txn_abort (txn );
471486 mdb_env_close (env );
472487
0 commit comments