From 2b482f2e873326d1bce85ec5b7f275d9a75f49f9 Mon Sep 17 00:00:00 2001
From: "Stephen R. van den Berg" <srb@cuci.nl>
Date: Wed, 3 Sep 2014 22:40:50 +0200
Subject: [PATCH] Correct calculation for string sizes.

---
 src/stralloc.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/src/stralloc.c b/src/stralloc.c
index afd364169f..8ea89e94df 100644
--- a/src/stralloc.c
+++ b/src/stralloc.c
@@ -2332,13 +2332,15 @@ PMOD_EXPORT void string_build_mkspace(struct string_builder *s,
   if(mag > s->s->size_shift)
   {
     struct pike_string *n;
-    ptrdiff_t l = s->s->len + chars + s->malloced;
+    ptrdiff_t l = s->s->len + chars;
+    if (l < s->malloced)
+      l = s->malloced;
     n=begin_wide_shared_string(l,mag);
     pike_string_cpy(MKPCHARP_STR(n),s->s);
     n->len=s->s->len;
     s->s->len = s->malloced;	/* Restore the real length */
-    free_string(s->s);
     s->malloced=l;
+    free_string(s->s);
     s->s=n;
   }
   else if(s->s->len+chars > s->malloced)
-- 
GitLab