Skip to content

Commit 4dadb7a

Browse files
committed
Fixed memory leak in lmdump
Ticket: CFE-4581 Signed-off-by: Victor Moene <victor.moene@northern.tech>
1 parent 0338b4d commit 4dadb7a

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

cf-check/lmdump.c

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -87,25 +87,25 @@ int lmdump(lmdump_mode mode, const char *file)
8787
assert(file != NULL);
8888

8989
int rc;
90+
MDB_env *env = NULL;
91+
MDB_txn *txn = NULL;
92+
MDB_dbi dbi = 0;
93+
MDB_cursor *cursor = NULL;
9094

91-
MDB_env *env;
9295
rc = mdb_env_create(&env);
93-
if (rc) return lmdump_report_error(rc);
96+
if (rc) goto cleanup;
9497

9598
rc = mdb_env_open(env, file, MDB_NOSUBDIR | MDB_RDONLY, 0644);
96-
if (rc) return lmdump_report_error(rc);
99+
if (rc) goto cleanup;
97100

98-
MDB_txn *txn;
99101
rc = mdb_txn_begin(env, NULL, MDB_RDONLY, &txn);
100-
if (rc) return lmdump_report_error(rc);
102+
if (rc) goto cleanup;
101103

102-
MDB_dbi dbi;
103104
rc = mdb_open(txn, NULL, 0, &dbi);
104-
if (rc) return lmdump_report_error(rc);
105+
if (rc) goto cleanup;
105106

106-
MDB_cursor *cursor;
107107
rc = mdb_cursor_open(txn, dbi, &cursor);
108-
if (rc) return lmdump_report_error(rc);
108+
if (rc) goto cleanup;
109109

110110
MDB_val key, data;
111111
while ( (rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == MDB_SUCCESS )
@@ -115,14 +115,16 @@ int lmdump(lmdump_mode mode, const char *file)
115115
if (rc != MDB_NOTFOUND)
116116
{
117117
// At this point, not found is expected, anything else is an error
118-
return lmdump_report_error(rc);
118+
goto cleanup;
119119
}
120-
mdb_cursor_close(cursor);
121-
mdb_close(env, dbi);
122-
123-
mdb_txn_abort(txn);
124-
mdb_env_close(env);
120+
rc = 0;
121+
cleanup:
122+
if (cursor) mdb_cursor_close(cursor);
123+
if (dbi) mdb_close(env, dbi);
124+
if (txn) mdb_txn_abort(txn);
125+
if (env) mdb_env_close(env);
125126

127+
if (rc) return lmdump_report_error(rc);
126128
return 0;
127129
}
128130

0 commit comments

Comments
 (0)