Blame SOURCES/0001-Memory-leak-in-SQLPrepare-with-queries-that-use-para.patch

94c54c
From 4cf222c082950ff6a713792f68e2a2882135199e Mon Sep 17 00:00:00 2001
94c54c
From: Bogdan Degtyariov <bogdan.degtyariov@oracle.com>
94c54c
Date: Wed, 4 Sep 2013 16:46:27 +1000
94c54c
Subject: [PATCH] Memory leak in SQLPrepare with queries that use parameters
94c54c
 (Bug# 17400483/70113) - no test case
94c54c
94c54c
---
94c54c
 ChangeLog                 |  2 ++
94c54c
 driver/catalog_no_i_s.c   |  1 +
94c54c
 driver/my_prepared_stmt.c | 21 ++++++++++++++-------
94c54c
 driver/my_stmt.c          | 35 ++++++++++++++++++++++-------------
94c54c
 4 files changed, 39 insertions(+), 20 deletions(-)
94c54c
94c54c
diff --git a/driver/catalog_no_i_s.c b/driver/catalog_no_i_s.c
94c54c
index ac54d3e..be4d616 100644
94c54c
--- a/driver/catalog_no_i_s.c
94c54c
+++ b/driver/catalog_no_i_s.c
94c54c
@@ -2141,6 +2141,7 @@ mysql_tables(SQLHSTMT hstmt,
94c54c
       if (!row_count)
94c54c
       {
94c54c
         mysql_free_result(stmt->result);
94c54c
+        stmt->result= NULL;
94c54c
         goto empty_set;
94c54c
       }
94c54c
 
94c54c
diff --git a/driver/my_prepared_stmt.c b/driver/my_prepared_stmt.c
94c54c
index 71a5648..126d892 100644
94c54c
--- a/driver/my_prepared_stmt.c
94c54c
+++ b/driver/my_prepared_stmt.c
94c54c
@@ -1,5 +1,5 @@
94c54c
 /*
94c54c
-  Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
94c54c
+  Copyright (c) 2012-2013, Oracle and/or its affiliates. All rights reserved.
94c54c
 
94c54c
   The MySQL Connector/ODBC is licensed under the terms of the GPLv2
94c54c
   <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most
94c54c
@@ -223,10 +223,17 @@ void free_result_bind(STMT *stmt)
94c54c
 {
94c54c
   if (stmt->result_bind != NULL)
94c54c
   {
94c54c
+    int i, field_cnt= field_count(stmt);
94c54c
+
94c54c
     x_free(stmt->result_bind[0].is_null);
94c54c
     x_free(stmt->result_bind[0].length);
94c54c
     x_free(stmt->result_bind[0].error);
94c54c
-    x_free(stmt->result_bind[0].buffer);
94c54c
+
94c54c
+    /* buffer was allocated for each column */
94c54c
+    for (i= 0; i < field_cnt; i++)
94c54c
+    {
94c54c
+      x_free(stmt->result_bind[i].buffer);
94c54c
+    }
94c54c
 
94c54c
     x_free(stmt->result_bind);
94c54c
     stmt->result_bind= 0;
94c54c
@@ -438,11 +445,11 @@ int ssps_bind_result(STMT *stmt)
94c54c
                                                       IS_PS_OUT_PARAMS(stmt));
94c54c
 
94c54c
       stmt->result_bind[i].buffer_type  = p.type;
94c54c
-	  stmt->result_bind[i].buffer       = p.buffer;
94c54c
-	  stmt->result_bind[i].buffer_length= (unsigned long)p.size;
94c54c
-	  stmt->result_bind[i].length       = &len[i];
94c54c
-	  stmt->result_bind[i].is_null      = &is_null[i];
94c54c
-	  stmt->result_bind[i].error        = &err[i];
94c54c
+	    stmt->result_bind[i].buffer       = p.buffer;
94c54c
+	    stmt->result_bind[i].buffer_length= (unsigned long)p.size;
94c54c
+	    stmt->result_bind[i].length       = &len[i];
94c54c
+	    stmt->result_bind[i].is_null      = &is_null[i];
94c54c
+	    stmt->result_bind[i].error        = &err[i];
94c54c
       stmt->result_bind[i].is_unsigned  = (field->flags & UNSIGNED_FLAG)? 1: 0;
94c54c
 
94c54c
       stmt->array[i]= p.buffer;
94c54c
diff --git a/driver/my_stmt.c b/driver/my_stmt.c
94c54c
index 0dd059f..6e201d5 100644
94c54c
--- a/driver/my_stmt.c
94c54c
+++ b/driver/my_stmt.c
94c54c
@@ -44,7 +44,16 @@ BOOL returned_result(STMT *stmt)
94c54c
   if (ssps_used(stmt))
94c54c
   {
94c54c
     /* Basically at this point we are supposed to get result already */
94c54c
-    return stmt->result ? TRUE : mysql_stmt_result_metadata(stmt->ssps) != NULL;
94c54c
+    MYSQL_RES *temp_res= NULL;
94c54c
+
94c54c
+    if ((stmt->result != NULL) || 
94c54c
+        (temp_res= mysql_stmt_result_metadata(stmt->ssps)) != NULL)
94c54c
+    {
94c54c
+      /* mysql_free_result checks for NULL, so we can always call it */
94c54c
+      mysql_free_result(temp_res);
94c54c
+      return TRUE;
94c54c
+    }
94c54c
+    return FALSE;
94c54c
   }
94c54c
   else
94c54c
   {
94c54c
@@ -55,24 +64,18 @@ BOOL returned_result(STMT *stmt)
94c54c
 
94c54c
 my_bool free_current_result(STMT *stmt)
94c54c
 {
94c54c
+  my_bool res= 0;
94c54c
   if (returned_result(stmt))
94c54c
   {
94c54c
     if (ssps_used(stmt))
94c54c
- 
94c54c
-    {
94c54c
-      my_bool res= mysql_stmt_free_result(stmt->ssps);
94c54c
-      stmt->result= NULL;
94c54c
-
94c54c
-      return res;
94c54c
-    }
94c54c
-    else
94c54c
     {
94c54c
-      mysql_free_result(stmt->result);
94c54c
-      stmt->result= NULL;
94c54c
-      return '\0';
94c54c
+      res= mysql_stmt_free_result(stmt->ssps);
94c54c
     }
94c54c
+    /* We need to always free stmt->result because SSPS keep metadata there */
94c54c
+    mysql_free_result(stmt->result);
94c54c
+    stmt->result= NULL;
94c54c
   }
94c54c
-  return '\0';
94c54c
+  return res;
94c54c
 }
94c54c
 
94c54c
 
94c54c
@@ -98,6 +101,9 @@ MYSQL_RES * stmt_get_result(STMT *stmt, BOOL force_use)
94c54c
    we need to use/store each resultset of multiple resultsets */
94c54c
 MYSQL_RES * get_result_metadata(STMT *stmt, BOOL force_use)
94c54c
 {
94c54c
+  /* just a precaution, mysql_free_result checks for NULL anywat */
94c54c
+  mysql_free_result(stmt->result);
94c54c
+
94c54c
   if (ssps_used(stmt))
94c54c
   {
94c54c
     stmt->result= mysql_stmt_result_metadata(stmt->ssps);
94c54c
@@ -399,6 +405,9 @@ SQLRETURN prepare(STMT *stmt, char * query, SQLINTEGER query_length)
94c54c
 
94c54c
       stmt->param_count= mysql_stmt_param_count(stmt->ssps);
94c54c
 
94c54c
+      /* make sure we free the result from the previous time */
94c54c
+      mysql_free_result(stmt->result);
94c54c
+
94c54c
       /* Getting result metadata */
94c54c
       if ((stmt->result= mysql_stmt_result_metadata(stmt->ssps)))
94c54c
       {
94c54c
-- 
94c54c
2.13.3
94c54c