From ba39f359aa6b28eb30f7165f9e629373c730b3b2 Mon Sep 17 00:00:00 2001 From: nitely Date: Tue, 5 Jun 2018 20:22:27 -0300 Subject: check bounds instead of index --- compiler/ccgcalls.nim | 5 ++--- compiler/ccgexprs.nim | 19 ++++++++++++------- tests/system/tsystem_misc.nim | 25 +++++++++++++++++++++++++ 3 files changed, 39 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) = diff --git a/tests/system/tsystem_misc.nim b/tests/system/tsystem_misc.nim index 85228e9e7..460d94d56 100644 --- a/tests/system/tsystem_misc.nim +++ b/tests/system/tsystem_misc.nim @@ -11,6 +11,7 @@ discard """ 2 3 4 +2 ''' """ @@ -47,3 +48,27 @@ foo(toOpenArray(arr, 8, 12)) var seqq = @[1, 2, 3, 4, 5] foo(toOpenArray(seqq, 1, 3)) + +# empty openArray issue #7904 +foo(toOpenArray(seqq, 0, -1)) +foo(toOpenArray(seqq, 1, 0)) +doAssertRaises(IndexError): + foo(toOpenArray(seqq, 0, -2)) + +foo(toOpenArray(arr, 9, 8)) +foo(toOpenArray(arr, 0, -1)) +foo(toOpenArray(arr, 1, 0)) +doAssertRaises(IndexError): + foo(toOpenArray(arr, 10, 8)) + +# test openArray of openArray +proc oaEmpty(a: openArray[int]) = + foo(toOpenArray(a, 0, -1)) + +proc oaFirstElm(a: openArray[int]) = + foo(toOpenArray(a, 0, 0)) + +oaEmpty(toOpenArray(seqq, 0, -1)) +oaEmpty(toOpenArray(seqq, 1, 0)) +oaEmpty(toOpenArray(seqq, 1, 2)) +oaFirstElm(toOpenArray(seqq, 1, seqq.len-1)) -- cgit 1.4.1-2-gfad0 From 7297195f9fc5aa47de4b64f1402cfc7af109badc Mon Sep 17 00:00:00 2001 From: nitely Date: Wed, 6 Jun 2018 02:22:33 -0300 Subject: test negative range array --- tests/system/tsystem_misc.nim | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/system/tsystem_misc.nim b/tests/system/tsystem_misc.nim index 460d94d56..6d14aa68f 100644 --- a/tests/system/tsystem_misc.nim +++ b/tests/system/tsystem_misc.nim @@ -12,6 +12,9 @@ discard """ 3 4 2 +1 +2 +3 ''' """ @@ -72,3 +75,14 @@ oaEmpty(toOpenArray(seqq, 0, -1)) oaEmpty(toOpenArray(seqq, 1, 0)) oaEmpty(toOpenArray(seqq, 1, 2)) oaFirstElm(toOpenArray(seqq, 1, seqq.len-1)) + +var arrNeg: array[-3 .. -1, int] = [1, 2, 3] +foo(toOpenArray(arrNeg, -3, -1)) +foo(toOpenArray(arrNeg, 0, -1)) +foo(toOpenArray(arrNeg, -3, -4)) +doAssertRaises(IndexError): + foo(toOpenArray(arrNeg, -4, -1)) +doAssertRaises(IndexError): + foo(toOpenArray(arrNeg, -1, 0)) +doAssertRaises(IndexError): + foo(toOpenArray(arrNeg, -1, -3)) -- cgit 1.4.1-2-gfad0