* Make index out of bounds more useful by including the 'bounds'.
* fixes #9880 index out of bounds (remaining cases); revives #10228
* change err msg to: `index 3 not in 0 .. 1`
This commit is contained in:
Timothee Cour 2019-02-10 13:07:11 -08:00 • committed by Andreas Rumpf
commit 942495611b
11 changed files with 88 additions and 36 deletions

View file

@ -11,6 +11,7 @@
## An instruction is 1-3 int32s in memory, it is a register based VM.
import ast except getstr
import system/indexerrors
import
strutils, astalgo, msgs, vmdef, vmgen, nimsets, types, passes,
@ -473,7 +474,6 @@ proc setLenSeq(c: PCtx; node: PNode; newLen: int; info: TLineInfo) =
node.sons[i] = getNullValue(typ.sons[0], info, c.config)
const
errIndexOutOfBounds = "index out of bounds"
errNilAccess = "attempt to access a nil address"
errOverOrUnderflow = "over- or underflow"
errConstantDivisionByZero = "division by zero"
@ -577,7 +577,7 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg =
# a = b[c]
decodeBC(rkNode)
if regs[rc].intVal > high(int):
stackTrace(c, tos, pc, errIndexOutOfBounds)
stackTrace(c, tos, pc, formatErrorIndexBound(regs[rc].intVal, high(int)))
let idx = regs[rc].intVal.int
let src = regs[rb].node
if src.kind in {nkStrLit..nkTripleStrLit}:
@ -585,11 +585,11 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg =
regs[ra].node = newNodeI(nkCharLit, c.debug[pc])
regs[ra].node.intVal = src.strVal[idx].ord
else:
stackTrace(c, tos, pc, errIndexOutOfBounds)
stackTrace(c, tos, pc, formatErrorIndexBound(idx, src.strVal.len-1))
elif src.kind notin {nkEmpty..nkFloat128Lit} and idx <% src.len:
regs[ra].node = src.sons[idx]
else:
stackTrace(c, tos, pc, errIndexOutOfBounds)
stackTrace(c, tos, pc, formatErrorIndexBound(idx, src.len-1))
of opcLdStrIdx:
decodeBC(rkInt)
let idx = regs[rc].intVal.int
@ -599,7 +599,7 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg =
elif idx == s.len and optLaxStrings in c.config.options:
regs[ra].intVal = 0
else:
stackTrace(c, tos, pc, errIndexOutOfBounds)
stackTrace(c, tos, pc, formatErrorIndexBound(idx, s.len-1))
of opcWrArr:
# a[b] = c
decodeBC(rkNode)
@ -609,11 +609,11 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg =
if idx <% arr.strVal.len:
arr.strVal[idx] = chr(regs[rc].intVal)
else:
stackTrace(c, tos, pc, errIndexOutOfBounds)
stackTrace(c, tos, pc, formatErrorIndexBound(idx, arr.strVal.len-1))
elif idx <% arr.len:
writeField(arr.sons[idx], regs[rc])
else:
stackTrace(c, tos, pc, errIndexOutOfBounds)
stackTrace(c, tos, pc, formatErrorIndexBound(idx, arr.len-1))
of opcLdObj:
# a = b.c
decodeBC(rkNode)
@ -644,7 +644,7 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg =
if idx <% regs[ra].node.strVal.len:
regs[ra].node.strVal[idx] = chr(regs[rc].intVal)
else:
stackTrace(c, tos, pc, errIndexOutOfBounds)
stackTrace(c, tos, pc, formatErrorIndexBound(idx, regs[ra].node.strVal.len-1))
of opcAddrReg:
decodeB(rkRegisterAddr)
regs[ra].regAddr = addr(regs[rb])
@ -1361,7 +1361,7 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg =
if src.kind notin {nkEmpty..nkNilLit} and idx <% src.len:
regs[ra].node = src.sons[idx]
else:
stackTrace(c, tos, pc, errIndexOutOfBounds)
stackTrace(c, tos, pc, formatErrorIndexBound(idx, src.len-1))
of opcNSetChild:
decodeBC(rkNode)
let idx = regs[rb].intVal.int
@ -1369,7 +1369,7 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg =
if dest.kind notin {nkEmpty..nkNilLit} and idx <% dest.len:
dest.sons[idx] = regs[rc].node
else:
stackTrace(c, tos, pc, errIndexOutOfBounds)
stackTrace(c, tos, pc, formatErrorIndexBound(idx, dest.len-1))
of opcNAdd:
decodeBC(rkNode)
var u = regs[rb].node
@ -1774,7 +1774,7 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg =
if contains(g.cacheSeqs, destKey) and idx <% g.cacheSeqs[destKey].len:
regs[ra].node = g.cacheSeqs[destKey][idx.int]
else:
stackTrace(c, tos, pc, errIndexOutOfBounds)
stackTrace(c, tos, pc, formatErrorIndexBound(idx, g.cacheSeqs[destKey].len-1))
of opcNctPut:
let g = c.graph
let destKey = regs[ra].node.strVal