Memory leak with SQLNumResultCols

From: Heikki Linnakangas <hlinnakangas(at)vmware(dot)com>
To: pgsql-odbc(at)postgresql(dot)org
Subject: Memory leak with SQLNumResultCols
Date: 2013-03-15 11:20:58
Message-ID: 5143041A.20605@vmware.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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 Content-Type Size
0001-Avoid-multiple-evaluation-in-some-macros.patch text/x-diff 3.3 KB

Browse pgsql-odbc by date

  From Date Subject
Next Message Heikki Linnakangas 2013-03-15 13:52:32 Migrating from CVS to git
Previous Message Heikki Linnakangas 2013-03-15 08:45:34 QR_Destructor should iterate, not recurse, chained result sets