From 0da50cef4f2a693a65713e8dbfbeb3d39918c122 Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Tue, 6 Dec 2022 05:27:18 +0800 Subject: [PATCH] fixes #20954; bounchecks for len(toOpenArray()) [backport] (#20956) * bounchecks for len(toOpenArray()) * add a testcase (cherry picked from commit b83bd282dcd6ba8e26fc838ba968089f86b614b4) --- compiler/ccgexprs.nim | 4 ++++ tests/arc/topenarray.nim | 16 ++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/compiler/ccgexprs.nim b/compiler/ccgexprs.nim index 250752525c..4d016cf7a0 100644 --- a/compiler/ccgexprs.nim +++ b/compiler/ccgexprs.nim @@ -1796,9 +1796,13 @@ proc genArrayLen(p: BProc, e: PNode, d: var TLoc, op: TMagic) = # Bug #9279, len(toOpenArray()) has to work: if a.kind in nkCallKinds and a[0].kind == nkSym and a[0].sym.magic == mSlice: # magic: pass slice to openArray: + var m: TLoc var b, c: TLoc + initLocExpr(p, a[1], m) initLocExpr(p, a[2], b) initLocExpr(p, a[3], c) + if optBoundsCheck in p.options: + genBoundsCheck(p, m, b, c) if op == mHigh: putIntoDest(p, d, e, ropecg(p.module, "($2)-($1)", [rdLoc(b), rdLoc(c)])) else: diff --git a/tests/arc/topenarray.nim b/tests/arc/topenarray.nim index 03ec7adf21..47c26a11f8 100644 --- a/tests/arc/topenarray.nim +++ b/tests/arc/topenarray.nim @@ -22,3 +22,19 @@ block: # bug 18627 for i in params.toOpenArray(0, params.len - 1): echo i uciLoop2() + +when defined(nimPreviewSlimSystem): + import std/assertions + +block: # bug #20954 + block: + doAssertRaises(IndexDefect): + var v: array[10, int] + + echo len(toOpenArray(v, 20, 30)) + + block: + doAssertRaises(IndexDefect): + var v: seq[int] + + echo len(toOpenArray(v, 20, 30))