diff options
author | Andreas Rumpf <rumpf_a@web.de> | 2020-12-17 23:21:55 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-17 23:21:55 +0100 |
commit | 868c31e88a68ce16c5c0d9283a41fb3cfcec4c41 (patch) | |
tree | b7491387b52a396e8f99976c55058b7e8604e0de /lib | |
parent | 5d8f862bc644e1db076e7e8cc8a2fe8c99009329 (diff) | |
download | Nim-868c31e88a68ce16c5c0d9283a41fb3cfcec4c41.tar.gz |
fixes #16365 [backport] (#16381)
Diffstat (limited to 'lib')
-rw-r--r-- | lib/system/strs_v2.nim | 13 |
1 files changed, 7 insertions, 6 deletions
diff --git a/lib/system/strs_v2.nim b/lib/system/strs_v2.nim index f51791da2..fe117997b 100644 --- a/lib/system/strs_v2.nim +++ b/lib/system/strs_v2.nim @@ -40,21 +40,22 @@ 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.} = + let newLen = s.len + addlen if isLiteral(s): let oldP = s.p # can't mutate a literal, so we need a fresh copy here: when compileOption("threads"): - s.p = cast[ptr NimStrPayload](allocShared0(contentSize(s.len + addlen))) + s.p = cast[ptr NimStrPayload](allocShared0(contentSize(newLen))) else: - s.p = cast[ptr NimStrPayload](alloc0(contentSize(s.len + addlen))) - s.p.cap = s.len + addlen + s.p = cast[ptr NimStrPayload](alloc0(contentSize(newLen))) + s.p.cap = newLen 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) + copyMem(unsafeAddr s.p.data[0], unsafeAddr oldP.data[0], min(s.len, newLen)) else: let oldCap = s.p.cap and not strlitFlag - if s.len + addlen > oldCap: - let newCap = max(s.len + addlen, resize(oldCap)) + if newLen > oldCap: + let newCap = max(newLen, resize(oldCap)) when compileOption("threads"): s.p = cast[ptr NimStrPayload](reallocShared0(s.p, contentSize(oldCap), contentSize(newCap))) else: |