Re: [9.3 bug fix] ECPG does not escape backslashes

Lists: pgsql-hackers
From: "MauMau" <maumau307(at)gmail(dot)com>
To: <pgsql-hackers(at)postgresql(dot)org>
Subject: [9.3 bug fix] ECPG does not escape backslashes
Date: 2013-07-03 10:22:48
Message-ID: A0C361567285450883C37D254F58F054@maumau
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Hello,

I happened to find a trivial bug of ECPG while experimenting with 9.3 beta
2. Please find attached the patch to fix this. This is not specific to
9.3. Could you commit and backport this?

[Bug description]
Running "ecpg c:\command\a.pgc" produces the following line in a.c:

#line 1 "c:\command\a.pgc"

Then, compiling the resulting a.c with Visual Studio (cl.exe) issues the
warning:

a.c(8) : warning C4129: 'c' : unrecognized character escape sequence

This is because ecpg doesn't escape \ in the #line string.

[How to fix]
Escape \ in the input file name like this:

#line 1 "c:\\command\\a.pgc"

This is necessary not only on Windows but also on UNIX/Linux. For your
information, running "gcc -E di\\r/a.c" escapes \ and outputs the line:

# 1 "di\\r/a.c"

Regards
MauMau

Attachment Content-Type Size
ecpg_line.patch application/octet-stream 1.0 KB

From: Michael Meskes <meskes(at)postgresql(dot)org>
To: MauMau <maumau307(at)gmail(dot)com>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: [9.3 bug fix] ECPG does not escape backslashes
Date: 2013-07-04 11:04:07
Message-ID: 20130704110407.GA27740@feivel.credativ.lan
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Wed, Jul 03, 2013 at 07:22:48PM +0900, MauMau wrote:
> I happened to find a trivial bug of ECPG while experimenting with
> 9.3 beta 2. Please find attached the patch to fix this. This is
> not specific to 9.3. Could you commit and backport this?

This appears to be Windows specific. I don't have a Windows system to test
with. How does Visusal Studio handle #line entries with full path names? Are
they all escaped? Or better do they have to be?

> This is necessary not only on Windows but also on UNIX/Linux. For
> your information, running "gcc -E di\\r/a.c" escapes \ and outputs
> the line:
>
> # 1 "di\\r/a.c"

Now this statement surprises me:

michael(at)feivel:~$ ecpg test\\\\a/init.pgc
michael(at)feivel:~$ grep line test\\\\a/init.c |head -1
#line 1 "test\\a/init.pgc"
michael(at)feivel:~$ gcc -o i test\\\\a/init.c -I /usr/include/postgresql/ -l ecpg
michael(at)feivel:~$

This seems to suggest that it works nicely on Linux.

So what do ou mean when saying the problem also occurs on Linux?

Michael
--
Michael Meskes
Michael at Fam-Meskes dot De, Michael at Meskes dot (De|Com|Net|Org)
Michael at BorussiaFan dot De, Meskes at (Debian|Postgresql) dot Org
Jabber: michael.meskes at gmail dot com
VfL Borussia! Força Barça! Go SF 49ers! Use Debian GNU/Linux, PostgreSQL


