Skip to content

Commit 3a024c1

Browse files
author
Michael Meskes
committed
Fix handling of array of char pointers in ecpglib.
When array of char * was used as target for a FETCH statement returning more than one row, it tried to store all the result in the first element. Instead it should dump array of char pointers with right offset, use the address instead of the value of the C variable while reading the array and treat such variable as char **, instead of char * for pointer arithmetic. Patch by Ashutosh Bapat <ashutosh.bapat@enterprisedb.com>
1 parent c8fbeeb commit 3a024c1

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

src/interfaces/ecpg/ecpglib/data.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,14 @@ ecpg_get_data(const PGresult *results, int act_tuple, int act_field, int lineno,
455455
{
456456
char *str = (char *) (var + offset * act_tuple);
457457

458+
/*
459+
* If varcharsize is unknown and the offset is that of
460+
* char *, then this variable represents the array of
461+
* character pointers. So, use extra indirection.
462+
*/
463+
if (varcharsize == 0 && offset == sizeof(char *))
464+
str = *(char **)str;
465+
458466
if (varcharsize == 0 || varcharsize > size)
459467
{
460468
strncpy(str, pval, size + 1);

src/interfaces/ecpg/ecpglib/execute.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1850,7 +1850,14 @@ ECPGdo(const int lineno, const int compat, const int force_indicator, const char
18501850
var->arrsize = va_arg(args, long);
18511851
var->offset = va_arg(args, long);
18521852

1853-
if (var->arrsize == 0 || var->varcharsize == 0)
1853+
/*
1854+
* Unknown array size means pointer to an array.
1855+
* Unknown varcharsize usually also means pointer. But if the
1856+
* type is character and the array size is known, it is an
1857+
* array of pointers to char, so use var->pointer as it is.
1858+
*/
1859+
if (var->arrsize == 0 ||
1860+
(var->varcharsize == 0 && ((var->type != ECPGt_char && var->type != ECPGt_unsigned_char) || (var->arrsize <= 1))))
18541861
var->value = *((char **) (var->pointer));
18551862
else
18561863
var->value = var->pointer;

src/interfaces/ecpg/preproc/type.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,8 @@ ECPGdump_a_simple(FILE *o, const char *name, enum ECPGttype type,
410410
case ECPGt_unsigned_char:
411411
case ECPGt_char_variable:
412412
case ECPGt_string:
413-
413+
{
414+
char *sizeof_name = "char";
414415
/*
415416
* we have to use the pointer except for arrays with given
416417
* bounds, ecpglib will distinguish between * and []
@@ -420,12 +421,24 @@ ECPGdump_a_simple(FILE *o, const char *name, enum ECPGttype type,
420421
(atoi(varcharsize) == 0 && strcmp(varcharsize, "0") != 0) ||
421422
(atoi(arrsize) == 0 && strcmp(arrsize, "0") != 0))
422423
&& siz == NULL)
424+
{
423425
sprintf(variable, "(%s%s)", prefix ? prefix : "", name);
426+
if ((type == ECPGt_char || type == ECPGt_unsigned_char) &&
427+
strcmp(varcharsize, "0") == 0)
428+
{
429+
/*
430+
* If this is an array of char *, the offset would be
431+
* sizeof(char *) and not sizeof(char).
432+
*/
433+
sizeof_name = "char *";
434+
}
435+
}
424436
else
425437
sprintf(variable, "&(%s%s)", prefix ? prefix : "", name);
426438

427-
sprintf(offset, "(%s)*sizeof(char)", strcmp(varcharsize, "0") == 0 ? "1" : varcharsize);
439+
sprintf(offset, "(%s)*sizeof(%s)", strcmp(varcharsize, "0") == 0 ? "1" : varcharsize, sizeof_name);
428440
break;
441+
}
429442
case ECPGt_numeric:
430443

431444
/*

0 commit comments

Comments
 (0)