hotfix: fixes seq.add and str.add for edge cases

This commit is contained in:
Andreas Rumpf 2017-09-16 10:56:28 +02:00
commit e7bcadaf32

View file

@ -140,9 +140,10 @@ proc addChar(s: NimString, c: char): NimString =
# is compilerproc! # is compilerproc!
result = s result = s
if result.len >= result.space: if result.len >= result.space:
result.reserved = resize(result.space) let r = resize(result.space)
result = cast[NimString](growObj(result, result = cast[NimString](growObj(result,
sizeof(TGenericSeq) + result.reserved + 1)) sizeof(TGenericSeq) + r + 1))
result.reserved = r
result.data[result.len] = c result.data[result.len] = c
result.data[result.len+1] = '\0' result.data[result.len+1] = '\0'
inc(result.len) inc(result.len)
@ -218,26 +219,29 @@ proc incrSeq(seq: PGenericSeq, elemSize: int): PGenericSeq {.compilerProc.} =
# seq[seq->len-1] = x; # seq[seq->len-1] = x;
result = seq result = seq
if result.len >= result.space: if result.len >= result.space:
result.reserved = resize(result.space) let r = resize(result.space)
result = cast[PGenericSeq](growObj(result, elemSize * result.reserved + result = cast[PGenericSeq](growObj(result, elemSize * r +
GenericSeqSize)) GenericSeqSize))
result.reserved = r
inc(result.len) inc(result.len)
proc incrSeqV2(seq: PGenericSeq, elemSize: int): PGenericSeq {.compilerProc.} = proc incrSeqV2(seq: PGenericSeq, elemSize: int): PGenericSeq {.compilerProc.} =
# incrSeq version 2 # incrSeq version 2
result = seq result = seq
if result.len >= result.space: if result.len >= result.space:
result.reserved = resize(result.space) let r = resize(result.space)
result = cast[PGenericSeq](growObj(result, elemSize * result.reserved + result = cast[PGenericSeq](growObj(result, elemSize * r +
GenericSeqSize)) GenericSeqSize))
result.reserved = r
proc setLengthSeq(seq: PGenericSeq, elemSize, newLen: int): PGenericSeq {. proc setLengthSeq(seq: PGenericSeq, elemSize, newLen: int): PGenericSeq {.
compilerRtl.} = compilerRtl.} =
result = seq result = seq
if result.space < newLen: if result.space < newLen:
result.reserved = max(resize(result.space), newLen) let r = max(resize(result.space), newLen)
result = cast[PGenericSeq](growObj(result, elemSize * result.reserved + result = cast[PGenericSeq](growObj(result, elemSize * r +
GenericSeqSize)) GenericSeqSize))
result.reserved = r
elif newLen < result.len: elif newLen < result.len:
# we need to decref here, otherwise the GC leaks! # we need to decref here, otherwise the GC leaks!
when not defined(boehmGC) and not defined(nogc) and when not defined(boehmGC) and not defined(nogc) and