libpq: PQcmdStatus, PQcmdTuples signatures can be painlessly improved

From: Alex Goncharov <alex-goncharov(at)comcast(dot)net>
To: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: libpq: PQcmdStatus, PQcmdTuples signatures can be painlessly improved
Date: 2011-12-13 12:55:45
Message-ID: E1RaRtZ-0008aS-MK@hans3
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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(at)comcast(dot)net --

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Pavel Stehule 2011-12-13 12:59:51 Re: review: CHECK FUNCTION statement
Previous Message Shigeru Hanada 2011-12-13 12:22:24 Re: pgsql_fdw, FDW for PostgreSQL server