Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WRW6N-0004Rh-6F for pgsql-bugs@arkaria.postgresql.org; Sun, 23 Mar 2014 00:17:23 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1WRW6M-0007h8-H1 for pgsql-bugs@arkaria.postgresql.org; Sun, 23 Mar 2014 00:17:22 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WRW6L-0007h1-C6 for pgsql-bugs@postgresql.org; Sun, 23 Mar 2014 00:17:21 +0000 Received: from momjian.us ([72.94.173.45]) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WRW6D-0000Bi-Dr; Sun, 23 Mar 2014 00:17:20 +0000 Received: from bruce by momjian.us with local (Exim 4.72) (envelope-from ) id 1WRW6B-0003fq-Bz; Sat, 22 Mar 2014 20:17:11 -0400 Date: Sat, 22 Mar 2014 20:17:11 -0400 From: Bruce Momjian To: Tom Lane Cc: Tatsuo Ishii , pg@heroku.com, pgsql-bugs@postgresql.org Subject: Re: PQclientEncoding() returns -1, resulting in possible assertion failure in psql Message-ID: <20140323001711.GA5606@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> <20140322151723.GB21843@momjian.us> <19770.1395503756@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="J2SCkAp4GZ/dPZZf" Content-Disposition: inline In-Reply-To: <19770.1395503756@sss.pgh.pa.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 --J2SCkAp4GZ/dPZZf Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Sat, Mar 22, 2014 at 11:55:56AM -0400, Tom Lane wrote: > Bruce Momjian writes: > > 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: > > Agreed that that's pretty useless. I wonder though why these functions > are not coded like > > return PG_VALID_ENCODING(encoding) ? > ((*pg_wchar_table[encoding].mblen) ((const unsigned char *) mbstr)) : > ((*pg_wchar_table[PG_SQL_ASCII].mblen) ((const unsigned char *) mbstr))); > > instead of the hard-to-read explicit range check. Agreed. Modified patch attached. -- Bruce Momjian http://momjian.us EnterpriseDB http://enterprisedb.com + Everyone has their own god. + --J2SCkAp4GZ/dPZZf 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..5f65f1b *** a/src/backend/utils/mb/wchar.c --- b/src/backend/utils/mb/wchar.c *************** pg_mic_mblen(const unsigned char *mbstr) *** 1778,1787 **** 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)) : ((*pg_wchar_table[PG_SQL_ASCII].mblen) ((const unsigned char *) mbstr))); } --- 1778,1784 ---- int pg_encoding_mblen(int encoding, const char *mbstr) { ! return (PG_VALID_ENCODING(encoding) ? ((*pg_wchar_table[encoding].mblen) ((const unsigned char *) mbstr)) : ((*pg_wchar_table[PG_SQL_ASCII].mblen) ((const unsigned char *) mbstr))); } *************** pg_encoding_mblen(int encoding, const ch *** 1792,1801 **** 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)) : ((*pg_wchar_table[PG_SQL_ASCII].dsplen) ((const unsigned char *) mbstr))); } --- 1789,1795 ---- int pg_encoding_dsplen(int encoding, const char *mbstr) { ! return (PG_VALID_ENCODING(encoding) ? ((*pg_wchar_table[encoding].dsplen) ((const unsigned char *) mbstr)) : ((*pg_wchar_table[PG_SQL_ASCII].dsplen) ((const unsigned char *) mbstr))); } *************** pg_encoding_dsplen(int encoding, const c *** 1808,1819 **** 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)) : ! ((*pg_wchar_table[PG_SQL_ASCII].mbverify) ((const unsigned char *) mbstr, len))); } /* --- 1802,1810 ---- int pg_encoding_verifymb(int encoding, const char *mbstr, int len) { ! return (PG_VALID_ENCODING(encoding) ? ! ((*pg_wchar_table[encoding].mbverify) ((const unsigned char *) mbstr, len)) : ! ((*pg_wchar_table[PG_SQL_ASCII].mbverify) ((const unsigned char *) mbstr, len))); } /* --J2SCkAp4GZ/dPZZf 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 --J2SCkAp4GZ/dPZZf--