summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2019-12-31 09:42:18 +0100
committerGitHub <noreply@github.com>2019-12-31 09:42:18 +0100
commitce40ed18bb648a1ed1b78e37a07cc749ec3ab27e (patch)
tree665868848b98b177d734a9c1782ad444118b0fa6
parent9eeff690d504496640a6d891fc2a029a78c47774 (diff)
downloadNim-ce40ed18bb648a1ed1b78e37a07cc749ec3ab27e.tar.gz
fixes #12965 (#12991)
-rw-r--r--lib/system/strs_v2.nim21
-rw-r--r--tests/destructor/tnewruntime_strutils.nim4
2 files changed, 15 insertions, 10 deletions
diff --git a/lib/system/strs_v2.nim b/lib/system/strs_v2.nim
index 3b7a46ff1..555028f31 100644
--- a/lib/system/strs_v2.nim
+++ b/lib/system/strs_v2.nim
@@ -37,16 +37,17 @@ proc resize(old: int): int {.inline.} =
   else: result = old * 3 div 2 # for large arrays * 3/2 is better
 
 proc prepareAdd(s: var NimStringV2; addlen: int) {.compilerRtl.} =
-  if isLiteral(s) and addlen > 0:
-    let oldP = s.p
-    # can't mutate a literal, so we need a fresh copy here:
-    let allocator = getLocalAllocator()
-    s.p = cast[ptr NimStrPayload](allocator.alloc(allocator, contentSize(s.len + addlen)))
-    s.p.allocator = allocator
-    s.p.cap = s.len + addlen
-    if s.len > 0:
-      # we are about to append, so there is no need to copy the \0 terminator:
-      copyMem(unsafeAddr s.p.data[0], unsafeAddr oldP.data[0], s.len)
+  if isLiteral(s):
+    if addlen > 0:
+      let oldP = s.p
+      # can't mutate a literal, so we need a fresh copy here:
+      let allocator = getLocalAllocator()
+      s.p = cast[ptr NimStrPayload](allocator.alloc(allocator, contentSize(s.len + addlen)))
+      s.p.allocator = allocator
+      s.p.cap = s.len + addlen
+      if s.len > 0:
+        # we are about to append, so there is no need to copy the \0 terminator:
+        copyMem(unsafeAddr s.p.data[0], unsafeAddr oldP.data[0], s.len)
   elif s.len + addlen > s.p.cap:
     let cap = max(s.len + addlen, resize(s.p.cap))
     s.p = cast[ptr NimStrPayload](s.p.allocator.realloc(s.p.allocator, s.p,
diff --git a/tests/destructor/tnewruntime_strutils.nim b/tests/destructor/tnewruntime_strutils.nim
index 74cd985ab..76f2d1a76 100644
--- a/tests/destructor/tnewruntime_strutils.nim
+++ b/tests/destructor/tnewruntime_strutils.nim
@@ -210,5 +210,9 @@ proc staticTests =
 nonStaticTests()
 staticTests()
 
+# bug #12965
+let xaa = @[""].join()
+let xbb = @["", ""].join()
+
 let (a, d) = allocCounters()
 discard cprintf("%ld %ld\n", a, d)