pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Avoid using %c printf format for potentially non-ASCII character
Date: Mon, 29 Jun 2020 15:41:31 +0000
Message-ID: <E1jpvuV-0000Pf-5d@gemulon.postgresql.org> (raw)

Avoid using %c printf format for potentially non-ASCII characters.

Since %c only passes a C "char" to printf, it's incapable of dealing
with multibyte characters.  Passing just the first byte of such a
character leads to an output string that is visibly not correctly
encoded, resulting in undesirable behavior such as encoding conversion
failures while sending error messages to clients.

We've lived with this issue for a long time because it was inconvenient
to avoid in a portable fashion.  However, now that we always use our own
snprintf code, it's reasonable to use the %.*s format to print just one
possibly-multibyte character in a string.  (We previously avoided that
obvious-looking answer in order to work around glibc's bug #6530, cf
commits 54cd4f045 and ed437e2b2.)

Hence, run around and fix a bunch of places that used %c to report
a character found in a user-supplied string.  For simplicity, I did
not touch places that were emitting non-user-facing debug messages,
or reporting catalog data that should always be ASCII.  (It's also
unclear how useful this approach could be in frontend code, where
it's less certain that we know what encoding we're dealing with.)

In passing, improve a couple of poorly-written error messages in
pageinspect/heapfuncs.c.

This is a longstanding issue, but I'm hesitant to back-patch because
of the impact on translatable message strings.  In any case this fix
would not work reliably before v12.

Tom Lane and Quan Zongliang

Discussion: https://postgr.es/m/a120087c-4c88-d9d4-1ec5-808d7a7f133d@gmail.com

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/16e3ad5d143795b05a21dc887c2ab384cce4bcb8

Modified Files
--------------
contrib/hstore/hstore_io.c            | 16 ++++++++++++----
contrib/pageinspect/heapfuncs.c       | 11 ++++++-----
src/backend/utils/adt/encode.c        | 20 +++++++++++++-------
src/backend/utils/adt/jsonpath_gram.y |  4 ++--
src/backend/utils/adt/regexp.c        |  4 ++--
src/backend/utils/adt/varbit.c        | 16 ++++++++--------
src/backend/utils/adt/varlena.c       |  8 ++++----
7 files changed, 47 insertions(+), 32 deletions(-)



Message-ID: <E1jpvuV-0000Pf-5d@gemulon.postgresql.org>
Permalink:  ../E1jpvuV-0000Pf-5d@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1jpvuV-0000Pf-5d@gemulon.postgresql.org

 · 

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-committers@postgresql.org
  Cc: tgl@sss.pgh.pa.us
  Subject: Re: pgsql: Avoid using %c printf format for potentially non-ASCII character
  In-Reply-To: <E1jpvuV-0000Pf-5d@gemulon.postgresql.org>
  References: <E1jpvuV-0000Pf-5d@gemulon.postgresql.org>

* 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