diff options
author | Ayke van Laethem <[email protected]> | 2024-02-27 14:11:19 +0100 |
---|---|---|
committer | Ron Evans <[email protected]> | 2024-02-27 15:16:38 +0100 |
commit | 9951eb9990071aaf5e29665cc3185c1abe7368dc (patch) | |
tree | 131641e5ea07026bc2628e54a16829faa700ce06 /interp/interpreter.go | |
parent | c8f77d26a872cf9686b06a7a7ce3b7c4f2292363 (diff) | |
download | tinygo-9951eb9990071aaf5e29665cc3185c1abe7368dc.tar.gz tinygo-9951eb9990071aaf5e29665cc3185c1abe7368dc.zip |
interp: return a proper error message when indexing out of range
This helps debug issues inside interp.
Diffstat (limited to 'interp/interpreter.go')
-rw-r--r-- | interp/interpreter.go | 22 |
1 files changed, 18 insertions, 4 deletions
diff --git a/interp/interpreter.go b/interp/interpreter.go index 24735eb26..864d6f652 100644 --- a/interp/interpreter.go +++ b/interp/interpreter.go @@ -427,7 +427,11 @@ func (r *runner) run(fn *function, params []value, parentMem *memoryView, indent if err != nil { return nil, mem, r.errorAt(inst, err) } - methodSetPtr, err := mem.load(typecodePtr.addOffset(-int64(r.pointerSize)), r.pointerSize).asPointer(r) + typecodePtrOffset, err := typecodePtr.addOffset(-int64(r.pointerSize)) + if err != nil { + return nil, mem, r.errorAt(inst, err) // unlikely + } + methodSetPtr, err := mem.load(typecodePtrOffset, r.pointerSize).asPointer(r) if err != nil { return nil, mem, r.errorAt(inst, err) } @@ -473,7 +477,11 @@ func (r *runner) run(fn *function, params []value, parentMem *memoryView, indent if err != nil { return nil, mem, r.errorAt(inst, err) } - methodSetPtr, err := mem.load(typecodePtr.addOffset(-int64(r.pointerSize)), r.pointerSize).asPointer(r) + typecodePtrOffset, err := typecodePtr.addOffset(-int64(r.pointerSize)) + if err != nil { + return nil, mem, r.errorAt(inst, err) + } + methodSetPtr, err := mem.load(typecodePtrOffset, r.pointerSize).asPointer(r) if err != nil { return nil, mem, r.errorAt(inst, err) } @@ -658,7 +666,10 @@ func (r *runner) run(fn *function, params []value, parentMem *memoryView, indent locals[inst.localIndex] = makeLiteralInt(ptrValue, int(operands[0].len(r)*8)) continue } - ptr = ptr.addOffset(int64(offset)) + ptr, err = ptr.addOffset(int64(offset)) + if err != nil { + return nil, mem, r.errorAt(inst, err) + } locals[inst.localIndex] = ptr if r.debug { fmt.Fprintln(os.Stderr, indent+"gep:", operands, "->", ptr) @@ -756,7 +767,10 @@ func (r *runner) run(fn *function, params []value, parentMem *memoryView, indent if inst.opcode == llvm.Add { // This likely means this is part of a // unsafe.Pointer(uintptr(ptr) + offset) pattern. - lhsPtr = lhsPtr.addOffset(int64(rhs.Uint())) + lhsPtr, err = lhsPtr.addOffset(int64(rhs.Uint())) + if err != nil { + return nil, mem, r.errorAt(inst, err) + } locals[inst.localIndex] = lhsPtr } else if inst.opcode == llvm.Xor && rhs.Uint() == 0 { // Special workaround for strings.noescape, see |