libpq: PQcmdStatus, PQcmdTuples signatures can be painlessly improved - Mailing list pgsql-hackers

From Alex Goncharov
Subject libpq: PQcmdStatus, PQcmdTuples signatures can be painlessly improved
Date
Msg-id E1RaRtZ-0008aS-MK@hans3
Whole thread Raw
Responses Re: libpq: PQcmdStatus, PQcmdTuples signatures can be painlessly improved  (Robert Haas <robertmhaas@gmail.com>)
Re: libpq: PQcmdStatus, PQcmdTuples signatures can be painlessly improved  (Peter Eisentraut <peter_e@gmx.net>)
List pgsql-hackers
Compare:
 int PQntuples(const PGresult *res)

Reasonable: doesn't modify 'res'.

With:
 char *PQcmdStatus(PGresult *res); char *PQcmdTuples(PGresult *res);

Unreasonable:
 a. What, these two can modify 'res' I pass in?..
 b. Oh, yes, because they return 'char *' pointing to    'res->cmdStatus+n', so, a libpq user may write:
       char *s = PQcmdStatus(res);       *s = 'x';
    and have 'res' modified.  (Would be the user's fault, of course.) 
The non-const-ness of 'PGresult *' for these two functions seems to
stand out among the functions covered in the "30.3.2. Retrieving Query
Result Information" manual section and inhibits writing the strict
client code.

I would suggest to change the signatures by applying this trivial
patch (and changing the documentation):

============================================================
== diff orig/postgresql-9.1.1/src/interfaces/libpq/libpq-fe.h ./postgresql-9.1.1/src/interfaces/libpq/libpq-fe.h
450c450
< extern char *PQcmdStatus(PGresult *res);
---
> extern const char *PQcmdStatus(const PGresult *res);
453c453
< extern char *PQcmdTuples(PGresult *res);
---
> extern const char *PQcmdTuples(const PGresult *res);
== diff orig/postgresql-9.1.1/src/interfaces/libpq/fe-exec.c ./postgresql-9.1.1/src/interfaces/libpq/fe-exec.c
2665,2666c2665,2666
< char *
< PQcmdStatus(PGresult *res)
---
> const char *
> PQcmdStatus(const PGresult *res)
2736,2737c2736,2737
< char *
< PQcmdTuples(PGresult *res)
---
> const char *
> PQcmdTuples(const PGresult *res)
2739,2740c2739
<       char       *p,
<                          *c;
---
>       const char *p, *c;
============================================================

(The above was obtained in 9.1.1; the subsequent build with GCC 4.1.2
succeeds without warnings.)

If the above change causes a warning in a client code, so much the
better: the client code is doing something unreasonable like the "*s"
assignment in my example above.

-- Alex -- alex-goncharov@comcast.net --


pgsql-hackers by date:

Previous
From: Etsuro Fujita
Date:
Subject: Re: WIP: Collecting statistics on CSV file data
Next
From: Pavel Stehule
Date:
Subject: Re: review: CHECK FUNCTION statement