03.01.2025 19:23, Wietse Venema via Postfix-devel wrote:
Michael, You can simplify the patch. I ran the patched code under
valgrind and it informed me that there was a memory leak. It would
not be the end of the world, but it just isn't a good example.

Yes.  Somehow I thought the dict_sqlite->db need not to be freed
if open failed, which is obviously not the case.  This is what
happens when doing programming during New Year vacation :)


...
On closer investigation, the leak happens because you moved some
code out of dict_sqlite_close() into a new function dict_sqlite_free()
that does not free the db.

Exactly.

It isn't 100% clear what happens with the db which is supposed to
be inited by sqlite3_open(_v2), when this function returned failure, -
because dict_sqlite_close() calls sqlite3_close() on unopened db.

I was too lazy to check the code there, and it seems like it is
okay to do that.

Simplified patch follows... please verify.

Yes.  This was my first (1.5 actually) version, which I later
rewrote to use half of the dict_sqlite_close() function.

Thank you for the correction.

/mjt

--- /var/tmp/postfix-3.10-20241202/src/global/dict_sqlite.c     2023-10-12 
11:34:40.000000000 -0400
+++ ./dict_sqlite.c     2025-01-03 11:15:36.752684221 -0500
@@ -59,6 +59,9 @@
  #if !defined(SQLITE_VERSION_NUMBER) || (SQLITE_VERSION_NUMBER < 3005004)
  #define sqlite3_prepare_v2 sqlite3_prepare
  #endif
+#if !defined(SQLITE_VERSION_NUMBER) || (SQLITE_VERSION_NUMBER < 3005000)
+#define sqlite3_open_v2(fname,ppDB,flags,zVfs) sqlite_open(fname,ppDB)
+#endif
/* Utility library. */ @@ -320,10 +323,16 @@
      dict_sqlite->parser = parser;
      sqlite_parse_config(dict_sqlite, name);
- if (sqlite3_open(dict_sqlite->dbpath, &dict_sqlite->db))
-       msg_fatal("%s:%s: Can't open database: %s\n",
-                 DICT_TYPE_SQLITE, name, sqlite3_errmsg(dict_sqlite->db));
+    if (sqlite3_open_v2(dict_sqlite->dbpath, &dict_sqlite->db,
+                       SQLITE_OPEN_READONLY, NULL) != SQLITE_OK) {
+       DICT   *dict = dict_surrogate(DICT_TYPE_SQLITE, name, open_flags,
+                             dict_flags, "%s:%s: open database %s: %s: %m",
+                               DICT_TYPE_SQLITE, name, dict_sqlite->dbpath,
+                                     sqlite3_errmsg(dict_sqlite->db));
+ dict_sqlite_close(&dict_sqlite->dict);
+       return dict;
+    }
      dict_sqlite->dict.owner = cfg_get_owner(dict_sqlite->parser);
return (DICT_DEBUG (&dict_sqlite->dict));
_______________________________________________
Postfix-devel mailing list -- postfix-devel@postfix.org
To unsubscribe send an email to postfix-devel-le...@postfix.org

_______________________________________________
Postfix-devel mailing list -- postfix-devel@postfix.org
To unsubscribe send an email to postfix-devel-le...@postfix.org

Reply via email to