Skip to content

Commit cbbe246

Browse files
committed
Fixed memory leak in lmdump
Ticket: CFE-4581 Signed-off-by: Victor Moene <[email protected]>
1 parent 0338b4d commit cbbe246

File tree

1 file changed

+47
-15
lines changed

1 file changed

+47
-15
lines changed

cf-check/lmdump.c

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -87,25 +87,40 @@ 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 != 0)
97+
{
98+
goto cleanup;
99+
}
94100

95101
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+
}
97106

98-
MDB_txn *txn;
99107
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+
}
101112

102-
MDB_dbi dbi;
103113
rc = mdb_open(txn, NULL, 0, &dbi);
104-
if (rc) return lmdump_report_error(rc);
114+
if (rc != 0)
115+
{
116+
goto cleanup;
117+
}
105118

106-
MDB_cursor *cursor;
107119
rc = mdb_cursor_open(txn, dbi, &cursor);
108-
if (rc) return lmdump_report_error(rc);
120+
if (rc != 0)
121+
{
122+
goto cleanup;
123+
}
109124

110125
MDB_val key, data;
111126
while ( (rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == MDB_SUCCESS )
@@ -115,14 +130,31 @@ int lmdump(lmdump_mode mode, const char *file)
115130
if (rc != MDB_NOTFOUND)
116131
{
117132
// 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);
119152
}
120-
mdb_cursor_close(cursor);
121-
mdb_close(env, dbi);
122-
123-
mdb_txn_abort(txn);
124-
mdb_env_close(env);
125153

154+
if (rc != 0)
155+
{
156+
return lmdump_report_error(rc);
157+
}
126158
return 0;
127159
}
128160

0 commit comments

Comments
 (0)