'add' on a nil sequence doesn't produce a crash anymore

This commit is contained in:
Andreas Rumpf
2018-04-27 09:57:14 +02:00
parent c71c951a64
commit ef1b547f35
2 changed files with 16 additions and 4 deletions

View File

@@ -1086,17 +1086,17 @@ proc genSeqElemAppend(p: BProc, e: PNode, d: var TLoc) =
# seq = (typeof seq) incrSeq(&seq->Sup, sizeof(x));
# seq->data[seq->len-1] = x;
let seqAppendPattern = if not p.module.compileToCpp:
"$1 = ($2) #incrSeqV2(&($1)->Sup, sizeof($3));$n"
"$1 = ($2) #incrSeqV3(&($1)->Sup, $3);$n"
else:
"$1 = ($2) #incrSeqV2($1, sizeof($3));$n"
"$1 = ($2) #incrSeqV3($1, $3);$n"
var a, b, dest, tmpL: TLoc
initLocExpr(p, e.sons[1], a)
initLocExpr(p, e.sons[2], b)
let bt = skipTypes(e.sons[2].typ, {tyVar})
let seqType = skipTypes(e.sons[1].typ, {tyVar})
lineCg(p, cpsStmts, seqAppendPattern, [
rdLoc(a),
getTypeDesc(p.module, e.sons[1].typ),
getTypeDesc(p.module, bt)])
genTypeInfo(p.module, seqType, e.info)])
#if bt != b.t:
# echo "YES ", e.info, " new: ", typeToString(bt), " old: ", typeToString(b.t)
initLoc(dest, locExpr, e.sons[2], OnHeap)

View File

@@ -261,6 +261,18 @@ proc incrSeqV2(seq: PGenericSeq, elemSize: int): PGenericSeq {.compilerProc.} =
GenericSeqSize))
result.reserved = r
proc incrSeqV3(s: PGenericSeq, typ: PNimType): PGenericSeq {.compilerProc.} =
if s == nil:
result = cast[PGenericSeq](newSeq(typ, 1))
result.len = 0
else:
result = s
if result.len >= result.space:
let r = resize(result.space)
result = cast[PGenericSeq](growObj(result, typ.base.size * r +
GenericSeqSize))
result.reserved = r
proc setLengthSeq(seq: PGenericSeq, elemSize, newLen: int): PGenericSeq {.
compilerRtl, inl.} =
result = seq