[HACKERS] Pointer to structure in ECPG

2014-04-09 Thread Ashutosh Bapat
Hi All,
I tried to use pointer to array to fetch results of a query. The test case
test_select.pgc is attached. Changes specific to one's environment are
needed before that test can be tried. Otherwise, you may try file
pointer_to_struct.pgc in the patch attached, by putting it ecpg/test
directory.

The file test_select.pgc compiles fine but when run it produces error -203
and produces garbage because the result of FETCH was not saved in the array
pointed by pointer.

./test_select
SQL error: SQL error -203 on line 40
empno=-163754450, ename=, job=
empno=634004672, ename=�, job=
empno=634004624, ename=�, job=

This happens because of two problems
1. When dumping individual members of structure emp in test_select.pgc,
when a pointer to that structure is dumped, its array size is dumped as 1
instead of 0 or a negative number like scalar variable pointers.

2. As discussed in other thread using arrays within structure in ECPG,
the offsets of members of structures are not dumped correctly for an
unbounded array.

Problem 1 reason:
-
When dumping individual members of structure emp in test_select.pgc, when a
pointer to that structure is dumped, the call is routed through code path
as shown below

*#0*  *ECPGdump_a_type* (o=0x6cc4f0, *name=0x6d2190 empno*,
type=0x6d2130, brace_level=-1, ind_name=0x4c0ae8 no_indicator,
ind_type=0x6c8260, ind_brace_l
evel=-1, *prefix=0x6d1a20 emp2-*, ind_prefix=0x0, *arr_str_siz=0x6d2070
0,* *struct_sizeof=0x6d0c60 sizeof( struct employee )*,
ind_struct_sizeof=0x0)
 at type.c:247
*#1*  0x0042f21d in *ECPGdump_a_struct* (o=0x6cc4f0, *name=0x6d20b0
emp2*, ind_name=0x435c20 no_indicator, *arrsiz=0x6d2070 0*,
type=0x6d20d0, in
d_type=0x6c8260, *prefix=0x6d1a20 emp2-*, ind_prefix=0x0) at type.c:572
*#2*  0x0042e2cc in *ECPGdump_a_type *(o=0x6cc4f0, *name=0x6d20b0
emp2*, type=0x6d22b0, brace_level=1, ind_name=0x435c20 no_indicator,
ind_type=0
x6c8260, ind_brace_level=0, prefix=0x0, ind_prefix=0x0, *arr_str_siz=0x6d1e60
0*, *struct_sizeof=0x0*, ind_struct_sizeof=0x0) at type.c:293
*#3*  0x0043335d in *dump_variables* (list=0x6d2310, mode=1) at
variable.c:452

ECPGdump_a_type() at frame #3, then calls ECPGdump_a_simple() as follows
#0  *ECPGdump_a_simple* (o=0x6cc4f0, *name=0x6d2190 empno*,
type=ECPGt_int, varcharsize=0x6d0f70 1,* arrsize=0x6d2360 -1*,
*siz=0x6d0c60 sizeof( struct employee )*, prefix=0x6d1a20 emp2-,
counter=0) at type.c:405

ECPGdump_a_simple() while dumping the member (here empno) dumps with
arrsize = 1 instead of -1 because of code at line
523│ if (atoi(arrsize)  0)
524│ strcpy(arrsize, 1);

In ECPG run time library when reading this variable, it is read with array
size 1 and is falsely interpreted as an array with size 1 instead of an
unbounded array (which are read with arrsize  0). Hence giving error -203.

ECPGdump_a_struct has correctly called ECPGdump_a_type with arrsize 0 to
mean an unbounded array, and ECPGdump_a_type() has correctly converted it
to -1 again to differentiate between pointer to struct from pointer to
scalar variable. This differentiation is important to dump pointer to
struct and pointer to scalar variable differently in ECPGdump_a_simple().
So, in ECPGdump_a_simple() we should dump the variable with arrsize -1 if
it's part of an outer structure.

The patch attached, includes solutions to both the problems and also a
testcase preproc/pointer_to_struct.pgc. This test is a copy of test
preproc/array_of_struct.pgc with the structure arrays replaced by pointers
to structures and memory allocations.

The patch is based on development head. Please consider this to be
backpatched to 9.3 as well.

-- 
Best Wishes,
Ashutosh Bapat
EnterpriseDB Corporation
The Postgres Database Company


test_select.pgc
Description: Binary data
diff --git a/src/interfaces/ecpg/preproc/type.c b/src/interfaces/ecpg/preproc/type.c
index 2982cb6..dbe2d7e 100644
--- a/src/interfaces/ecpg/preproc/type.c
+++ b/src/interfaces/ecpg/preproc/type.c
@@ -512,25 +512,33 @@ ECPGdump_a_simple(FILE *o, const char *name, enum ECPGttype type,
 if (((atoi(arrsize)  0) ||
 	 (atoi(arrsize) == 0  strcmp(arrsize, 0) != 0)) 
 	siz == NULL)
 	sprintf(variable, (%s%s), prefix ? prefix : , name);
 else
 	sprintf(variable, (%s%s), prefix ? prefix : , name);
 
 sprintf(offset, sizeof(%s), ecpg_type_name(type));
 break;
 		}
-
-		if (atoi(arrsize)  0)
+		
+		/*
+		 * Array size would be -1 for addresses of members within structure,
+		 * when pointer to structure is being dumped.
+		 */
+		if (atoi(arrsize)  0  !siz)
 			strcpy(arrsize, 1);
 
-		if (siz == NULL || strlen(siz) == 0 || strcmp(arrsize, 0) == 0 || strcmp(arrsize, 1) == 0)
+		/*
+		 * If siz i.e. the size of structure of which this variable is part of,
+		 * that gives the offset to the next element, if required 
+		 */
+		if (siz == NULL || strlen(siz) == 0)
 			fprintf(o, \n\t%s,%s,(long)%s,(long)%s,%s, , 

Re: [HACKERS] Pointer to structure in ECPG

2014-04-09 Thread Michael Meskes
Thanks Ashutosh,

both patches committed and backported to the whole 9.* series.

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-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers


Re: [HACKERS] Pointer to structure in ECPG

2014-04-09 Thread Ashutosh Bapat
Thanks a lot Michael.


On Wed, Apr 9, 2014 at 3:59 PM, Michael Meskes mes...@postgresql.orgwrote:

 Thanks Ashutosh,

 both patches committed and backported to the whole 9.* series.

 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




-- 
Best Wishes,
Ashutosh Bapat
EnterpriseDB Corporation
The Postgres Database Company