@@ -87,25 +87,40 @@ int lmdump(lmdump_mode mode, const char *file)
87
87
assert (file != NULL );
88
88
89
89
int rc ;
90
+ MDB_env * env = NULL ;
91
+ MDB_txn * txn = NULL ;
92
+ MDB_dbi dbi = 0 ;
93
+ MDB_cursor * cursor = NULL ;
90
94
91
- MDB_env * env ;
92
95
rc = mdb_env_create (& env );
93
- if (rc ) return lmdump_report_error (rc );
96
+ if (rc != 0 )
97
+ {
98
+ goto cleanup ;
99
+ }
94
100
95
101
rc = mdb_env_open (env , file , MDB_NOSUBDIR | MDB_RDONLY , 0644 );
96
- if (rc ) return lmdump_report_error (rc );
102
+ if (rc != 0 )
103
+ {
104
+ goto cleanup ;
105
+ }
97
106
98
- MDB_txn * txn ;
99
107
rc = mdb_txn_begin (env , NULL , MDB_RDONLY , & txn );
100
- if (rc ) return lmdump_report_error (rc );
108
+ if (rc != 0 )
109
+ {
110
+ goto cleanup ;
111
+ }
101
112
102
- MDB_dbi dbi ;
103
113
rc = mdb_open (txn , NULL , 0 , & dbi );
104
- if (rc ) return lmdump_report_error (rc );
114
+ if (rc != 0 )
115
+ {
116
+ goto cleanup ;
117
+ }
105
118
106
- MDB_cursor * cursor ;
107
119
rc = mdb_cursor_open (txn , dbi , & cursor );
108
- if (rc ) return lmdump_report_error (rc );
120
+ if (rc != 0 )
121
+ {
122
+ goto cleanup ;
123
+ }
109
124
110
125
MDB_val key , data ;
111
126
while ( (rc = mdb_cursor_get (cursor , & key , & data , MDB_NEXT )) == MDB_SUCCESS )
@@ -115,14 +130,31 @@ int lmdump(lmdump_mode mode, const char *file)
115
130
if (rc != MDB_NOTFOUND )
116
131
{
117
132
// At this point, not found is expected, anything else is an error
118
- return lmdump_report_error (rc );
133
+ goto cleanup ;
134
+ }
135
+ rc = 0 ;
136
+ cleanup :
137
+ if (cursor != NULL )
138
+ {
139
+ mdb_cursor_close (cursor );
140
+ }
141
+ if (dbi != 0 )
142
+ {
143
+ mdb_close (env , dbi );
144
+ }
145
+ if (txn != NULL )
146
+ {
147
+ mdb_txn_abort (txn );
148
+ }
149
+ if (env != NULL )
150
+ {
151
+ mdb_env_close (env );
119
152
}
120
- mdb_cursor_close (cursor );
121
- mdb_close (env , dbi );
122
-
123
- mdb_txn_abort (txn );
124
- mdb_env_close (env );
125
153
154
+ if (rc != 0 )
155
+ {
156
+ return lmdump_report_error (rc );
157
+ }
126
158
return 0 ;
127
159
}
128
160
0 commit comments