Skip to content

Commit

Permalink
fixes #12965
Browse files Browse the repository at this point in the history
  • Loading branch information
Araq committed Dec 31, 2019
1 parent 1917ebf commit 088532b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
21 changes: 11 additions & 10 deletions lib/system/strs_v2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions tests/destructor/tnewruntime_strutils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 088532b

Please sign in to comment.