diff options
author | nitely <ecastroborsani@gmail.com> | 2018-06-05 20:22:27 -0300 |
---|---|---|
committer | nitely <ecastroborsani@gmail.com> | 2018-06-05 20:22:27 -0300 |
commit | ba39f359aa6b28eb30f7165f9e629373c730b3b2 (patch) | |
tree | 4964e2406691a57c7e852c6af4008e6d46a35a03 /compiler | |
parent | 959b6354c126159e5a59c3a021e472380d04e088 (diff) | |
download | Nim-ba39f359aa6b28eb30f7165f9e629373c730b3b2.tar.gz |
check bounds instead of index
Diffstat (limited to 'compiler')
-rw-r--r-- | compiler/ccgcalls.nim | 5 | ||||
-rw-r--r-- | compiler/ccgexprs.nim | 19 |
2 files changed, 14 insertions, 10 deletions
diff --git a/compiler/ccgcalls.nim b/compiler/ccgcalls.nim index 7d355db5f..22733f6ac 100644 --- a/compiler/ccgcalls.nim +++ b/compiler/ccgcalls.nim @@ -83,7 +83,7 @@ proc isInCurrentFrame(p: BProc, n: PNode): bool = result = isInCurrentFrame(p, n.sons[0]) else: discard -proc genIndexCheck(p: BProc; arr, idx: TLoc) +proc genBoundsCheck(p: BProc; arr, a, b: TLoc) proc openArrayLoc(p: BProc, n: PNode): Rope = var a: TLoc @@ -97,8 +97,7 @@ proc openArrayLoc(p: BProc, n: PNode): Rope = initLocExpr(p, q[3], c) # but first produce the required index checks: if optBoundsCheck in p.options: - genIndexCheck(p, a, b) - genIndexCheck(p, a, c) + genBoundsCheck(p, a, b, c) let ty = skipTypes(a.t, abstractVar+{tyPtr}) case ty.kind of tyArray: diff --git a/compiler/ccgexprs.nim b/compiler/ccgexprs.nim index 335aa2f84..674821666 100644 --- a/compiler/ccgexprs.nim +++ b/compiler/ccgexprs.nim @@ -873,21 +873,26 @@ proc genCStringElem(p: BProc, n, x, y: PNode, d: var TLoc) = putIntoDest(p, d, n, ropecg(p.module, "$1[$2]", rdLoc(a), rdCharLoc(b)), a.storage) -proc genIndexCheck(p: BProc; arr, idx: TLoc) = +proc genBoundsCheck(p: BProc; arr, a, b: TLoc) = let ty = skipTypes(arr.t, abstractVarRange) case ty.kind of tyOpenArray, tyVarargs: - linefmt(p, cpsStmts, "if ((NU)($1) >= (NU)($2Len_0)) #raiseIndexError();$n", - rdLoc(idx), rdLoc(arr)) + linefmt(p, cpsStmts, + "if ($2-$1 != -1 && " & + "((NU)($1) >= (NU)($3Len_0) || (NU)($2) >= (NU)($3Len_0))) #raiseIndexError();$n", + rdLoc(a), rdLoc(b), rdLoc(arr)) of tyArray: let first = intLiteral(firstOrd(ty)) if tfUncheckedArray notin ty.flags: - linefmt(p, cpsStmts, "if ($1 < $2 || $1 > $3) #raiseIndexError();$n", - rdCharLoc(idx), first, intLiteral(lastOrd(ty))) + linefmt(p, cpsStmts, + "if ($2-$1 != -1 && " & + "($2-$1 < -1 || $1 < $3 || $1 > $4 || $2 < $3 || $2 > $4)) #raiseIndexError();$n", + rdCharLoc(a), rdCharLoc(b), first, intLiteral(lastOrd(ty))) of tySequence, tyString: linefmt(p, cpsStmts, - "if (!$2 || (NU)($1) >= (NU)($2->$3)) #raiseIndexError();$n", - rdLoc(idx), rdLoc(arr), lenField(p)) + "if ($2-$1 != -1 && " & + "(!$3 || (NU)($1) >= (NU)($3->$4) || (NU)($2) >= (NU)($3->$4))) #raiseIndexError();$n", + rdLoc(a), rdLoc(b), rdLoc(arr), lenField(p)) else: discard proc genOpenArrayElem(p: BProc, n, x, y: PNode, d: var TLoc) = |