setLen doesn't crash on nil strings/seqs anymore

This commit is contained in:
Andreas Rumpf 2018-04-27 11:07:40 +02:00
commit ce36b8efd6
2 changed files with 12 additions and 5 deletions

View file

@ -1500,13 +1500,13 @@ proc genSetLengthSeq(p: BProc, e: PNode, d: var TLoc) =
initLocExpr(p, e.sons[2], b) initLocExpr(p, e.sons[2], b)
let t = skipTypes(e.sons[1].typ, {tyVar}) let t = skipTypes(e.sons[1].typ, {tyVar})
let setLenPattern = if not p.module.compileToCpp: let setLenPattern = if not p.module.compileToCpp:
"$1 = ($3) #setLengthSeq(&($1)->Sup, sizeof($4), $2);$n" "$1 = ($3) #setLengthSeqV2(&($1)->Sup, $4, $2);$n"
else: else:
"$1 = ($3) #setLengthSeq($1, sizeof($4), $2);$n" "$1 = ($3) #setLengthSeqV2($1, $4, $2);$n"
lineCg(p, cpsStmts, setLenPattern, [ lineCg(p, cpsStmts, setLenPattern, [
rdLoc(a), rdLoc(b), getTypeDesc(p.module, t), rdLoc(a), rdLoc(b), getTypeDesc(p.module, t),
getTypeDesc(p.module, t.skipTypes(abstractInst).sons[0])]) genTypeInfo(p.module, t.skipTypes(abstractInst), e.info)])
gcUsage(e) gcUsage(e)
proc genSetLengthStr(p: BProc, e: PNode, d: var TLoc) = proc genSetLengthStr(p: BProc, e: PNode, d: var TLoc) =

View file

@ -226,8 +226,8 @@ proc appendChar(dest: NimString, c: char) {.compilerproc, inline.} =
proc setLengthStr(s: NimString, newLen: int): NimString {.compilerRtl.} = proc setLengthStr(s: NimString, newLen: int): NimString {.compilerRtl.} =
var n = max(newLen, 0) var n = max(newLen, 0)
if wasMoved(s): if s == nil:
result = newOwnedString(s, n) result = mnewString(newLen)
elif n <= s.space: elif n <= s.space:
result = s result = s
else: else:
@ -313,6 +313,13 @@ proc setLengthSeq(seq: PGenericSeq, elemSize, newLen: int): PGenericSeq {.
(newLen*%elemSize)), (result.len-%newLen) *% elemSize) (newLen*%elemSize)), (result.len-%newLen) *% elemSize)
result.len = newLen result.len = newLen
proc setLengthSeqV2(s: PGenericSeq, typ: PNimType, newLen: int): PGenericSeq {.
compilerRtl.} =
if s == nil:
result = cast[PGenericSeq](newSeq(typ, newLen))
else:
result = setLengthSeq(s, typ.base.size, newLen)
# --------------- other string routines ---------------------------------- # --------------- other string routines ----------------------------------
proc add*(result: var string; x: int64) = proc add*(result: var string; x: int64) =
let base = result.len let base = result.len