Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1XEpJ9-0002Uq-DP for pgsql-bugs@arkaria.postgresql.org; Wed, 06 Aug 2014 00:42:23 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1XEpJ7-0008I2-NO for pgsql-bugs@arkaria.postgresql.org; Wed, 06 Aug 2014 00:42:21 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1XEpJ6-0008Hw-Vz for pgsql-bugs@postgresql.org; Wed, 06 Aug 2014 00:42:21 +0000 Received: from mout.kundenserver.de ([212.227.17.10]) by makus.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1XEpJ3-0006J3-8r for pgsql-bugs@postgresql.org; Wed, 06 Aug 2014 00:42:19 +0000 Received: from ayaki.localdomain (58-7-183-163.dyn.iinet.net.au [58.7.183.163]) by mrelayeu.kundenserver.de (node=mreue101) with ESMTP (Nemesis) id 0Md4aY-1Wxi7z2mAc-00IA5T; Wed, 06 Aug 2014 02:42:14 +0200 Message-ID: <53E179E1.3060404@2ndquadrant.com> Date: Wed, 06 Aug 2014 08:42:09 +0800 From: Craig Ringer Organization: 2nd Quadrant User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.4.0 MIME-Version: 1.0 To: pgsql-bugs Subject: PostgreSQL fails to convert decomposed utf-8 to other encodings X-Enigmail-Version: 1.6 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Provags-ID: V02:K0:GKJniSNRzKoWqlbZn9mn5osUVvRsVAPLLSjbbLRQyW3 QOk/UDhbWKYOIPQYCyLQ5qIErlljktuWvIMDapk43Nc6mPqfHg CyuWE6+BtMIwzJWYUndxCcMpTHVFrks086gZvSrAGGTgrn2qbA Cw52y/tQMpk17zIMPBfHqNE+pcVo9uEq/JpylRKKtljizWE68m DM8z7YvPM2ZiNHbY+lc3CQeWEb+dcmQYurFo0agWuvWEds4rtf r2iZxIK1Bs6w/Avd0rpjATv3dH3WHL6EmVCjgCRsGAmuzeYsbG KWDlOrkztYxi5T3l2bW7GtTrYl0BECqFi/5quR0LQ7hGreUgcx T/4Zmcp62OPlfnlSG4H0p9A/dB47RP8v3WyNaI4Wh 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 There's a bug in encoding conversions from utf-8 to other encodings that results in corrupt output if decomposed utf-8 is used. PostgreSQL doesn't process utf-8 to pre-composed form first, so decomposed UTF-8 is not handled correctly. Take á: regress=> -- Decomposed - 'a' then 'acute' regress=> SELECT E'\u0061\u0301'; ' ?column? ---------- á (1 row) regress=> -- Precomposed - 'a-acute' regress=> SELECT E'\u00E1'; ?column? ---------- á (1 row) regress=> SELECT convert_to(E'\u0061\u0301', 'iso-8859-1'); ERROR: character with byte sequence 0xcc 0x81 in encoding "UTF8" has no equivalent in encoding "LATIN1" regress=> SELECT convert_to(E'\u00E1', 'iso-8859-1'); convert_to ------------ \xe1 (1 row) This affects input from the client too: regress=> SELECT convert_to('á', 'iso-8859-1'); ERROR: character with byte sequence 0xcc 0x81 in encoding "UTF8" has no equivalent in encoding "LATIN1" regress=> SELECT convert_to('á', 'iso-8859-1'); convert_to ------------ \xe1 (1 row) ... yes, that looks like the same function producing different results on identical input. You might not be able to reproduce with copy and paste from this mail if your client normalizes UTF-8, but you'll be able to by printing the decomposed character to your terminal as an escape string, then copying and pasting from there. We should've probably been normalizing decomposed sequences to precomposed as part of utf-8 validation wherever 'text' input occurs, but it's too late for that now as DBs in the wild will contain decomposed chars. Instead, conversion functions need to normalize decomposed chars to precomposed before converting from utf-8 to another encoding. Comments? -- Craig Ringer http://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Training & Services -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs