changeset 9556:a39372cd040c HEAD

pgsql: Don't leak memory if query returns multiple results. Found by Rainer Weikusat.
author Timo Sirainen <tss@iki.fi>
date Sat, 27 Mar 2010 04:06:20 +0200
parents 0cf38dcd8ced
children 7798e254ff67
files src/lib-sql/driver-pgsql.c
diffstat 1 files changed, 9 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib-sql/driver-pgsql.c	Sat Mar 27 03:54:00 2010 +0200
+++ b/src/lib-sql/driver-pgsql.c	Sat Mar 27 04:06:20 2010 +0200
@@ -249,13 +249,17 @@
 
 static void consume_results(struct pgsql_db *db)
 {
-	do {
-		if (!PQconsumeInput(db->pg))
-			break;
+	PGresult *pgres;
 
+	while (PQconsumeInput(db->pg)) {
 		if (PQisBusy(db->pg))
 			return;
-	} while (PQgetResult(db->pg) != NULL);
+
+		pgres = PQgetResult(db->pg);
+		if (pgres == NULL)
+			break;
+		PQclear(pgres);
+	}
 
 	if (PQstatus(db->pg) == CONNECTION_BAD)
 		io_remove_closed(&db->io);
@@ -280,6 +284,7 @@
 
 	if (result->pgres != NULL) {
 		PQclear(result->pgres);
+		result->pgres = NULL;
 
 		/* we'll have to read the rest of the results as well */
 		i_assert(db->io == NULL);