Memory leak with SQLNumResultCols - Mailing list pgsql-odbc

From Heikki Linnakangas
Subject Memory leak with SQLNumResultCols
Date
Msg-id 5143041A.20605@vmware.com
Whole thread Raw
List pgsql-odbc
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

Attachment

pgsql-odbc by date:

Previous
From: Heikki Linnakangas
Date:
Subject: QR_Destructor should iterate, not recurse, chained result sets
Next
From: Heikki Linnakangas
Date:
Subject: Migrating from CVS to git