Bizarre behavior of literal-substring parsing in formatting.c

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: pgsql-hackers(at)postgreSQL(dot)org
Subject: Bizarre behavior of literal-substring parsing in formatting.c
Date: 2017-11-17 20:11:26
Message-ID: 3626.1510949486@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

I started to look at handling multibyte data more sanely in formatting.c,
and soon noticed that the treatment of literal substrings in
parse_format() seemed overly complicated and underly correct, as well
as inadequately documented. In particular, it can't make up its mind
whether backslash is a quoting character or not. These cases make it
look like it is:

regression=# select to_char('100'::numeric, 'f"\ool"999');
to_char
----------
fool 100
(1 row)

regression=# select to_char('100'::numeric, 'f"\\ool"999');
to_char
-----------
f\ool 100
(1 row)

regression=# select to_char('100'::numeric, 'f"ool\"999');
to_char
----------
fool"999
(1 row)

(Since the second " is quoted, we never escape the literal substring,
and the format-code 999 becomes just literal data.) But try this:

regression=# select to_char('100'::numeric, 'f"ool\\"999');
to_char
-----------
fool\"999
(1 row)

The first backslash has quoted the second one, and then reached out
and quoted the quote too. That hardly seems sane.

Outside double quotes, it seems that backslash is only special when it
immediately precedes a double quote. That's not enormously consistent
with other string parsers in our code, but it's at least self-consistent,
and given the lack of complaints it's probably best not to change that
behavior.

Accordingly I offer the attached rewrite. Of the new regression
test cases, the only one that changes behavior from before is the
last one, which is the case I just showed.

If no objections, I'll push this shortly. I think putting it in
HEAD is enough, given the lack of field complaints.

regards, tom lane

Attachment Content-Type Size
fix-formatting.c-literal-parsing.patch text/x-diff 7.8 KB

Browse pgsql-hackers by date

  From Date Subject
Next Message Pavel Stehule 2017-11-17 20:40:12 Re: pspg - psql pager
Previous Message Robert Haas 2017-11-17 19:55:32 Re: bgwriter_lru_maxpages range in postgresql.conf