diff mbox series

ras-record: fix memory leak when sqlite3_open_v2 fails

Message ID 1604926163-27370-1-git-send-email-lvying6@huawei.com (mailing list archive)
State New, archived
Headers show
Series ras-record: fix memory leak when sqlite3_open_v2 fails | expand

Commit Message

Lv Ying Nov. 9, 2020, 12:49 p.m. UTC
According to SQLite documentation, a database connection handle
is usually returned, even if an error occurs. This behavior has
caused rasdaemon to leak memory when opening a database is failed. Now,
even if sqlite3_open_v2() does not return SQLITE_OK, rasdaemon tries to
release SQLite database handle by calling sqlite3_close_v2().

Signed-off-by: lvying6 <lvying6@huawei.com>
---
 ras-record.c | 2 ++
 1 file changed, 2 insertions(+)
diff mbox series

Patch

diff --git a/ras-record.c b/ras-record.c
index 549c494..dc89f01 100644
--- a/ras-record.c
+++ b/ras-record.c
@@ -726,6 +726,8 @@  int ras_mc_event_opendb(unsigned cpu, struct ras_events *ras)
 	} while (rc == SQLITE_BUSY);
 
 	if (rc != SQLITE_OK) {
+		sqlite3_close_v2(db);
+		db = NULL;
 		log(TERM, LOG_ERR,
 		    "cpu %u: Failed to connect to %s: error = %d\n",
 		    cpu, SQLITE_RAS_DB, rc);