pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Bruce Momjian <bruce@momjian.us>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: Tatsuo Ishii <ishii@postgresql.org>
Cc: pg@heroku.com
Cc: pgsql-bugs@postgresql.org
Subject: Re: PQclientEncoding() returns -1, resulting in possible assertion failure in psql
Date: Sat, 22 Mar 2014 20:17:11 -0400
Message-ID: <20140323001711.GA5606@momjian.us> (raw)
In-Reply-To: <19770.1395503756@sss.pgh.pa.us>
References: <CAM3SWZSU2XSb0cKDreoQnMSge7xN5q32XNC3LE0wfuke-V9BRQ@mail.gmail.com>
	<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>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

On Sat, Mar 22, 2014 at 11:55:56AM -0400, Tom Lane wrote:
> Bruce Momjian <bruce@momjian.us> 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  <bruce@momjian.us>        http://momjian.us
  EnterpriseDB                             http://enterprisedb.com

  + Everyone has their own god. +

-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs

Attachments:

mblen.diff (text/x-diff, 3.1K) download 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 *<repl *** 5377,5383 **** </synopsis> Note that it returns the encoding ID, not a symbolic string ! such as <literal>EUC_JP</literal>. To convert an encoding ID to an encoding name, you can use: <synopsis> --- 5377,5384 ---- </synopsis> Note that it returns the encoding ID, not a symbolic string ! such as <literal>EUC_JP</literal>. If unsuccessful, it returns -1. ! To convert an encoding ID to an encoding name, you can use: <synopsis> 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))); } /*

view thread (12+ messages)  latest in thread

Message-ID: <20140323001711.GA5606@momjian.us>
Permalink:  ../20140323001711.GA5606@momjian.us/
Also on:    postgresql.org/message-id/20140323001711.GA5606@momjian.us

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: bruce@momjian.us, tgl@sss.pgh.pa.us, ishii@postgresql.org, pg@heroku.com
  Subject: Re: PQclientEncoding() returns -1, resulting in possible assertion failure in psql
  In-Reply-To: <20140323001711.GA5606@momjian.us>
  References: <CAM3SWZSU2XSb0cKDreoQnMSge7xN5q32XNC3LE0wfuke-V9BRQ@mail.gmail.com> <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> <20140323001711.GA5606@momjian.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox