8000 Fix handling of array of char pointers in ecpglib. · larkly/postgres-docker@fb66e88 · GitHub
[go: up one dir, main page]

Skip to content

Commit fb66e88

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 2f4ee3a commit fb66e88

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
@@ -407,7 +407,8 @@ ECPGdump_a_simple(FILE *o, const char *name, enum ECPGttype type,
407407
case ECPGt_unsigned_char:
408408
case ECPGt_char_variable:
409409
case ECPGt_string:
410-
410+
{
411+
char *sizeof_name = "char";
411412
/*
412413
* we have to use the pointer except for arrays with given
413414
* bounds, ecpglib will distinguish between * and []
@@ -417,12 +418,24 @@ ECPGdump_a_simple(FILE *o, const char *name, enum ECPGttype type,
417418
(atoi(varcharsize) == 0 && strcmp(varcharsize, "0") != 0) ||
418419
(atoi(arrsize) == 0 && strcmp(arrsize, "0") != 0))
419420
&& siz == NULL)
421+
{
420422
sprintf(variable, "(%s%s)", prefix ? prefix : "", name);
423+
if ((type == ECPGt_char || type == ECPGt_unsigned_char) &&
424+
strcmp(varcharsize, "0") == 0)
425+
{
426+
/*
427+
* If this is an array of char *, the offset would be
428+
* sizeof(char *) and not sizeof(char).
429+
*/
430+
sizeof_name = "char *";
431+
}
432+
}
421433
else
422434
sprintf(variable, "&(%s%s)", prefix ? prefix : "", name);
423435

424-
sprintf(offset, "(%s)*sizeof(char)", strcmp(varcharsize, "0") == 0 ? "1" : varcharsize);
436+
sprintf(offset, "(%s)*sizeof(%s)", strcmp(varcharsize, "0") == 0 ? "1" : varcharsize, sizeof_name);
425437
break;
438+ }
426439
case ECPGt_numeric:
427440

428441
/*

0 commit comments

Comments
 (0)
0