From 078dcd8eb2f589874595849ec9a5edd9efcb05e7 Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Thu, 20 Jun 2024 22:10:27 +0800 Subject: [PATCH] fixes #23742; setLen(0) no longer allocates memory for uninitialized strs/seqs --- lib/system/sysstr.nim | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/system/sysstr.nim b/lib/system/sysstr.nim index e4d6479ec7cfa..3621c4960a5a9 100644 --- a/lib/system/sysstr.nim +++ b/lib/system/sysstr.nim @@ -220,7 +220,10 @@ proc appendChar(dest: NimString, c: char) {.compilerproc, inline.} = proc setLengthStr(s: NimString, newLen: int): NimString {.compilerRtl.} = let n = max(newLen, 0) if s == nil: - result = mnewString(n) + if n == 0: + return s + else: + result = mnewString(n) elif n <= s.space: result = s else: @@ -301,7 +304,10 @@ proc setLengthSeqV2(s: PGenericSeq, typ: PNimType, newLen: int): PGenericSeq {. compilerRtl.} = sysAssert typ.kind == tySequence, "setLengthSeqV2: type is not a seq" if s == nil: - result = cast[PGenericSeq](newSeq(typ, newLen)) + if newLen == 0: + result = s + else: + result = cast[PGenericSeq](newSeq(typ, newLen)) else: let elemSize = typ.base.size let elemAlign = typ.base.align