From fc19373993f69bcc26a3910374c4b8689966212d Mon Sep 17 00:00:00 2001
From: Michael Meskes <meskes@postgresql.org>
Date: Thu, 3 Sep 2009 10:24:48 +0000
Subject: [PATCH] Fixed incorrect memory management.

---
 src/interfaces/ecpg/ecpglib/execute.c | 38 +++++++++++++--------------
 1 file changed, 19 insertions(+), 19 deletions(-)

diff --git a/src/interfaces/ecpg/ecpglib/execute.c b/src/interfaces/ecpg/ecpglib/execute.c
index 7df16069476..bf5b1de5039 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.86 2009/08/07 10:51:20 meskes Exp $ */
+/* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/execute.c,v 1.87 2009/09/03 10:24:48 meskes Exp $ */
 
 /*
  * The aim is to get a simpler inteface to the database routines.
@@ -469,8 +469,8 @@ ecpg_store_input(const int lineno, const bool force_indicator, const struct vari
 	char	   *newcopy = NULL;
 
 	/*
-	 * arrays are not possible unless the attribute is an array too FIXME: we
-	 * do not know if the attribute is an array here
+	 * arrays are not possible unless the attribute is an array too
+	 * FIXME: we do not know if the attribute is an array here
 	 */
 #if 0
 	if (var->arrsize > 1 &&...)
@@ -818,6 +818,9 @@ ecpg_store_input(const int lineno, const bool force_indicator, const struct vari
 
 					if (var->arrsize > 1)
 					{
+						if (!(mallocedval = ecpg_strdup("array [", lineno)))
+							return false;
+
 						for (element = 0; element < var->arrsize; element++)
 						{
 							nval = PGTYPESnumeric_new();
@@ -833,15 +836,12 @@ ecpg_store_input(const int lineno, const bool force_indicator, const struct vari
 							slen = strlen(str);
 							PGTYPESnumeric_free(nval);
 
-							if (!(mallocedval = ecpg_realloc(mallocedval, strlen(mallocedval) + slen + sizeof("array [] "), lineno)))
+							if (!(mallocedval = ecpg_realloc(mallocedval, strlen(mallocedval) + slen + 2, lineno)))
 							{
 								ecpg_free(str);
 								return false;
 							}
 
-							if (!element)
-								strcpy(mallocedval, "array [");
-
 							strncpy(mallocedval + strlen(mallocedval), str, slen + 1);
 							strcpy(mallocedval + strlen(mallocedval), ",");
 							ecpg_free(str);
@@ -885,6 +885,9 @@ ecpg_store_input(const int lineno, const bool force_indicator, const struct vari
 
 					if (var->arrsize > 1)
 					{
+						if (!(mallocedval = ecpg_strdup("array [", lineno)))
+							return false;
+
 						for (element = 0; element < var->arrsize; element++)
 						{
 							str = quote_postgres(PGTYPESinterval_to_asc((interval *) ((var + var->offset * element)->value)), quote, lineno);
@@ -892,15 +895,12 @@ ecpg_store_input(const int lineno, const bool force_indicator, const struct vari
 								return false;
 							slen = strlen(str);
 
-							if (!(mallocedval = ecpg_realloc(mallocedval, strlen(mallocedval) + slen + sizeof("array [],interval "), lineno)))
+							if (!(mallocedval = ecpg_realloc(mallocedval, strlen(mallocedval) + slen + 2, lineno)))
 							{
 								ecpg_free(str);
 								return false;
 							}
 
-							if (!element)
-								strcpy(mallocedval, "array [");
-
 							strncpy(mallocedval + strlen(mallocedval), str, slen + 1);
 							strcpy(mallocedval + strlen(mallocedval), ",");
 							ecpg_free(str);
@@ -936,6 +936,9 @@ ecpg_store_input(const int lineno, const bool force_indicator, const struct vari
 
 					if (var->arrsize > 1)
 					{
+						if (!(mallocedval = ecpg_strdup("array [", lineno)))
+							return false;
+
 						for (element = 0; element < var->arrsize; element++)
 						{
 							str = quote_postgres(PGTYPESdate_to_asc(*(date *) ((var + var->offset * element)->value)), quote, lineno);
@@ -943,15 +946,12 @@ ecpg_store_input(const int lineno, const bool force_indicator, const struct vari
 								return false;
 							slen = strlen(str);
 
-							if (!(mallocedval = ecpg_realloc(mallocedval, strlen(mallocedval) + slen + sizeof("array [],date "), lineno)))
+							if (!(mallocedval = ecpg_realloc(mallocedval, strlen(mallocedval) + slen + 2, lineno)))
 							{
 								ecpg_free(str);
 								return false;
 							}
 
-							if (!element)
-								strcpy(mallocedval, "array [");
-
 							strncpy(mallocedval + strlen(mallocedval), str, slen + 1);
 							strcpy(mallocedval + strlen(mallocedval), ",");
 							ecpg_free(str);
@@ -987,6 +987,9 @@ ecpg_store_input(const int lineno, const bool force_indicator, const struct vari
 
 					if (var->arrsize > 1)
 					{
+						if (!(mallocedval = ecpg_strdup("array [", lineno)))
+							return false;
+
 						for (element = 0; element < var->arrsize; element++)
 						{
 							str = quote_postgres(PGTYPEStimestamp_to_asc(*(timestamp *) ((var + var->offset * element)->value)), quote, lineno);
@@ -995,15 +998,12 @@ ecpg_store_input(const int lineno, const bool force_indicator, const struct vari
 
 							slen = strlen(str);
 
-							if (!(mallocedval = ecpg_realloc(mallocedval, strlen(mallocedval) + slen + sizeof("array [], timestamp "), lineno)))
+							if (!(mallocedval = ecpg_realloc(mallocedval, strlen(mallocedval) + slen + 2, lineno)))
 							{
 								ecpg_free(str);
 								return false;
 							}
 
-							if (!element)
-								strcpy(mallocedval, "array [");
-
 							strncpy(mallocedval + strlen(mallocedval), str, slen + 1);
 							strcpy(mallocedval + strlen(mallocedval), ",");
 							ecpg_free(str);
-- 
GitLab