diff --git a/compiler/semfold.nim b/compiler/semfold.nim index 0bdd0b64c451..e0975f2dd5de 100644 --- a/compiler/semfold.nim +++ b/compiler/semfold.nim @@ -493,11 +493,11 @@ proc foldArrayAccess(m: PSym, n: PNode; g: ModuleGraph): PNode = result = x.sons[int(idx)] if result.kind == nkExprColonExpr: result = result.sons[1] else: - localError(g.config, n.info, formatErrorIndexBound(idx, sonsLen(x)+1) & $n) + localError(g.config, n.info, formatErrorIndexBound(idx, sonsLen(x)-1) & $n) of nkBracket: idx = idx - firstOrd(g.config, x.typ) if idx >= 0 and idx < x.len: result = x.sons[int(idx)] - else: localError(g.config, n.info, formatErrorIndexBound(idx, x.len+1) & $n) + else: localError(g.config, n.info, formatErrorIndexBound(idx, x.len-1) & $n) of nkStrLit..nkTripleStrLit: result = newNodeIT(nkCharLit, x.info, n.typ) if idx >= 0 and idx < len(x.strVal): diff --git a/compiler/vm.nim b/compiler/vm.nim index c001981f8981..282b6eb75eed 100644 --- a/compiler/vm.nim +++ b/compiler/vm.nim @@ -15,6 +15,7 @@ const traceCode = debugEchoCode import ast except getstr +import system/helpers2 import strutils, astalgo, msgs, vmdef, vmgen, nimsets, types, passes, @@ -432,7 +433,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" @@ -529,7 +529,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}: @@ -537,11 +537,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 @@ -551,7 +551,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) @@ -561,11 +561,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) @@ -596,7 +596,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]) @@ -1298,7 +1298,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 @@ -1306,7 +1306,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 @@ -1711,7 +1711,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 diff --git a/lib/core/typeinfo.nim b/lib/core/typeinfo.nim index cfb8f8f5d70e..a4573ff402a4 100644 --- a/lib/core/typeinfo.nim +++ b/lib/core/typeinfo.nim @@ -20,6 +20,8 @@ include "system/inclrtl.nim" include "system/hti.nim" +import system/helpers2 + {.pop.} type @@ -194,14 +196,14 @@ proc `[]`*(x: Any, i: int): Any = of tyArray: var bs = x.rawType.base.size if i >=% x.rawType.size div bs: - raise newException(IndexError, "index out of bounds") + raise newException(IndexError, formatErrorIndexBound(i, x.rawType.size div bs)) return newAny(x.value +!! i*bs, x.rawType.base) of tySequence: var s = cast[ppointer](x.value)[] if s == nil: raise newException(ValueError, "sequence is nil") var bs = x.rawType.base.size if i >=% cast[PGenSeq](s).len: - raise newException(IndexError, "index out of bounds") + raise newException(IndexError, formatErrorIndexBound(i, cast[PGenSeq](s).len-1)) return newAny(s +!! (GenericSeqSize+i*bs), x.rawType.base) else: assert false @@ -211,7 +213,7 @@ proc `[]=`*(x: Any, i: int, y: Any) = of tyArray: var bs = x.rawType.base.size if i >=% x.rawType.size div bs: - raise newException(IndexError, "index out of bounds") + raise newException(IndexError, formatErrorIndexBound(i, x.rawType.size div bs)) assert y.rawType == x.rawType.base genericAssign(x.value +!! i*bs, y.value, y.rawType) of tySequence: @@ -219,7 +221,7 @@ proc `[]=`*(x: Any, i: int, y: Any) = if s == nil: raise newException(ValueError, "sequence is nil") var bs = x.rawType.base.size if i >=% cast[PGenSeq](s).len: - raise newException(IndexError, "index out of bounds") + raise newException(IndexError, formatErrorIndexBound(i, cast[PGenSeq](s).len-1)) assert y.rawType == x.rawType.base genericAssign(s +!! (GenericSeqSize+i*bs), y.value, y.rawType) else: assert false diff --git a/lib/pure/os.nim b/lib/pure/os.nim index 32fa2885eace..7b84e2063531 100644 --- a/lib/pure/os.nim +++ b/lib/pure/os.nim @@ -17,7 +17,7 @@ include "system/inclrtl" import - strutils, pathnorm + strutils, pathnorm, system/helpers2 const weirdTarget = defined(nimscript) or defined(js) @@ -2018,7 +2018,7 @@ elif defined(windows): ownArgv = parseCmdLine($getCommandLine()) ownParsedArgv = true if i < ownArgv.len and i >= 0: return TaintedString(ownArgv[i]) - raise newException(IndexError, "invalid index") + raise newException(IndexError, formatErrorIndexBound(i, ownArgv.len-1)) elif defined(genode): proc paramStr*(i: int): TaintedString = @@ -2037,7 +2037,7 @@ elif not defined(createNimRtl) and proc paramStr*(i: int): TaintedString {.tags: [ReadIOEffect].} = # Docstring in nimdoc block. if i < cmdCount and i >= 0: return TaintedString($cmdLine[i]) - raise newException(IndexError, "invalid index") + raise newException(IndexError, formatErrorIndexBound(i, cmdCount-1)) proc paramCount*(): int {.tags: [ReadIOEffect].} = # Docstring in nimdoc block. diff --git a/lib/system/jssys.nim b/lib/system/jssys.nim index d7718e4f4eda..20a46f1b90c6 100644 --- a/lib/system/jssys.nim +++ b/lib/system/jssys.nim @@ -7,6 +7,8 @@ # distribution, for details about the copyright. # +import system/helpers2 + proc log*(s: cstring) {.importc: "console.log", varargs, nodecl.} type @@ -157,8 +159,8 @@ proc raiseDivByZero {.exportc: "raiseDivByZero", noreturn, compilerProc.} = proc raiseRangeError() {.compilerproc, noreturn.} = raise newException(RangeError, "value out of range") -proc raiseIndexError() {.compilerproc, noreturn.} = - raise newException(IndexError, "index out of bounds") +proc raiseIndexError(i, a, b: int) {.compilerproc, noreturn.} = + raise newException(IndexError, formatErrorIndexBound(int(i), int(a), int(b))) proc raiseFieldError(f: string) {.compilerproc, noreturn.} = raise newException(FieldError, f & " is not accessible") @@ -626,7 +628,7 @@ proc arrayConstr(len: int, value: JSRef, typ: PNimType): JSRef {. proc chckIndx(i, a, b: int): int {.compilerproc.} = if i >= a and i <= b: return i - else: raiseIndexError() + else: raiseIndexError(i, a, b) proc chckRange(i, a, b: int): int {.compilerproc.} = if i >= a and i <= b: return i diff --git a/tests/exception/testindexerroroutput.nims b/tests/exception/testindexerroroutput.nims new file mode 100644 index 000000000000..e282f14b416d --- /dev/null +++ b/tests/exception/testindexerroroutput.nims @@ -0,0 +1,23 @@ +mode = ScriptMode.Verbose + +case paramStr(3): + of "test1": + #543 + block: + let s = "abc" + discard s[len(s)] + of "test2": + #537 + block: + var s = "abc" + s[len(s)] = 'd' + of "test3": + #588 + block: + let arr = ['a', 'b', 'c'] + discard arr[len(arr)] + of "test4": + #588 + block: + var arr = ['a', 'b', 'c'] + arr[len(arr)] = 'd' diff --git a/tests/exception/tindexerrorformatbounds.nim b/tests/exception/tindexerrorformatbounds.nim new file mode 100644 index 000000000000..a5757e4e10b2 --- /dev/null +++ b/tests/exception/tindexerrorformatbounds.nim @@ -0,0 +1,52 @@ +import os +import strutils + + +const characters = "abcdefghijklmnopqrstuvwxyz" +var s: string + +# # chcks.nim:23 +# # test formatErrorIndexBound returns correct bounds +block: + s = characters + try: + discard s[0..999] + except IndexError: + let msg = getCurrentExceptionMsg() + let expected = "(i:$#) <= (n:$#)" % [$len(s), $(len(s)-1)] + doAssert msg.contains expected + +block: + try: + discard paramStr(999) + except IndexError: + let msg = getCurrentExceptionMsg() + let expected = "(i:999) <= (n:0)" + doAssert msg.contains expected + +static: + const nim = getCurrentCompilerExe() + + block: + let ret = gorgeEx(nim & " e testindexerroroutput.nims test1") + let expected = "(i:3) <= (n:2)" + doAssert ret.exitCode != 0 + doAssert ret.output.contains expected + + block: + let ret = gorgeEx(nim & " e testindexerroroutput.nims test2") + let expected = "(i:3) <= (n:2)" + doAssert ret.exitCode != 0 + doAssert ret.output.contains expected + + block: + let ret = gorgeEx(nim & " e testindexerroroutput.nims test3") + let expected = "(i:3) <= (n:2)" + doAssert ret.exitCode != 0 + doAssert ret.output.contains expected + + block: + let ret = gorgeEx(nim & " e testindexerroroutput.nims test4") + let expected = "(i:3) <= (n:2)" + doAssert ret.exitCode != 0 + doAssert ret.output.contains expected