Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ouAvW-0004zY-Mh for pgsql-hackers@arkaria.postgresql.org; Sun, 13 Nov 2022 11:13:27 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1ouAvU-00031L-O0 for pgsql-hackers@arkaria.postgresql.org; Sun, 13 Nov 2022 11:13:24 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ouAvT-00030E-Lx for pgsql-hackers@lists.postgresql.org; Sun, 13 Nov 2022 11:13:24 +0000 Received: from out2-smtp.messagingengine.com ([66.111.4.26]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ouAvP-000123-7X for pgsql-hackers@postgresql.org; Sun, 13 Nov 2022 11:13:22 +0000 Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailout.nyi.internal (Postfix) with ESMTP id DF8DF5C00C6; Sun, 13 Nov 2022 06:13:17 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute3.internal (MEProxy); Sun, 13 Nov 2022 06:13:17 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t=1668337997; x= 1668424397; bh=t8IupFo9eDkF2DUJkjWGYvKJw7l+8dP969di0l/+9Q8=; b=d oNoqyAZFsbARQfJyAQFBXRcrO6GjdNED+osF6UWU7Xz7G+JUxejkYiMtb7IrYREp MQ/UVSN8uZWw141Yye70huuM2A7YxWK0440s2dy+jSq9qvjpbZQs+n8JVtcQusmV orvVnptWbIZGczuohI0EA+z9hgy/tyiHW4rpMdjqvRv5m5LK1LQKmKbQyh4QD0L2 GGk0WyC7g3ZKjODN5tIZ+/C+Sh6M9ozqRq95GMorqmzACTQVISDeYyB7CjBOJRYM fNc6o6yBoMT2ZMkuKsUhWe5tL20NFbEE8I3X5obv4qFH/3D8b+R0nfE4ps/Nqnzd ee6nos8+wZgYmnmSWcAUg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvgedrgedtgddvhecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefkffggfgfuvfevfhfhjggtgfesthejredttdefjeenucfhrhhomheprfgvthgv rhcugfhishgvnhhtrhgruhhtuceophgvthgvrhdrvghishgvnhhtrhgruhhtsegvnhhtvg hrphhrihhsvggusgdrtghomheqnecuggftrfgrthhtvghrnhepheelffeggedujeetiefh leetuddvieffhfffvdejvdffgeejkeduleduheduieefnecuvehluhhsthgvrhfuihiivg eptdenucfrrghrrghmpehmrghilhhfrhhomhepphgvthgvrhdrvghishgvnhhtrhgruhht segvnhhtvghrphhrihhsvggusgdrtghomh X-ME-Proxy: Feedback-ID: i131946ab:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sun, 13 Nov 2022 06:13:16 -0500 (EST) Message-ID: <8e795834-7c09-e204-97cd-c3b194ea64fb@enterprisedb.com> Date: Sun, 13 Nov 2022 12:13:15 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Thunderbird/102.4.1 Subject: Re: libpq error message refactoring Content-Language: en-US To: Alvaro Herrera Cc: pgsql-hackers References: <20221109122931.zmjx264q52jjhk7y@alvherre.pgsql> From: Peter Eisentraut In-Reply-To: <20221109122931.zmjx264q52jjhk7y@alvherre.pgsql> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 09.11.22 13:29, Alvaro Herrera wrote: >> +/* >> + * Append a formatted string to the given buffer, after translation. A >> + * newline is automatically appended; the format should not end with a >> + * newline. >> + */ > > I find the "after translation" bit unclear -- does it mean that the > caller should have already translated, or is it the other way around? I > would say "Append a formatted string to the given buffer, after > translating it", which (to me) conveys more clearly that translation > occurs here. ok >> + /* Loop in case we have to retry after enlarging the buffer. */ >> + do >> + { >> + errno = save_errno; >> + va_start(args, fmt); >> + done = appendPQExpBufferVA(errorMessage, libpq_gettext(fmt), args); > > I wonder if it makes sense to do libpq_gettext() just once, instead of > redoing it on each iteration. I wonder whether that would expose us to potential compiler warnings about the format string not being constant. As long as the compiler can trace that the string comes from gettext, it knows what's going on. Also, most error strings in practice don't need the loop, so maybe it's not a big issue. >> +/*------------------------ >> + * appendPQExpBufferVA >> + * Shared guts of printfPQExpBuffer/appendPQExpBuffer. >> + * Attempt to format data and append it to str. Returns true if done >> + * (either successful or hard failure), false if need to retry. >> + * >> + * Caution: callers must be sure to preserve their entry-time errno >> + * when looping, in case the fmt contains "%m". >> + */ >> +extern bool appendPQExpBufferVA(PQExpBuffer str, const char *fmt, va_list args) pg_attribute_printf(2, 0); > > As an API user, I don't care that this is shared guts for something > else, I just care about what it does. I think deleting that line is a > sufficient fix. ok >> @@ -420,7 +418,8 @@ pqsecure_raw_write(PGconn *conn, const void *ptr, size_t len) >> snprintf(msgbuf, sizeof(msgbuf), >> libpq_gettext("server closed the connection unexpectedly\n" >> "\tThis probably means the server terminated abnormally\n" >> - "\tbefore or while processing the request.\n")); >> + "\tbefore or while processing the request.")); >> + strlcat(msgbuf, "\n", sizeof(msgbuf)); >> conn->write_err_msg = strdup(msgbuf); >> /* Now claim the write succeeded */ >> n = len; > > In these two places, we're writing the error message manually to a > separate variable, so the extra \n is necessary. It looks a bit odd to > do it with strlcat() after the fact, but AFAICT it's necessary as it > keeps the \n out of the translation catalog, which is good. This is > nonobvious, so perhaps add a comment about it. ok