From dee89f856ffba3fcfccf530cd98f4d515bb46728 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Mon, 25 Mar 2013 07:28:36 +0100 Subject: [PATCH] mysql plugin: Call mysql_init() only once. Apparently, if you call it with a structure allocated by mysql_init() itself, it leaks memory. Thanks to Yves Mettier for pointing this out! Fixes Github issue #274. --- src/mysql.c | 63 ++++++++++++++++++++++++++++++------------------------------- 1 file changed, 31 insertions(+), 32 deletions(-) diff --git a/src/mysql.c b/src/mysql.c index 6f9efe35..8b3cd210 100644 --- a/src/mysql.c +++ b/src/mysql.c @@ -59,7 +59,7 @@ struct mysql_database_s /* {{{ */ int slave_sql_running; MYSQL *con; - int state; + _Bool is_connected; }; typedef struct mysql_database_s mysql_database_t; /* }}} */ @@ -359,30 +359,31 @@ static int mysql_config (oconfig_item_t *ci) /* {{{ */ static MYSQL *getconnection (mysql_database_t *db) { - if (db->state != 0) + if (db->is_connected) { - int err; - if ((err = mysql_ping (db->con)) != 0) - { - WARNING ("mysql_ping failed for %s: %s", - (db->instance != NULL) - ? db->instance - : "", - mysql_error (db->con)); - db->state = 0; - } - else - { - db->state = 1; + int status; + + status = mysql_ping (db->con); + if (status == 0) return (db->con); - } + + WARNING ("mysql plugin: Lost connection to instance \"%s\": %s", + (db->instance != NULL) + ? db->instance + : "", + mysql_error (db->con)); } + db->is_connected = 0; - if ((db->con = mysql_init (db->con)) == NULL) + if (db->con == NULL) { - ERROR ("mysql_init failed: %s", mysql_error (db->con)); - db->state = 0; - return (NULL); + db->con = mysql_init (NULL); + if (db->con == NULL) + { + ERROR ("mysql plugin: mysql_init failed: %s", + mysql_error (db->con)); + return (NULL); + } } if (mysql_real_connect (db->con, db->host, db->user, db->pass, @@ -393,20 +394,18 @@ static MYSQL *getconnection (mysql_database_t *db) (db->database != NULL) ? db->database : "", (db->host != NULL) ? db->host : "localhost", mysql_error (db->con)); - db->state = 0; return (NULL); } - else - { - INFO ("mysql plugin: Successfully connected to database %s " - "at server %s (server version: %s, protocol version: %d)", - (db->database != NULL) ? db->database : "", - mysql_get_host_info (db->con), - mysql_get_server_info (db->con), - mysql_get_proto_info (db->con)); - db->state = 1; - return (db->con); - } + + INFO ("mysql plugin: Successfully connected to database %s " + "at server %s (server version: %s, protocol version: %d)", + (db->database != NULL) ? db->database : "", + mysql_get_host_info (db->con), + mysql_get_server_info (db->con), + mysql_get_proto_info (db->con)); + + db->is_connected = 1; + return (db->con); } /* static MYSQL *getconnection (mysql_database_t *db) */ static void set_host (mysql_database_t *db, char *buf, size_t buflen) -- 2.11.0