Re: errmsg() clobbers errno

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: John Gorman <johngorman2(at)gmail(dot)com>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: errmsg() clobbers errno
Date: 2015-05-19 16:21:26
Message-ID: 11996.1432052486@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

John Gorman <johngorman2(at)gmail(dot)com> writes:
> While debugging an extension I discovered that the errmsg()
> function zeros out errno.

So might a lot of other functions used in an ereport's arguments.

> This is annoying because if the process of assembling a meaningful
> error message happens to call errmsg() before calling strerror()
> we lose the strerror information.

This is why you should use %m and not strerror(errno). The infrastructure
for %m is set up so that errno is captured before evaluating any of the
ereport's arguments.

> I am attaching a patch to preserve errno across errmsg() calls.

This is pretty useless, unfortunately, because there are just too
many ways to bite yourself on the rear if you reference errno inside
the arguments of an ereport (or any other complicated nest of function
calls).

regards, tom lane

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Andres Freund 2015-05-19 16:29:47 Re: RFC: Non-user-resettable SET SESSION AUTHORISATION
Previous Message Heikki Linnakangas 2015-05-19 16:20:40 Re: Wrong Assert in PageIndexMultiDelete?