[dovecot-cvs] dovecot/src/lib-sql driver-mysql.c,1.19,1.20

cras at dovecot.org cras at dovecot.org
Sat Jun 17 15:20:25 EEST 2006


Update of /var/lib/cvs/dovecot/src/lib-sql
In directory talvi:/tmp/cvs-serv7935

Modified Files:
	driver-mysql.c 
Log Message:
Escaping a string crashed if we weren't connected to mysql.



Index: driver-mysql.c
===================================================================
RCS file: /var/lib/cvs/dovecot/src/lib-sql/driver-mysql.c,v
retrieving revision 1.19
retrieving revision 1.20
diff -u -d -r1.19 -r1.20
--- driver-mysql.c	16 Jun 2006 11:42:30 -0000	1.19
+++ driver-mysql.c	17 Jun 2006 12:20:21 -0000	1.20
@@ -347,15 +347,40 @@
 driver_mysql_escape_string(struct sql_db *_db, const char *string)
 {
 	struct mysql_db *db = (struct mysql_db *)_db;
-	const struct mysql_connection *conn;
+	struct mysql_connection *conn;
+	unsigned int i, count;
 	size_t len = strlen(string);
 	char *to;
 
-	/* All the connections should be identical, so just use the first one */
-	conn = array_idx(&db->connections, 0);
+	/* All the connections should be identical, so just use the first
+	   connected one */
+	conn = array_get_modifyable(&db->connections, &count);
+	for (i = 0; i < count; i++) {
+		if (conn[i].connected)
+			break;
+	}
+	if (i == count) {
+		/* so, try connecting.. */
+		for (i = 0; i < count; i++) {
+			if (driver_mysql_connect(&conn[i]))
+				break;
+		}
+		if (i == count) {
+			/* FIXME: we don't have a valid connection, so fallback
+			   to using default escaping. the next query will most
+			   likely fail anyway so it shouldn't matter that much
+			   what we return here.. Anyway, this API needs
+			   changing so that the escaping function could already
+			   fail the query reliably. */
+			to = t_buffer_get(len * 2 + 1);
+			len = mysql_escape_string(to, string, len);
+			t_buffer_alloc(len + 1);
+			return to;
+		}
+	}
 
 	to = t_buffer_get(len * 2 + 1);
-	len = mysql_real_escape_string(conn->mysql, to, string, len);
+	len = mysql_real_escape_string(conn[i].mysql, to, string, len);
 	t_buffer_alloc(len + 1);
 	return to;
 }



More information about the dovecot-cvs mailing list