<p>Vadim Yanitskiy has uploaded this change for <strong>review</strong>.</p><p><a href="https://gerrit.osmocom.org/10238">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">src/db.c: fix: make sure the database is properly closed<br><br>Thanks to ASAN, it was discovered that some part of heap<br>is not released on exit:<br><br>  ==19736==ERROR: LeakSanitizer: detected memory leaks<br><br>  Indirect leak of 94616 byte(s) in 214 object(s) allocated from:<br>    #0 0x4e05c6  (/home/wmn/osmocom/osmo-hlr/src/osmo-hlr+0x4e05c6)<br>    #1 0x7f9b01061dc6  (/usr/lib/x86_64-linux-gnu/libsqlite3.so.0+0x33dc6)<br><br>  Indirect leak of 1160 byte(s) in 1 object(s) allocated from:<br>    #0 0x4e097d  (/home/wmn/osmocom/osmo-hlr/src/osmo-hlr+0x4e097d)<br>    #1 0x7f9b01061d58  (/usr/lib/x86_64-linux-gnu/libsqlite3.so.0+0x33d58)<br><br>  SUMMARY: AddressSanitizer: 95776 byte(s) leaked in 215 allocation(s).<br><br>After a long investigation, it was figured out that *sqlite never<br>closes the database* due to 'unfinalized statements or unfinished<br>backups'.<br><br>The problem was in db_bootstrap(), where several statements were<br>prepared, but not finalized in loop. This was also the reason of<br>*.db-shm / *.db-wal files remaining after the program is closed,<br>and the reason of the following message<br><br>  db.c:77 (283) recovered 18 frames from WAL file *.db-wal<br><br>Let's fix this and stop ignoring the result of sqlite3_close().<br><br>Change-Id: Ibe620d7723b1947d4f60f820bd18435ad0193112<br>Related: OS#3434<br>---<br>M src/db.c<br>1 file changed, 10 insertions(+), 1 deletion(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://gerrit.osmocom.org:29418/osmo-hlr refs/changes/38/10238/1</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/src/db.c b/src/db.c</span><br><span>index cc06ee6..2d1bdde 100644</span><br><span>--- a/src/db.c</span><br><span>+++ b/src/db.c</span><br><span>@@ -173,12 +173,20 @@</span><br><span> void db_close(struct db_context *dbc)</span><br><span> {</span><br><span>   unsigned int i;</span><br><span style="color: hsl(120, 100%, 40%);">+       int rc;</span><br><span> </span><br><span>  for (i = 0; i < ARRAY_SIZE(dbc->stmt); i++) {</span><br><span>          /* it is ok to call finalize on NULL */</span><br><span>              sqlite3_finalize(dbc->stmt[i]);</span><br><span>   }</span><br><span style="color: hsl(0, 100%, 40%);">-       sqlite3_close(dbc->db);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+  /* Ask sqlite3 to close DB */</span><br><span style="color: hsl(120, 100%, 40%);">+ rc = sqlite3_close(dbc->db);</span><br><span style="color: hsl(120, 100%, 40%);">+       if (rc != SQLITE_OK) { /* Make sure it's actually closed! */</span><br><span style="color: hsl(120, 100%, 40%);">+              LOGP(DDB, LOGL_ERROR, "Couldn't close database: (rc=%d) %s\n",</span><br><span style="color: hsl(120, 100%, 40%);">+                  rc, sqlite3_errmsg(dbc->db));</span><br><span style="color: hsl(120, 100%, 40%);">+      }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span>  talloc_free(dbc);</span><br><span> }</span><br><span> </span><br><span>@@ -200,6 +208,7 @@</span><br><span>             /* execute the statement */</span><br><span>          rc = sqlite3_step(stmt);</span><br><span>             db_remove_reset(stmt);</span><br><span style="color: hsl(120, 100%, 40%);">+                sqlite3_finalize(stmt);</span><br><span>              if (rc != SQLITE_DONE) {</span><br><span>                     LOGP(DDB, LOGL_ERROR, "Cannot bootstrap database: SQL error: (%d) %s,"</span><br><span>                          " during stmt '%s'",</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.osmocom.org/10238">change 10238</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.osmocom.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.osmocom.org/10238"/><meta itemprop="name" content="View Change"/></div></div>

<div style="display:none"> Gerrit-Project: osmo-hlr </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-MessageType: newchange </div>
<div style="display:none"> Gerrit-Change-Id: Ibe620d7723b1947d4f60f820bd18435ad0193112 </div>
<div style="display:none"> Gerrit-Change-Number: 10238 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: Vadim Yanitskiy <axilirator@gmail.com> </div>