Skip to content

interp: defer out-of-bounds loads to runtime instead of crashing#5458

Open
iamrajiv wants to merge 1 commit into
tinygo-org:devfrom
iamrajiv:fix-interp-oob-load-4214
Open

interp: defer out-of-bounds loads to runtime instead of crashing#5458
iamrajiv wants to merge 1 commit into
tinygo-org:devfrom
iamrajiv:fix-interp-oob-load-4214

Conversation

@iamrajiv

Copy link
Copy Markdown

Fixes #4214

When the interpreter encountered a load that was out of bounds of the object, it panicked with "interp: load out of bounds", crashing the compiler. This can happen for valid Go programs, for example when dereferencing the pointer returned by unsafe.SliceData on a zero-capacity slice, which points to a zero-sized object:

package main

import "unsafe"

var p = unsafe.SliceData([]int{})
var v = *p

func main() {}

Return nil for an out-of-bounds load, the same as for an external global, so the caller defers the load to runtime instead of crashing. This matches what regular Go does, where the load reads from the runtime zero-base at runtime.

When the interpreter encountered a load that was out of bounds of the
object, it panicked with "interp: load out of bounds", crashing the
compiler. This can happen for valid Go programs, for example when
dereferencing the pointer returned by unsafe.SliceData on a
zero-capacity slice, which points to a zero-sized object:

	package main

	import "unsafe"

	var p = unsafe.SliceData([]int{})
	var v = *p

	func main() {}

Return nil for an out-of-bounds load, the same as for an external
global, so the caller defers the load to runtime instead of crashing.
This matches what regular Go does, where the load reads from the
runtime zero-base at runtime.

Fixes tinygo-org#4214
@dgryski

dgryski commented Jun 11, 2026

Copy link
Copy Markdown
Member

/cc @aykevl for interp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

panic: interp: load out of bounds on unsafe.SliceData of zero cap slice

2 participants