fix #16555, fixes #16405: len, high honors '\0' for cstring in vm (#16610)

This commit is contained in:
Timothee Cour 2021-01-11 01:16:20 -08:00 • committed by GitHub
commit f6c2450cdb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 74 additions and 15 deletions

View file

@ -872,6 +872,10 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg =
decodeBImm(rkInt)
assert regs[rb].kind == rkNode
regs[ra].intVal = regs[rb].node.strVal.len - imm
of opcLenCstring:
decodeBImm(rkInt)
assert regs[rb].kind == rkNode
regs[ra].intVal = regs[rb].node.strVal.cstring.len - imm
of opcIncl:
decodeB(rkNode)
let b = regs[rb].regToNode

View file

@ -86,6 +86,7 @@ type
opcSubImmInt,
opcLenSeq,
opcLenStr,
opcLenCstring,
opcIncl, opcInclRange, opcExcl, opcCard, opcMulInt, opcDivInt, opcModInt,
opcAddFloat, opcSubFloat, opcMulFloat, opcDivFloat,

View file

@ -1034,7 +1034,10 @@ proc genMagic(c: PCtx; n: PNode; dest: var TDest; m: TMagic) =
of mLengthOpenArray, mLengthArray, mLengthSeq:
genUnaryABI(c, n, dest, opcLenSeq)
of mLengthStr:
genUnaryABI(c, n, dest, opcLenStr)
case n[1].typ.kind
of tyString: genUnaryABI(c, n, dest, opcLenStr)
of tyCString: genUnaryABI(c, n, dest, opcLenCstring)
else: doAssert false, $n[1].typ.kind
of mIncl, mExcl:
unused(c, n, dest)
var d = c.genx(n[1])
@ -1178,10 +1181,9 @@ proc genMagic(c: PCtx; n: PNode; dest: var TDest; m: TMagic) =
if dest < 0: dest = c.getTemp(n.typ)
let tmp = c.genx(n[1])
case n[1].typ.skipTypes(abstractVar-{tyTypeDesc}).kind:
of tyString, tyCString:
c.gABI(n, opcLenStr, dest, tmp, 1)
else:
c.gABI(n, opcLenSeq, dest, tmp, 1)
of tyString: c.gABI(n, opcLenStr, dest, tmp, 1)
of tyCString: c.gABI(n, opcLenCstring, dest, tmp, 1)
else: c.gABI(n, opcLenSeq, dest, tmp, 1)
c.freeTemp(tmp)
of mEcho:
unused(c, n, dest)