Thread: [HACKERS] Modifing returning value of PQgetvalue.

[HACKERS] Modifing returning value of PQgetvalue.

From
Dmitry Igrishin
Date:
Hello,

PQgetvalue returns a value of type char* (without const). But the documentation says:
"The pointer returned by PQgetvalue points to storage that is part of the PGresult structure. One should not modify the data it points to" (my italics). Could someone tell me please, what wrong with modifing arbitrary character of the data pointed by PQgetvalue's returning value? Or why this restriction is documented? Thanks.

Re: [HACKERS] Modifing returning value of PQgetvalue.

From
Peter Eisentraut
Date:
On 6/24/17 06:31, Dmitry Igrishin wrote:
> PQgetvalue returns a value of type char* (without const). But the
> documentation says:
> "The pointer returned by PQgetvalue points to storage that is part of
> the PGresult structure. /One should not modify the data it points to/"
> (my italics). Could someone tell me please, what wrong with modifing
> arbitrary character of the data pointed by PQgetvalue's returning value?
> Or why this restriction is documented? Thanks.

This is just how the API is defined.  It could be defined differently,
but it is not.

-- 
Peter Eisentraut              http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services



Re: [HACKERS] Modifing returning value of PQgetvalue.

From
Tom Lane
Date:
Peter Eisentraut <peter.eisentraut@2ndquadrant.com> writes:
> On 6/24/17 06:31, Dmitry Igrishin wrote:
>> PQgetvalue returns a value of type char* (without const). But the
>> documentation says:
>> "The pointer returned by PQgetvalue points to storage that is part of
>> the PGresult structure. /One should not modify the data it points to/"
>> (my italics). Could someone tell me please, what wrong with modifing
>> arbitrary character of the data pointed by PQgetvalue's returning value?
>> Or why this restriction is documented? Thanks.

> This is just how the API is defined.  It could be defined differently,
> but it is not.

To enlarge on that: it might well work today.  But if we change the
code in future so that it does not work, we will make no apologies.

There's at least one case where you could have a problem today.
All null entries in a PGresult share the same pointer to an empty
string, so that if you were to modify that string, it would affect
what's seen for other "null" values.  If the API allowed modification
of data values, that would be a bug.  But it doesn't, and so it isn't,
and we wouldn't look kindly on complaints about it.

Ideally, PQgetvalue should be redefined to return "const char *".
I don't really see us doing that anytime soon though.  It would
result in a lot of compile warnings for perfectly-safe client code.
I'm sure this API would look different if it weren't older than
universal availability of "const" :-(
        regards, tom lane