From c0e9dc1229e2c783fa9191d6d6ac8698d115f1ea Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Sun, 5 May 2002 17:38:26 +0000
Subject: [PATCH] plpgsql_dstring_append was broken for long strings.

---
 src/pl/plpgsql/src/pl_funcs.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/src/pl/plpgsql/src/pl_funcs.c b/src/pl/plpgsql/src/pl_funcs.c
index 6a503505c4c..f64604bd70f 100644
--- a/src/pl/plpgsql/src/pl_funcs.c
+++ b/src/pl/plpgsql/src/pl_funcs.c
@@ -3,7 +3,7 @@
  *			  procedural language
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/pl/plpgsql/src/pl_funcs.c,v 1.17 2001/11/15 23:31:09 tgl Exp $
+ *	  $Header: /cvsroot/pgsql/src/pl/plpgsql/src/pl_funcs.c,v 1.18 2002/05/05 17:38:26 tgl Exp $
  *
  *	  This software is copyrighted by Jan Wieck - Hamburg.
  *
@@ -64,6 +64,7 @@ plpgsql_dstring_init(PLpgSQL_dstring * ds)
 {
 	ds->value = palloc(ds->alloc = 512);
 	ds->used = 0;
+	ds->value[0] = '\0';
 }
 
 
@@ -86,10 +87,14 @@ void
 plpgsql_dstring_append(PLpgSQL_dstring * ds, char *str)
 {
 	int			len = strlen(str);
+	int			needed = ds->used + len + 1;
 
-	if (ds->used + len + 1 > ds->alloc)
+	if (needed > ds->alloc)
 	{
-		ds->alloc *= 2;
+		/* might have to double more than once, if len is large */
+		do {
+			ds->alloc *= 2;
+		} while (needed > ds->alloc);
 		ds->value = repalloc(ds->value, ds->alloc);
 	}
 
-- 
GitLab