]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-sql: driver-mysql - Initialize MYSQL struct once
authorAki Tuomi <aki.tuomi@open-xchange.com>
Wed, 30 Aug 2023 10:19:47 +0000 (13:19 +0300)
committerAki Tuomi <aki.tuomi@open-xchange.com>
Mon, 4 Sep 2023 07:53:57 +0000 (10:53 +0300)
Calling initialize more than once will leak memory.

src/lib-sql/driver-mysql.c

index ba402ef994ccad2a38148764c373093c4b5cb6b9..d8dd8d128357496de153ebf06a41232d575c9189 100644 (file)
@@ -91,9 +91,6 @@ static int driver_mysql_connect(struct sql_db *_db)
 
        sql_db_set_state(&db->api, SQL_DB_STATE_CONNECTING);
 
-       if (mysql_init(db->mysql) == NULL)
-               i_fatal("mysql_init() failed");
-
        if (db->host == NULL) {
                /* assume option_file overrides the host, or if not we'll just
                   connect to localhost */
@@ -177,7 +174,6 @@ static void driver_mysql_disconnect(struct sql_db *_db)
        struct mysql_db *db = container_of(_db, struct mysql_db, api);
        if (db->mysql != NULL)
                mysql_close(db->mysql);
-       db->mysql = NULL;
 }
 
 static int driver_mysql_parse_connect_string(struct mysql_db *db,
@@ -274,7 +270,12 @@ static int driver_mysql_parse_connect_string(struct mysql_db *db,
                *error_r = "No hosts given in connect string";
                return -1;
        }
-       db->mysql = p_new(db->pool, MYSQL, 1);
+       if (db->mysql == NULL) {
+               db->mysql = p_new(db->pool, MYSQL, 1);
+               MYSQL *ptr = mysql_init(db->mysql);
+               if (ptr == NULL)
+                       i_fatal_status(FATAL_OUTOFMEM, "mysql_init() failed");
+       }
        return 0;
 }