diff --git a/src/stralloc.c b/src/stralloc.c
index afd364169f5c4a62aa7cfee6fada43fc35ae5350..8ea89e94dfef3ccd0a771fcdca7b941ba1d6f739 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)