Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UGShC-0002Ze-Am for pgsql-odbc@arkaria.postgresql.org; Fri, 15 Mar 2013 11:21:10 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1UGShB-0007Th-RF for pgsql-odbc@arkaria.postgresql.org; Fri, 15 Mar 2013 11:21:09 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UGShB-0007Tc-9N for pgsql-odbc@postgresql.org; Fri, 15 Mar 2013 11:21:09 +0000 Received: from smtp-outbound-2.vmware.com ([208.91.2.13]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UGSh6-0001KF-A2 for pgsql-odbc@postgresql.org; Fri, 15 Mar 2013 11:21:07 +0000 Received: from sc9-mailhost1.vmware.com (sc9-mailhost1.vmware.com [10.113.161.71]) by smtp-outbound-2.vmware.com (Postfix) with ESMTP id EF948280B8 for ; Fri, 15 Mar 2013 04:21:00 -0700 (PDT) Received: from zcs-prod-ext-mta-2.vmware.com (zcs-prod-ext-mta-2.vmware.com [10.113.62.224]) by sc9-mailhost1.vmware.com (Postfix) with ESMTP id EC8D11806E for ; Fri, 15 Mar 2013 04:21:00 -0700 (PDT) Received: from localhost (localhost.localdomain [127.0.0.1]) by zcs-prod-ext-mta-2.vmware.com (Postfix) with ESMTP id 48E83C154C for ; Fri, 15 Mar 2013 04:20:59 -0700 (PDT) X-Virus-Scanned: amavisd-new at zcs-prod-ext-mta-2.vmware.com Received: from zcs-prod-ext-mta-2.vmware.com ([127.0.0.1]) by localhost (zcs-prod-ext-mta-2.vmware.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id hs9OOqfipcGO for ; Fri, 15 Mar 2013 04:20:59 -0700 (PDT) Received: from zcs-prod-ext-mta-2.vmware.com (localhost.localdomain [127.0.0.1]) by zcs-prod-ext-mta-2.vmware.com (Postfix) with ESMTP id 1AF91C1523 for ; Fri, 15 Mar 2013 04:20:59 -0700 (PDT) Received: from [192.168.1.90] (zimbra-prod-ext-proxy-vip.vmware.com [10.113.63.87]) by zcs-prod-ext-mta-2.vmware.com (Postfix) with ESMTPSA for ; Fri, 15 Mar 2013 04:20:58 -0700 (PDT) Message-ID: <5143041A.20605@vmware.com> Date: Fri, 15 Mar 2013 13:20:58 +0200 From: Heikki Linnakangas Organization: VMware User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:10.0.12) Gecko/20130116 Icedove/10.0.12 MIME-Version: 1.0 To: pgsql-odbc@postgresql.org Subject: Memory leak with SQLNumResultCols Content-Type: multipart/mixed; boundary="------------090004050600030900040805" X-Pg-Spam-Score: -9.3 (---------) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-odbc Precedence: bulk Sender: pgsql-odbc-owner@postgresql.org This is a multi-part message in MIME format. --------------090004050600030900040805 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit If you call SQLNumResultCols() on a non-SELECT statement, e.g. INSERT RETURNING, it leaks memory. The root cause is that QR_Constructor() gets called multiple times due to multiple-evaluation of macro arguments. A the end of SC_pre_execute(): > if (!SC_is_pre_executable(self)) > { > SC_set_Result(self, QR_Constructor()); > QR_set_rstatus(SC_get_Result(self), PORES_TUPLES_OK); > self->inaccurate_result = TRUE; > self->status = STMT_PREMATURE; > num_fields = 0; > } SC_set_Result is a macro, which evaluates the 2nd argument multiple times, so we end up creating multiple result sets, most of which are leaked. Multiple-evaluating macros are pretty dangerous. I think we should turn them into proper functions, per attached patch. This doesn't change all macros, only a few of the more complicated ones, and the one causing the leak in particular. The attached patch is also available at my git repository at https://github.com/hlinnaka/psqlodbc/, along with a test case in master-with-testcases branch. - Heikki --------------090004050600030900040805 Content-Type: text/x-diff; name="0001-Avoid-multiple-evaluation-in-some-macros.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="0001-Avoid-multiple-evaluation-in-some-macros.patch" From 30b201b01a60f8c88ac3d043d942c562d491fdb8 Mon Sep 17 00:00:00 2001 From: Heikki Linnakangas Date: Fri, 15 Mar 2013 11:51:12 +0200 Subject: [PATCH 1/1] Avoid multiple evaluation in some macros. There was at least one real bug caused by the multiple evaluation of 2nd argument to SC_set_result(). SC_pre_execute() passed QR_Constructor() call as the 2nd argument, so we ended up creating extra result sets that were leaked. --- statement.c | 35 +++++++++++++++++++++++++++++++++++ statement.h | 33 +++------------------------------ 2 files changed, 38 insertions(+), 30 deletions(-) diff --git a/statement.c b/statement.c index a0543f5..697d142 100644 --- a/statement.c +++ b/statement.c @@ -522,6 +522,41 @@ SC_Destructor(StatementClass *self) return TRUE; } +void +SC_init_Result(StatementClass *self) +{ + self->result = self->curres = NULL; + self->curr_param_result = 0; + mylog("SC_init_Result(%x)", self); +} + +void +SC_set_Result(StatementClass *self, QResultClass *res) +{ + if (res != self->result) + { + mylog("SC_set_Result(%x, %x)", self, res); + QR_Destructor(self->result); + self->result = self->curres = res; + if (NULL != res) + self->curr_param_result = 1; + } +} + +void +SC_forget_unnamed(StatementClass *self) +{ + if (PREPARED_TEMPORARILY == self->prepared) + { + SC_set_prepared(self, ONCE_DESCRIBED); + if (FALSE && !SC_IsExecuting(self)) + { + QResultClass *res = SC_get_Curres(self); + if (NULL != res && !res->dataFilled && !QR_is_fetching_tuples(res)) + SC_set_Result(self, NULL); + } + } +} /* * Free parameters and free the memory from the diff --git a/statement.h b/statement.h index cb21911..21785ae 100644 --- a/statement.h +++ b/statement.h @@ -285,23 +285,8 @@ struct StatementClass_ }; #define SC_get_conn(a) (a->hdbc) -#define SC_init_Result(a) \ -do { \ - a->result = a->curres = NULL; \ - a->curr_param_result = 0; \ - mylog("SC_init_Result(%x)", a); \ -} while (0) -#define SC_set_Result(a, b) \ -do { \ - if (b != a->result) \ - { \ - mylog("SC_set_Result(%x, %x)", a, b); \ - QR_Destructor(a->result); \ - a->result = a->curres = b; \ - if (NULL != b) \ - a->curr_param_result = 1; \ - } \ -} while (0) +void SC_init_Result(StatementClass *self); +void SC_set_Result(StatementClass *self, QResultClass *res); #define SC_get_Result(a) (a->result) #define SC_set_Curres(a, b) (a->curres = b) #define SC_get_Curres(a) (a->curres) @@ -435,19 +420,7 @@ enum #define SC_started_rbpoint(a) ((a->rbonerr & (1L << 4)) != 0) #define SC_unref_CC_error(a) ((a->ref_CC_error) = FALSE) #define SC_ref_CC_error(a) ((a->ref_CC_error) = TRUE) -#define SC_forget_unnamed(a) \ -do { \ - if (PREPARED_TEMPORARILY == (a)->prepared) \ - { \ - SC_set_prepared(a, ONCE_DESCRIBED); \ - if (FALSE && !SC_IsExecuting((a))) \ - { \ - QResultClass *res = SC_get_Curres((a)); \ - if (NULL != res && !res->dataFilled && !QR_is_fetching_tuples(res)) \ - SC_set_Result((a), NULL); \ - } \ - } \ -} while (0) +void SC_forget_unnamed(StatementClass *self); #define SC_can_parse_statement(a) (STMT_TYPE_SELECT == (a)->statement_type) #define SC_may_use_cursor(a) (STMT_TYPE_SELECT == (a)->statement_type || STMT_TYPE_WITH == (a)->statement_type) #define SC_may_fetch_rows(a) (STMT_TYPE_SELECT == (a)->statement_type || STMT_TYPE_WITH == (a)->statement_type) -- 1.7.10.4 --------------090004050600030900040805 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-odbc --------------090004050600030900040805--