Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WRNft-0006qr-Qg for pgsql-bugs@arkaria.postgresql.org; Sat, 22 Mar 2014 15:17:30 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1WRNft-0000vi-BC for pgsql-bugs@arkaria.postgresql.org; Sat, 22 Mar 2014 15:17:29 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WRNfs-0000vc-Gu for pgsql-bugs@postgresql.org; Sat, 22 Mar 2014 15:17:28 +0000 Received: from momjian.us ([72.94.173.45]) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WRNfp-0007jd-EK; Sat, 22 Mar 2014 15:17:27 +0000 Received: from bruce by momjian.us with local (Exim 4.72) (envelope-from ) id 1WRNfn-0001pz-Jf; Sat, 22 Mar 2014 11:17:23 -0400 Date: Sat, 22 Mar 2014 11:17:23 -0400 From: Bruce Momjian To: Tatsuo Ishii Cc: tgl@sss.pgh.pa.us, pg@heroku.com, pgsql-bugs@postgresql.org Subject: Re: PQclientEncoding() returns -1, resulting in possible assertion failure in psql Message-ID: <20140322151723.GB21843@momjian.us> References: <14465.1386526658@sss.pgh.pa.us> <15210.1386529004@sss.pgh.pa.us> <20131209.081735.1321847791044178990.t-ishii@sraoss.co.jp> <20140322015325.GA21843@momjian.us> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="UlVJffcvxoiEqYs2" Content-Disposition: inline In-Reply-To: <20140322015325.GA21843@momjian.us> User-Agent: Mutt/1.5.20 (2009-06-14) X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org --UlVJffcvxoiEqYs2 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Fri, Mar 21, 2014 at 09:53:25PM -0400, Bruce Momjian wrote: > On Mon, Dec 9, 2013 at 08:17:35AM +0900, Tatsuo Ishii wrote: > > > branches. I would argue however that the documentation nowhere > > > suggests that PQclientEncoding can return a bogus encoding ID, > > > so this is more likely to be a bug fix than a new bug for other > > > programs as well. Also, it looks to me like there are probably > > > > This sounds like a little bit unfair argument. The libpq documentation > > is pretty sloppy for the error case for other PQ* as well. For > > example, look at the PQdb document: > > > > PQdb > > Returns the database name of the connection. > > char *PQdb(const PGconn *conn); > > > > This says nothing about when the connection is bad. Reality is PQdb > > returns NULL in the case. But are we allowed to change PQdb returns > > say, "template1" when the connection is bad because the doc says > > nothing about error case? > > So, what did we decide on this? Should we document the -1 return, or > return SQL_ASCII. OK, hearing nothing, I dug into this, and I think the solution is simpler than we thought. Basically, the Assert is checking for the encoding value to be in a valid range, but the main code is also checking for an invalid encoding and returning PG_SQL_ASCII: pg_encoding_mblen(int encoding, const char *mbstr) { Assert(PG_VALID_ENCODING(encoding)); return ((encoding >= 0 && encoding < sizeof(pg_wchar_table) / sizeof(pg_wchar_tbl)) ? ((*pg_wchar_table[encoding].mblen) ((const unsigned char *) mbstr)) : ((*pg_wchar_table[PG_SQL_ASCII].mblen) ((const unsigned char *) mbstr))); } I think the Assert can be removed as it is checking for something that the main code handles just fine. I assume Asserts are only for checks we don't want to make in the main code path. Throwing an error for an assert build and handling the value just fine in a non-assert build makes no sense. I have updated the documentation to mention the libpq's PQclientEncoding() possible return value of -1, and removed the Asserts from three functions that are already handling invalid encoding values just fine. This does fix the reported failure with Asserts enabled: BEFORE: test=> \df lkjasdf server closed the connection unexpectedly This probably means the server terminated abnormally before or while processing the request. The connection to the server was lost. Attempting reset: Failed. !> \df lkjasdf psql: wchar.c:1781: pg_encoding_mblen: Assertion `((encoding) >= 0 && (encoding) < _PG_LAST_ENCODING_)' failed. /usr/lbin/execargs: line 10: 25883 Aborted "$@" AFTER: test=> \df lkjasdf server closed the connection unexpectedly This probably means the server terminated abnormally before or while processing the request. The connection to the server was lost. Attempting reset: Failed. !> \df lkjasdf You are currently not connected to a database. !> -- Bruce Momjian http://momjian.us EnterpriseDB http://enterprisedb.com + Everyone has their own god. + --UlVJffcvxoiEqYs2 Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="mblen.diff" diff --git a/doc/src/sgml/libpq.sgml b/doc/src/sgml/libpq.sgml new file mode 100644 index 22815bc..be0d602 *** a/doc/src/sgml/libpq.sgml --- b/doc/src/sgml/libpq.sgml *************** int PQclientEncoding(const PGconn * Note that it returns the encoding ID, not a symbolic string ! such as EUC_JP. To convert an encoding ID to an encoding name, you can use: --- 5377,5384 ---- Note that it returns the encoding ID, not a symbolic string ! such as EUC_JP. If unsuccessful, it returns -1. ! To convert an encoding ID to an encoding name, you can use: diff --git a/src/backend/utils/mb/wchar.c b/src/backend/utils/mb/wchar.c new file mode 100644 index 6d03a10..73ef112 *** a/src/backend/utils/mb/wchar.c --- b/src/backend/utils/mb/wchar.c *************** pg_mic_mblen(const unsigned char *mbstr) *** 1778,1785 **** int pg_encoding_mblen(int encoding, const char *mbstr) { - Assert(PG_VALID_ENCODING(encoding)); - return ((encoding >= 0 && encoding < sizeof(pg_wchar_table) / sizeof(pg_wchar_tbl)) ? ((*pg_wchar_table[encoding].mblen) ((const unsigned char *) mbstr)) : --- 1778,1783 ---- *************** pg_encoding_mblen(int encoding, const ch *** 1792,1799 **** int pg_encoding_dsplen(int encoding, const char *mbstr) { - Assert(PG_VALID_ENCODING(encoding)); - return ((encoding >= 0 && encoding < sizeof(pg_wchar_table) / sizeof(pg_wchar_tbl)) ? ((*pg_wchar_table[encoding].dsplen) ((const unsigned char *) mbstr)) : --- 1790,1795 ---- *************** pg_encoding_dsplen(int encoding, const c *** 1808,1815 **** int pg_encoding_verifymb(int encoding, const char *mbstr, int len) { - Assert(PG_VALID_ENCODING(encoding)); - return ((encoding >= 0 && encoding < sizeof(pg_wchar_table) / sizeof(pg_wchar_tbl)) ? ((*pg_wchar_table[encoding].mbverify) ((const unsigned char *) mbstr, len)) : --- 1804,1809 ---- --UlVJffcvxoiEqYs2 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs --UlVJffcvxoiEqYs2--