int PQntuples(const PGresult *res)

Reasonable: doesn't modify 'res'.


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


  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 
< extern char *PQcmdStatus(PGresult *res);
> extern const char *PQcmdStatus(const PGresult *res);
< extern char *PQcmdTuples(PGresult *res);
> extern const char *PQcmdTuples(const PGresult *res);
== diff orig/postgresql-9.1.1/src/interfaces/libpq/fe-exec.c 
< char *
< PQcmdStatus(PGresult *res)
> const char *
> PQcmdStatus(const PGresult *res)
< char *
< PQcmdTuples(PGresult *res)
> const char *
> PQcmdTuples(const PGresult *res)
<       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 -- --

Sent via pgsql-hackers mailing list (
To make changes to your subscription:

Reply via email to