diff options
author | Vadim Yanitskiy <axilirator@gmail.com> | 2018-07-30 20:14:16 +0700 |
---|---|---|
committer | Harald Welte <laforge@gnumonks.org> | 2018-07-30 13:35:32 +0000 |
commit | dc17e05e28fc23219aa2c03917bcf70a8edb2131 (patch) | |
tree | 6c703e4f5b69e2e932532072f7f561779fd8e102 | |
parent | 953d27ce8fa50bdd9d6e2f4f206a177f083690a4 (diff) |
src/db.c: fix: make sure the database is properly closed
Thanks to ASAN, it was discovered that some part of heap
is not released on exit:
==19736==ERROR: LeakSanitizer: detected memory leaks
Indirect leak of 94616 byte(s) in 214 object(s) allocated from:
#0 0x4e05c6 (/home/wmn/osmocom/osmo-hlr/src/osmo-hlr+0x4e05c6)
#1 0x7f9b01061dc6 (/usr/lib/x86_64-linux-gnu/libsqlite3.so.0+0x33dc6)
Indirect leak of 1160 byte(s) in 1 object(s) allocated from:
#0 0x4e097d (/home/wmn/osmocom/osmo-hlr/src/osmo-hlr+0x4e097d)
#1 0x7f9b01061d58 (/usr/lib/x86_64-linux-gnu/libsqlite3.so.0+0x33d58)
SUMMARY: AddressSanitizer: 95776 byte(s) leaked in 215 allocation(s).
After a long investigation, it was figured out that *sqlite never
closes the database* due to 'unfinalized statements or unfinished
backups'.
The problem was in db_bootstrap(), where several statements were
prepared, but not finalized in loop. This was also the reason of
*.db-shm / *.db-wal files remaining after the program is closed,
and the reason of the following message
db.c:77 (283) recovered 18 frames from WAL file *.db-wal
Let's fix this and stop ignoring the result of sqlite3_close().
Change-Id: Ibe620d7723b1947d4f60f820bd18435ad0193112
Related: OS#3434
-rw-r--r-- | src/db.c | 11 |
1 files changed, 10 insertions, 1 deletions
@@ -173,12 +173,20 @@ bool db_bind_int64(sqlite3_stmt *stmt, const char *param_name, int64_t nr) void db_close(struct db_context *dbc) { unsigned int i; + int rc; for (i = 0; i < ARRAY_SIZE(dbc->stmt); i++) { /* it is ok to call finalize on NULL */ sqlite3_finalize(dbc->stmt[i]); } - sqlite3_close(dbc->db); + + /* Ask sqlite3 to close DB */ + rc = sqlite3_close(dbc->db); + if (rc != SQLITE_OK) { /* Make sure it's actually closed! */ + LOGP(DDB, LOGL_ERROR, "Couldn't close database: (rc=%d) %s\n", + rc, sqlite3_errmsg(dbc->db)); + } + talloc_free(dbc); } @@ -200,6 +208,7 @@ static int db_bootstrap(struct db_context *dbc) /* execute the statement */ rc = sqlite3_step(stmt); db_remove_reset(stmt); + sqlite3_finalize(stmt); if (rc != SQLITE_DONE) { LOGP(DDB, LOGL_ERROR, "Cannot bootstrap database: SQL error: (%d) %s," " during stmt '%s'", |