From 2d280ef3fbb9af6460a33b24ee9cf5012384875e Mon Sep 17 00:00:00 2001 From: Michael Meskes Date: Fri, 18 Aug 2006 16:33:50 +0000 Subject: [PATCH] Backported buffer overrun fix from HEAD --- src/interfaces/ecpg/ecpglib/execute.c | 64 ++++++++++++++------------- 1 file changed, 33 insertions(+), 31 deletions(-) diff --git a/src/interfaces/ecpg/ecpglib/execute.c b/src/interfaces/ecpg/ecpglib/execute.c index e1a2091e15..db29b490a1 100644 --- a/src/interfaces/ecpg/ecpglib/execute.c +++ b/src/interfaces/ecpg/ecpglib/execute.c @@ -1,4 +1,4 @@ -/* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/execute.c,v 1.38.4.9 2006/07/05 10:50:20 meskes Exp $ */ +/* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/execute.c,v 1.38.4.10 2006/08/18 16:33:50 meskes Exp $ */ /* * The aim is to get a simpler inteface to the database routines. @@ -578,19 +578,21 @@ ECPGstore_input(const int lineno, const bool force_indicator, const struct varia } if (**tobeinserted_p == '\0') { + int asize = var->arrsize? var->arrsize : 1; + switch (var->type) { int element; case ECPGt_short: - if (!(mallocedval = ECPGalloc(var->arrsize * 20, lineno))) + if (!(mallocedval = ECPGalloc(asize * 20, lineno))) return false; - if (var->arrsize > 1) + if (asize > 1) { strcpy(mallocedval, "array ["); - for (element = 0; element < var->arrsize; element++) + for (element = 0; element < asize; element++) sprintf(mallocedval + strlen(mallocedval), "%hd,", ((short *) var->value)[element]); strcpy(mallocedval + strlen(mallocedval) - 1, "]"); @@ -603,14 +605,14 @@ ECPGstore_input(const int lineno, const bool force_indicator, const struct varia break; case ECPGt_int: - if (!(mallocedval = ECPGalloc(var->arrsize * 20, lineno))) + if (!(mallocedval = ECPGalloc(asize * 20, lineno))) return false; - if (var->arrsize > 1) + if (asize > 1) { strcpy(mallocedval, "array ["); - for (element = 0; element < var->arrsize; element++) + for (element = 0; element < asize; element++) sprintf(mallocedval + strlen(mallocedval), "%d,", ((int *) var->value)[element]); strcpy(mallocedval + strlen(mallocedval) - 1, "]"); @@ -623,14 +625,14 @@ ECPGstore_input(const int lineno, const bool force_indicator, const struct varia break; case ECPGt_unsigned_short: - if (!(mallocedval = ECPGalloc(var->arrsize * 20, lineno))) + if (!(mallocedval = ECPGalloc(asize * 20, lineno))) return false; - if (var->arrsize > 1) + if (asize > 1) { strcpy(mallocedval, "array ["); - for (element = 0; element < var->arrsize; element++) + for (element = 0; element < asize; element++) sprintf(mallocedval + strlen(mallocedval), "%hu,", ((unsigned short *) var->value)[element]); strcpy(mallocedval + strlen(mallocedval) - 1, "]"); @@ -643,14 +645,14 @@ ECPGstore_input(const int lineno, const bool force_indicator, const struct varia break; case ECPGt_unsigned_int: - if (!(mallocedval = ECPGalloc(var->arrsize * 20, lineno))) + if (!(mallocedval = ECPGalloc(asize * 20, lineno))) return false; - if (var->arrsize > 1) + if (asize > 1) { strcpy(mallocedval, "array ["); - for (element = 0; element < var->arrsize; element++) + for (element = 0; element < asize; element++) sprintf(mallocedval + strlen(mallocedval), "%u,", ((unsigned int *) var->value)[element]); strcpy(mallocedval + strlen(mallocedval) - 1, "]"); @@ -663,14 +665,14 @@ ECPGstore_input(const int lineno, const bool force_indicator, const struct varia break; case ECPGt_long: - if (!(mallocedval = ECPGalloc(var->arrsize * 20, lineno))) + if (!(mallocedval = ECPGalloc(asize * 20, lineno))) return false; - if (var->arrsize > 1) + if (asize > 1) { strcpy(mallocedval, "array ["); - for (element = 0; element < var->arrsize; element++) + for (element = 0; element < asize; element++) sprintf(mallocedval + strlen(mallocedval), "%ld,", ((long *) var->value)[element]); strcpy(mallocedval + strlen(mallocedval) - 1, "]"); @@ -683,14 +685,14 @@ ECPGstore_input(const int lineno, const bool force_indicator, const struct varia break; case ECPGt_unsigned_long: - if (!(mallocedval = ECPGalloc(var->arrsize * 20, lineno))) + if (!(mallocedval = ECPGalloc(asize * 20, lineno))) return false; - if (var->arrsize > 1) + if (asize > 1) { strcpy(mallocedval, "array ["); - for (element = 0; element < var->arrsize; element++) + for (element = 0; element < asize; element++) sprintf(mallocedval + strlen(mallocedval), "%lu,", ((unsigned long *) var->value)[element]); strcpy(mallocedval + strlen(mallocedval) - 1, "]"); @@ -703,14 +705,14 @@ ECPGstore_input(const int lineno, const bool force_indicator, const struct varia break; #ifdef HAVE_LONG_LONG_INT_64 case ECPGt_long_long: - if (!(mallocedval = ECPGalloc(var->arrsize * 30, lineno))) + if (!(mallocedval = ECPGalloc(asize * 30, lineno))) return false; - if (var->arrsize > 1) + if (asize > 1) { strcpy(mallocedval, "array ["); - for (element = 0; element < var->arrsize; element++) + for (element = 0; element < asize; element++) sprintf(mallocedval + strlen(mallocedval), "%lld,", ((long long *) var->value)[element]); strcpy(mallocedval + strlen(mallocedval) - 1, "]"); @@ -723,14 +725,14 @@ ECPGstore_input(const int lineno, const bool force_indicator, const struct varia break; case ECPGt_unsigned_long_long: - if (!(mallocedval = ECPGalloc(var->arrsize * 30, lineno))) + if (!(mallocedval = ECPGalloc(asize * 30, lineno))) return false; - if (var->arrsize > 1) + if (asize > 1) { strcpy(mallocedval, "array ["); - for (element = 0; element < var->arrsize; element++) + for (element = 0; element < asize; element++) sprintf(mallocedval + strlen(mallocedval), "%llu,", ((unsigned long long *) var->value)[element]); strcpy(mallocedval + strlen(mallocedval) - 1, "]"); @@ -743,14 +745,14 @@ ECPGstore_input(const int lineno, const bool force_indicator, const struct varia break; #endif /* HAVE_LONG_LONG_INT_64 */ case ECPGt_float: - if (!(mallocedval = ECPGalloc(var->arrsize * 25, lineno))) + if (!(mallocedval = ECPGalloc(asize * 25, lineno))) return false; - if (var->arrsize > 1) + if (asize > 1) { strcpy(mallocedval, "array ["); - for (element = 0; element < var->arrsize; element++) + for (element = 0; element < asize; element++) sprintf(mallocedval + strlen(mallocedval), "%.14g,", ((float *) var->value)[element]); strcpy(mallocedval + strlen(mallocedval) - 1, "]"); @@ -763,14 +765,14 @@ ECPGstore_input(const int lineno, const bool force_indicator, const struct varia break; case ECPGt_double: - if (!(mallocedval = ECPGalloc(var->arrsize * 25, lineno))) + if (!(mallocedval = ECPGalloc(asize * 25, lineno))) return false; - if (var->arrsize > 1) + if (asize > 1) { strcpy(mallocedval, "array ["); - for (element = 0; element < var->arrsize; element++) + for (element = 0; element < asize; element++) sprintf(mallocedval + strlen(mallocedval), "%.14g,", ((double *) var->value)[element]); strcpy(mallocedval + strlen(mallocedval) - 1, "]");