Re: [GENERAL] Retrieving query results - Mailing list pgsql-general

From Michael Paquier
Subject Re: [GENERAL] Retrieving query results
Date
Msg-id CAB7nPqR=zNbnsuX78TAuKa=3oZhuKn0JHR7ufvYfVcGdc7ZvyQ@mail.gmail.com
Whole thread Raw
In response to Re: [GENERAL] Retrieving query results  (Tom Lane <tgl@sss.pgh.pa.us>)
Responses Re: [GENERAL] Retrieving query results  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-general
On Fri, Aug 25, 2017 at 8:10 AM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
> I think the real problem occurs where we realloc the array bigger.
> tupArrSize needs to be kept to no more than INT_MAX --- and, ideally,
> it should reach that value rather than dying on the iteration after
> it reaches 2^30 (so that we support resultsets as large as we possibly
> can).  Without a range-check, it's not very clear what realloc will think
> it's being asked for.  Also, on 32-bit machines, we could overflow size_t
> before tupArrSize even gets that big, so a test against
> SIZE_MAX/sizeof(pointer) may be needed as well.
>
> As long as we constrain tupArrSize to be within bounds, we don't
> have to worry about overflow of ntups per se.

I just poked more seriously at this code, and we could use something like that:
@@ -868,6 +868,16 @@ pqAddTuple(PGresult *res, PGresAttValue *tup)
        int         newSize = (res->tupArrSize > 0) ? res->tupArrSize * 2 : 128;
        PGresAttValue **newTuples;

+       if (res->tupArrSize == INT_MAX)
+           return FALSE;
+       if (new_size == INT_MIN)
+           new_size = INT_MAX;
+       if (newSize > SIZE_MAX / sizeof(PGresAttValue *))
+           return FALSE;

Looking at the surroundings, I think that it would be nice to have
pqAddTuple and PQsetvalue set an error message with this patch. The
user can see now that those would only properly report on OOM, but if
we add more types of errors proper error messages would be nice for
users.
--
Michael


pgsql-general by date:

Previous
From: Dmitry Lazurkin
Date:
Subject: Re: [GENERAL] Change location of function/type installed from C-extension
Next
From: Tom Lane
Date:
Subject: Re: [GENERAL] Retrieving query results