BUG #8608: ECPG: sizeof() in EXEC SQL DECLARE SECTION

Started by Alexei Savchikover 12 years ago6 messagesbugs
Jump to latest
#1Alexei Savchik
alexsav23@gmail.com

The following bug has been logged on the website:

Bug reference: 8608
Logged by: Alexei Savchik
Email address: alexsav23@gmail.com
PostgreSQL version: 9.3.1
Operating system: Windows 8 x64
Description:

void MyFunc( MyClass*unit_address )
{
EXEC SQL BEGIN DECLARE SECTION;
unsigned char var1[sizeof(MyClass)];
EXEC SQL END DECLARE SECTION;
}

d:\>"c:\Program Files\PostgreSQL\9.3\bin\ecpg.exe"-o 1.c 1.pgc
1.pgc: 4: ERROR: syntax error at or near "("
error deleting output file "1.c"

http://stackoverflow.com/questions/20049432/ecpg-sizeof-in-exec-sql-declare-section

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

#2Michael Meskes
meskes@postgresql.org
In reply to: Alexei Savchik (#1)
Re: BUG #8608: ECPG: sizeof() in EXEC SQL DECLARE SECTION

On Tue, Nov 19, 2013 at 10:39:48PM +0000, alexsav23@gmail.com wrote:

d:\>"c:\Program Files\PostgreSQL\9.3\bin\ecpg.exe"-o 1.c 1.pgc
1.pgc: 4: ERROR: syntax error at or near "("
error deleting output file "1.c"

And another patch to try. Please send me feedback.

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

Attachments:

ecpg.sizeof.difftext/x-diff; charset=us-asciiDownload+5-0
#3Boszormenyi Zoltan
zb@cybertec.at
In reply to: Michael Meskes (#2)
Re: BUG #8608: ECPG: sizeof() in EXEC SQL DECLARE SECTION

2013-11-24 12:55 keltez�ssel, Michael Meskes �rta:

On Tue, Nov 19, 2013 at 10:39:48PM +0000, alexsav23@gmail.com wrote:

d:\>"c:\Program Files\PostgreSQL\9.3\bin\ecpg.exe"-o 1.c 1.pgc
1.pgc: 4: ERROR: syntax error at or near "("
error deleting output file "1.c"

And another patch to try. Please send me feedback.

I think it's needlessly narrows the scope of the problem.
Consider this code:

#include <stdio.h>

#define hide_size_of_var sizeof

int main(void) {
int a[hide_size_of_var(int)];
int i;

for (i = 0; i < hide_size_of_var(int); i++) {
a[i] = i*3;
printf("i: %d a[i]: %d\n", i, a[i]);
}

return 0;
}

It compiles just fine in a C compiler and runs from 0 to 3 as expected.
GCC 4.8.2 doesn't say anything about it with -Wall.

I think you should pass everything through as is inside the '[' and ']' and
let the C compiler fail if the code is not valid.

Best regards,
Zolt�n B�sz�rm�nyi

Michael

--
----------------------------------
Zolt�n B�sz�rm�nyi
Cybertec Sch�nig & Sch�nig GmbH
Gr�hrm�hlgasse 26
A-2700 Wiener Neustadt, Austria
Web: http://www.postgresql-support.de
http://www.postgresql.at/

#4Michael Meskes
meskes@postgresql.org
In reply to: Boszormenyi Zoltan (#3)
Re: BUG #8608: ECPG: sizeof() in EXEC SQL DECLARE SECTION

On Sun, Nov 24, 2013 at 03:59:40PM +0100, Boszormenyi Zoltan wrote:

I think it's needlessly narrows the scope of the problem.
Consider this code:

#include <stdio.h>

#define hide_size_of_var sizeof
...

I assume you'd want to put this defined name into a declare section, right?
Well, that's what EXEC SQL DECLARE is for, isn't it?

I think you should pass everything through as is inside the '[' and ']' and
let the C compiler fail if the code is not valid.

I prefer catching as much as possible without re-implementing a C-compiler of course.

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

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

#5Boszormenyi Zoltan
zb@cybertec.at
In reply to: Michael Meskes (#4)
Re: BUG #8608: ECPG: sizeof() in EXEC SQL DECLARE SECTION

2013-11-25 09:18 keltezéssel, Michael Meskes írta:

On Sun, Nov 24, 2013 at 03:59:40PM +0100, Boszormenyi Zoltan wrote:

I think it's needlessly narrows the scope of the problem.
Consider this code:

#include <stdio.h>

#define hide_size_of_var sizeof
...

I assume you'd want to put this defined name into a declare section, right?
Well, that's what EXEC SQL DECLARE is for, isn't it?

Of course. And of course you meant EXEC SQL DEFINE.

The attached code throws syntax errors over sizeof(int) and
sizeof(struct somestruct). I compiled GIT HEAD which contains your fix:

commit 08d1b22b3be2305ad6b83ca275829ff26305f5d7
Author: Michael Meskes <meskes@postgresql.org>
Date: Sun Nov 24 12:51:21 2013 +0100

Allow C array definitions to use sizeof().

When parsing C variable definitions ecpg should allow sizeof() operators as array
dimensions.

So it seems it's not enough, try these with the attached code:

Succeeds:
$ ecpg array-sizeof.pgc

These fail:
$ ecpg -D TRY_INT array-sizeof.pgc
array-sizeof.pgc:24: ERROR: syntax error at or near "int"

$ ecpg -D TRY_STRUCT array-sizeof.pgc
array-sizeof.pgc:30: ERROR: syntax error at or near "struct"

The attached patch fixes "sizeof(struct something)" but the
"simple_type" variant that should also accept
[signed|unsigned]{char|short|int|long|long long}
causes 2 shift/reduce problems when processing preproc.y.

Best regards,
Zoltán Böszörményi

I think you should pass everything through as is inside the '[' and ']' and
let the C compiler fail if the code is not valid.

I prefer catching as much as possible without re-implementing a C-compiler of course.

Michael

--
----------------------------------
Zoltán Böszörményi
Cybertec Schönig & Schönig GmbH
Gröhrmühlgasse 26
A-2700 Wiener Neustadt, Austria
Web: http://www.postgresql-support.de
http://www.postgresql.at/

Attachments:

array-sizeof.pgctext/plain; charset=UTF-8; name=array-sizeof.pgcDownload
array-sizeof-struct-fix.patchtext/x-patch; name=array-sizeof-struct-fix.patchDownload+16-2
#6Michael Meskes
meskes@postgresql.org
In reply to: Boszormenyi Zoltan (#5)
Re: BUG #8608: ECPG: sizeof() in EXEC SQL DECLARE SECTION

On Mon, Nov 25, 2013 at 02:19:13PM +0100, Boszormenyi Zoltan wrote:

Of course. And of course you meant EXEC SQL DEFINE.

Yes. :)

The attached code throws syntax errors over sizeof(int) and
sizeof(struct somestruct). I compiled GIT HEAD which contains your fix:

Thanks for spotting this. Using ColId as type name was pretty stupid of me, in particular when a non-terminal var_type already exists.

The attached patch fixes "sizeof(struct something)" but the
"simple_type" variant that should also accept
[signed|unsigned]{char|short|int|long|long long}
causes 2 shift/reduce problems when processing preproc.y.

That's because with the additional rules we add CHAR and DOUBLE twice. Should be fixed now.

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

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