Several libpqxx users have been reporting odd problems with certain error
messages generated by libpq. One of them was the inclusion of garbage
data.
As it turns out, src/interfaces/libpq/fe-misc.c contains several instances
of this construct:
printfPQExpBuffer(&conn->ErrorMessage,
libpq_gettext("error: %s"),
SOCK_STRERROR(SOCK_ERRNO, buffer, sizeof(buffer)));
This may occur in other source files as well. On Unix-like systems,
SOCK_ERRNO defines to plain errno--which is likely to be overwritten by
the libpq_gettext(). I'm attaching a patch that fixes these instances by
introducing a named pointer to the SOCK_STRERROR message, initialized
before either of the other function calls.
Another approach would have been to make libpq_gettext() preserve errno.
It's tempting, but I'm not sure it would be valid from a language-lawyer
point of view. There is no sequence point between the evaluations of
libpq_gettext() and SOCK_STRERROR(). From what I vaguely remember hearing
somewhere in the distant past, that means that theoretically they may be
evaluated not just in any order but even in parallel. I guess it may
actually happen if both inlining and scheduling are sufficiently
aggressive. Even if libpq_gettext() is made to restore errno, it will
still have to pollute errno at some points during its execution.
Jeroen