From: Andrew Dunstan <andrew(at)dunslane(dot)net>
To: MauMau <maumau307(at)gmail(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: [9.3 bug fix] ECPG does not escape backslashes
Date: 2013-07-04 11:58:39
Message-ID: 51D5636F.8040602@dunslane.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers


On 07/04/2013 07:04 AM, Michael Meskes wrote:
> On Wed, Jul 03, 2013 at 07:22:48PM +0900, MauMau wrote:
>> I happened to find a trivial bug of ECPG while experimenting with
>> 9.3 beta 2. Please find attached the patch to fix this. This is
>> not specific to 9.3. Could you commit and backport this?
> This appears to be Windows specific. I don't have a Windows system to test
> with. How does Visusal Studio handle #line entries with full path names? Are
> they all escaped? Or better do they have to be?
>
>> This is necessary not only on Windows but also on UNIX/Linux. For
>> your information, running "gcc -E di\\r/a.c" escapes \ and outputs
>> the line:
>>
>> # 1 "di\\r/a.c"
> Now this statement surprises me:
>
> michael(at)feivel:~$ ecpg test\\\\a/init.pgc
> michael(at)feivel:~$ grep line test\\\\a/init.c |head -1
> #line 1 "test\\a/init.pgc"
> michael(at)feivel:~$ gcc -o i test\\\\a/init.c -I /usr/include/postgresql/ -l ecpg
> michael(at)feivel:~$
>
> This seems to suggest that it works nicely on Linux.
>

Really? I'd expect to see 4 backslashes in the #line directive, I think.

cheers

andrew


From: Michael Meskes <meskes(at)postgresql(dot)org>
To: Andrew Dunstan <andrew(at)dunslane(dot)net>
Cc: MauMau <maumau307(at)gmail(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: [9.3 bug fix] ECPG does not escape backslashes
Date: 2013-07-04 12:31:36
Message-ID: 20130704123136.GA8898@feivel.credativ.lan
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Thu, Jul 04, 2013 at 07:58:39AM -0400, Andrew Dunstan wrote:
> >michael(at)feivel:~$ grep line test\\\\a/init.c |head -1
> >#line 1 "test\\a/init.pgc"
> ...
>
> Really? I'd expect to see 4 backslashes in the #line directive, I think.

Eh, why? The four backslashes come are two that are escaped for shell usage.
The directory name is in my example was "test\\a". What did I miss?

Michael
--
Michael Meskes
Michael at Fam-Meskes dot De, Michael at Meskes dot (De|Com|Net|Org)
Michael at BorussiaFan dot De, Meskes at (Debian|Postgresql) dot Org
Jabber: michael.meskes at gmail dot com
VfL Borussia! Força Barça! Go SF 49ers! Use Debian GNU/Linux, PostgreSQL


From: Andrew Dunstan <andrew(at)dunslane(dot)net>
To: MauMau <maumau307(at)gmail(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: [9.3 bug fix] ECPG does not escape backslashes
Date: 2013-07-04 12:50:34
Message-ID: 51D56F9A.4060303@dunslane.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers


On 07/04/2013 08:31 AM, Michael Meskes wrote:
> On Thu, Jul 04, 2013 at 07:58:39AM -0400, Andrew Dunstan wrote:
>>> michael(at)feivel:~$ grep line test\\\\a/init.c |head -1
>>> #line 1 "test\\a/init.pgc"
>> ...
>>
>> Really? I'd expect to see 4 backslashes in the #line directive, I think.
> Eh, why? The four backslashes come are two that are escaped for shell usage.
> The directory name is in my example was "test\\a". What did I miss?
>

Isn't the argument to #line a C string literal in which one would expect
backslashes to be escaped? If not, how would it show a filename
containing a '"' character?

[andrew(at)emma inst.92.5701]$ bin/ecpg x\\\"a/y.pgc
[andrew(at)emma inst.92.5701]$ grep line x\\\"a/y.c
#line 1 "x\"a/y.pgc"

This must surely be wrong.

cheers

andrew


From: Andres Freund <andres(at)2ndquadrant(dot)com>
To: Andrew Dunstan <andrew(at)dunslane(dot)net>
Cc: MauMau <maumau307(at)gmail(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: [9.3 bug fix] ECPG does not escape backslashes
Date: 2013-07-04 12:58:18
Message-ID: 20130704125818.GA30965@awork2.anarazel.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On 2013-07-04 08:50:34 -0400, Andrew Dunstan wrote:
>
> On 07/04/2013 08:31 AM, Michael Meskes wrote:
> >On Thu, Jul 04, 2013 at 07:58:39AM -0400, Andrew Dunstan wrote:
> >>>michael(at)feivel:~$ grep line test\\\\a/init.c |head -1
> >>>#line 1 "test\\a/init.pgc"
> >>...
> >>
> >>Really? I'd expect to see 4 backslashes in the #line directive, I think.
> >Eh, why? The four backslashes come are two that are escaped for shell usage.
> >The directory name is in my example was "test\\a". What did I miss?
> >
>
> Isn't the argument to #line a C string literal in which one would expect
> backslashes to be escaped? If not, how would it show a filename containing a
> '"' character?
>
> [andrew(at)emma inst.92.5701]$ bin/ecpg x\\\"a/y.pgc
> [andrew(at)emma inst.92.5701]$ grep line x\\\"a/y.c
> #line 1 "x\"a/y.pgc"
>
> This must surely be wrong.

I think it's correct. Quoting the gcc manual
(http://gcc.gnu.org/onlinedocs/gcc-4.8.1/cpp/Include-Syntax.html#Include-Syntax)
"However, if backslashes occur within file, they are considered ordinary
text characters, not escape characters. None of the character escape
sequences appropriate to string constants in C are processed. Thus,
#include "x\n\\y" specifies a filename containing three
backslashes. (Some systems interpret ‘\’ as a pathname separator. All of
these also interpret ‘/’ the same way. It is most portable to use only
‘/’.)"

Greetings,

Andres Freund

--
Andres Freund http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services


From: Andrew Dunstan <andrew(at)dunslane(dot)net>
To: Andres Freund <andres(at)2ndquadrant(dot)com>
Cc: MauMau <maumau307(at)gmail(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: [9.3 bug fix] ECPG does not escape backslashes
Date: 2013-07-04 13:12:37
Message-ID: 51D574C5.8090907@dunslane.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers


On 07/04/2013 08:58 AM, Andres Freund wrote:
> On 2013-07-04 08:50:34 -0400, Andrew Dunstan wrote:
>> On 07/04/2013 08:31 AM, Michael Meskes wrote:
>>> On Thu, Jul 04, 2013 at 07:58:39AM -0400, Andrew Dunstan wrote:
>>>>> michael(at)feivel:~$ grep line test\\\\a/init.c |head -1
>>>>> #line 1 "test\\a/init.pgc"
>>>> ...
>>>>
>>>> Really? I'd expect to see 4 backslashes in the #line directive, I think.
>>> Eh, why? The four backslashes come are two that are escaped for shell usage.
>>> The directory name is in my example was "test\\a". What did I miss?
>>>
>> Isn't the argument to #line a C string literal in which one would expect
>> backslashes to be escaped? If not, how would it show a filename containing a
>> '"' character?
>>
>> [andrew(at)emma inst.92.5701]$ bin/ecpg x\\\"a/y.pgc
>> [andrew(at)emma inst.92.5701]$ grep line x\\\"a/y.c
>> #line 1 "x\"a/y.pgc"
>>
>> This must surely be wrong.
> I think it's correct. Quoting the gcc manual
> (http://gcc.gnu.org/onlinedocs/gcc-4.8.1/cpp/Include-Syntax.html#Include-Syntax)
> "However, if backslashes occur within file, they are considered ordinary
> text characters, not escape characters. None of the character escape
> sequences appropriate to string constants in C are processed. Thus,
> #include "x\n\\y" specifies a filename containing three
> backslashes. (Some systems interpret ‘\’ as a pathname separator. All of
> these also interpret ‘/’ the same way. It is most portable to use only
> ‘/’.)"

Well, that refers to #include, but for the sake of argument I'll assume
the same rule applies to #line. So this just gets processed by stripping
the surrounding quotes? Well I guess I learn something every day.

cheers

andrew


From: Andres Freund <andres(at)2ndquadrant(dot)com>
To: Andrew Dunstan <andrew(at)dunslane(dot)net>
Cc: MauMau <maumau307(at)gmail(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: [9.3 bug fix] ECPG does not escape backslashes
Date: 2013-07-04 13:18:12
Message-ID: 20130704131812.GB1403@awork2.anarazel.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On 2013-07-04 09:12:37 -0400, Andrew Dunstan wrote:
>
> On 07/04/2013 08:58 AM, Andres Freund wrote:
> >On 2013-07-04 08:50:34 -0400, Andrew Dunstan wrote:
> >>On 07/04/2013 08:31 AM, Michael Meskes wrote:
> >>>On Thu, Jul 04, 2013 at 07:58:39AM -0400, Andrew Dunstan wrote:
> >>>>>michael(at)feivel:~$ grep line test\\\\a/init.c |head -1
> >>>>>#line 1 "test\\a/init.pgc"
> >>>>...
> >>>>
> >>>>Really? I'd expect to see 4 backslashes in the #line directive, I think.
> >>>Eh, why? The four backslashes come are two that are escaped for shell usage.
> >>>The directory name is in my example was "test\\a". What did I miss?
> >>>
> >>Isn't the argument to #line a C string literal in which one would expect
> >>backslashes to be escaped? If not, how would it show a filename containing a
> >>'"' character?
> >>
> >> [andrew(at)emma inst.92.5701]$ bin/ecpg x\\\"a/y.pgc
> >> [andrew(at)emma inst.92.5701]$ grep line x\\\"a/y.c
> >> #line 1 "x\"a/y.pgc"
> >>
> >>This must surely be wrong.
> >I think it's correct. Quoting the gcc manual
> >(http://gcc.gnu.org/onlinedocs/gcc-4.8.1/cpp/Include-Syntax.html#Include-Syntax)
> >"However, if backslashes occur within file, they are considered ordinary
> >text characters, not escape characters. None of the character escape
> >sequences appropriate to string constants in C are processed. Thus,
> >#include "x\n\\y" specifies a filename containing three
> >backslashes. (Some systems interpret ‘\’ as a pathname separator. All of
> >these also interpret ‘/’ the same way. It is most portable to use only
> >‘/’.)"
>
> Well, that refers to #include, but for the sake of argument I'll assume the
> same rule applies to #line. So this just gets processed by stripping the
> surrounding quotes? Well I guess I learn something every day.

Gah. You're right. I only remembered the rules for #include and thought
that would be applicable. But:
http://gcc.gnu.org/onlinedocs/gcc-4.8.1/cpp/Line-Control.html#Line-Control :
»filename is interpreted according to the normal rules for a string
constant: backslash escapes are interpreted. This is different from
‘#include’.

Previous versions of CPP did not interpret escapes in ‘#line’; we have
changed it because the standard requires they be interpreted, and most
other compilers do.«

Greetings,

Andres Freund

--
Andres Freund http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services


From: Michael Meskes <meskes(at)postgresql(dot)org>
To: Andres Freund <andres(at)2ndquadrant(dot)com>
Cc: Andrew Dunstan <andrew(at)dunslane(dot)net>, MauMau <maumau307(at)gmail(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: [9.3 bug fix] ECPG does not escape backslashes
Date: 2013-07-04 13:51:06
Message-ID: 20130704135106.GA14980@feivel.credativ.lan
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Thu, Jul 04, 2013 at 03:18:12PM +0200, Andres Freund wrote:
> Previous versions of CPP did not interpret escapes in ‘#line’; we have
> changed it because the standard requires they be interpreted, and most
> other compilers do.«

So that means MauMau was right and backslashes have to be escaped in filenames
in #line directives, right? Apparently my examples were badly chosen as I
didn't see an error no matter how many backslashes I had.

Michael
--
Michael Meskes
Michael at Fam-Meskes dot De, Michael at Meskes dot (De|Com|Net|Org)
Michael at BorussiaFan dot De, Meskes at (Debian|Postgresql) dot Org
Jabber: michael.meskes at gmail dot com
VfL Borussia! Força Barça! Go SF 49ers! Use Debian GNU/Linux, PostgreSQL


From: Michael Meskes <meskes(at)postgresql(dot)org>
To: MauMau <maumau307(at)gmail(dot)com>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: [9.3 bug fix] ECPG does not escape backslashes
Date: 2013-07-05 09:16:44
Message-ID: 20130705091644.GA14058@feivel.credativ.lan
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Wed, Jul 03, 2013 at 07:22:48PM +0900, MauMau wrote:
> not specific to 9.3. Could you commit and backport this?

Committed to 8.4, 9.0, 9.1, 9.2 and HEAD.

Michael
--
Michael Meskes
Michael at Fam-Meskes dot De, Michael at Meskes dot (De|Com|Net|Org)
Michael at BorussiaFan dot De, Meskes at (Debian|Postgresql) dot Org
Jabber: michael.meskes at gmail dot com
VfL Borussia! Força Barça! Go SF 49ers! Use Debian GNU/Linux, PostgreSQL


From: "MauMau" <maumau307(at)gmail(dot)com>
To: "Michael Meskes" <meskes(at)postgresql(dot)org>
Cc: <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: [9.3 bug fix] ECPG does not escape backslashes
Date: 2013-07-05 11:32:50
Message-ID: 5F5F408615BF40AA924506241C5B569E@maumau
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

From: "Michael Meskes" <meskes(at)postgresql(dot)org>
> So that means MauMau was right and backslashes have to be escaped in
> filenames
> in #line directives, right? Apparently my examples were badly chosen as I
> didn't see an error no matter how many backslashes I had.

Yes, the below examples shows the case:

[maumau(at)myhost ~]$ touch ab\\c/a.pgc
[maumau(at)myhost ~]$ ecpg ab\\c/a.pgc
[maumau(at)myhost ~]$ gcc -c -I/tuna/pgsql/include ab\\c/a.c
ab\c/a.c:8:9: warning: unknown escape sequence '\c'
[maumau(at)myhost ~]$

> Committed to 8.4, 9.0, 9.1, 9.2 and HEAD.

Thank you very much for your quick support.

Regards
MauMau


From: Andrew Dunstan <andrew(at)dunslane(dot)net>
To: MauMau <maumau307(at)gmail(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: [9.3 bug fix] ECPG does not escape backslashes
Date: 2013-07-05 12:08:06
Message-ID: 51D6B726.9040505@dunslane.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers


On 07/05/2013 05:16 AM, Michael Meskes wrote:
> On Wed, Jul 03, 2013 at 07:22:48PM +0900, MauMau wrote:
>> not specific to 9.3. Could you commit and backport this?
> Committed to 8.4, 9.0, 9.1, 9.2 and HEAD.
>

This looks incomplete. Surely just escaping backslashes alone is not
enough. I suspect at least the " char and any chars below 0x20 should be
quoted also.

cheers

andrew


From: Michael Meskes <meskes(at)postgresql(dot)org>
To: Andrew Dunstan <andrew(at)dunslane(dot)net>
Cc: MauMau <maumau307(at)gmail(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: [9.3 bug fix] ECPG does not escape backslashes
Date: 2013-07-05 12:58:44
Message-ID: 20130705125844.GA12214@feivel.credativ.lan
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Fri, Jul 05, 2013 at 08:08:06AM -0400, Andrew Dunstan wrote:
> This looks incomplete. Surely just escaping backslashes alone is not
> enough. I suspect at least the " char and any chars below 0x20
> should be quoted also.

Right, this didn't even occur to me, but there are surely more characters that
need to be escaped. Gotta dig into this.

Thanks for pointing this out.

Michael
--
Michael Meskes
Michael at Fam-Meskes dot De, Michael at Meskes dot (De|Com|Net|Org)
Michael at BorussiaFan dot De, Meskes at (Debian|Postgresql) dot Org
Jabber: michael.meskes at gmail dot com
VfL Borussia! Força Barça! Go SF 49ers! Use Debian GNU/Linux, PostgreSQL


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Michael Meskes <meskes(at)postgresql(dot)org>
Cc: MauMau <maumau307(at)gmail(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: [9.3 bug fix] ECPG does not escape backslashes
Date: 2013-07-05 13:41:26
Message-ID: 17778.1373031686@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Michael Meskes <meskes(at)postgresql(dot)org> writes:
> On Wed, Jul 03, 2013 at 07:22:48PM +0900, MauMau wrote:
>> not specific to 9.3. Could you commit and backport this?

> Committed to 8.4, 9.0, 9.1, 9.2 and HEAD.

Um ... 9.3 is a separate branch now, please fix it there also.

regards, tom lane


From: Michael Meskes <meskes(at)postgresql(dot)org>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Michael Meskes <meskes(at)postgresql(dot)org>, MauMau <maumau307(at)gmail(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: [9.3 bug fix] ECPG does not escape backslashes
Date: 2013-07-06 09:57:39
Message-ID: 20130706095739.GA17419@feivel.credativ.lan
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Fri, Jul 05, 2013 at 09:41:26AM -0400, Tom Lane wrote:
> Um ... 9.3 is a separate branch now, please fix it there also.

Done. Seems I missed a new branch - yet again. Sorry and thanks for pointing it
out to me.

michael
--
Michael Meskes
Michael at Fam-Meskes dot De, Michael at Meskes dot (De|Com|Net|Org)
Michael at BorussiaFan dot De, Meskes at (Debian|Postgresql) dot Org
Jabber: michael.meskes at gmail dot com
VfL Borussia! Força Barça! Go SF 49ers! Use Debian GNU/Linux, PostgreSQL


From: Michael Meskes <meskes(at)postgresql(dot)org>
To: Andrew Dunstan <andrew(at)dunslane(dot)net>
Cc: MauMau <maumau307(at)gmail(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: [9.3 bug fix] ECPG does not escape backslashes
Date: 2013-07-06 20:15:18
Message-ID: 20130706201518.GA24774@feivel.credativ.lan
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Fri, Jul 05, 2013 at 08:08:06AM -0400, Andrew Dunstan wrote:
> This looks incomplete. Surely just escaping backslashes alone is not
> enough. I suspect at least the " char and any chars below 0x20
> should be quoted also.

The " char I just added, however, my tests did bring up any problem with chars
below 0x20. If anybody sees another character breaking ecpg, please tell me and
I'll fix it.

Michael
--
Michael Meskes
Michael at Fam-Meskes dot De, Michael at Meskes dot (De|Com|Net|Org)
Michael at BorussiaFan dot De, Meskes at (Debian|Postgresql) dot Org
Jabber: michael.meskes at gmail dot com
VfL Borussia! Força Barça! Go SF 49ers! Use Debian GNU/Linux, PostgreSQL