8000 Handle sparse array holes and nil FunctionCall arguments as undefined by amstedy · Pull Request #903 · dop251/goja · GitHub
[go: up one dir, main page]

Skip to content

Handle sparse array holes and nil FunctionCall arguments as undefined - #903

Open
amstedy wants to merge 1 commit into
dop251:masterfrom
amstedy:fix/array-sparse-nil-value-panic
Open

amstedy wants to merge 1 commit into
dop251:masterfrom
amstedy:fix/array-sparse-nil-value-panic

Conversation

@amstedy
@amstedy amstedy commented Dec 1, 2026
Copy link
Copy Markdown

Related to #848 (follow-up to a537e0c).

Cause

Commit a537e0c documented in object.go that FunctionCall.Arguments may contain nil values (treated as undefined) and updated FunctionCall.Argument and builtin_String, but several remaining paths on master still panic with nil pointer dereference or violate dense arrayObject invariants:

  1. Raw nil leaked from sparse arrays, array-likes, and detached TypedArrays:
    • Array.prototype.{find, findIndex, findLast, findLastIndex} and Array.from (with mapFn) pass raw nil from getIdx directly into FunctionCall.Arguments[0] (and find/findLast return raw nil when the predicate matches a hole).
    • Array.prototype.{pop, shift, at} (with a Proxy has trap for at) can return raw nil instead of undefined, causing panics when invoked from another native builtin (e.g. [1].findIndex(Array.prototype.find.bind([,], () => true))) or returning nil via Go AssertFunction.
    • TypedArray.from passes raw nil from getIdx into mapFn, and TypedArray.prototype.{find, findLast, forEach} leave val as nil when the underlying buffer is detached mid-iteration.
  2. Builtins reading call.Arguments[...] directly without nilSafe:
    • Boolean, Number, eval, wrapReflectFunc, String.fromCharCode, String.fromCodePoint, String.prototype.concat, Object.assign, Iterator.prototype.reduce, Iterator.concat, and TypedArray.of panic when called with a nil argument.
    • Array constructor, Array.of, Array.prototype.{push, unshift, splice, toSpliced} can store raw nil inside dense arrayObject.values while keeping objCount == len(values) (e.g. Array.from({ length: 1 }, Array.of)[0].hasOwnProperty(0) returned false).

Fix

  • Normalize getIdx reads and mapFn results with nilSafe in builtin_array.go and builtin_typedarrays.go, and initialize detached-buffer elements to _undefined in TypedArray.prototype.{find, findLast, forEach}.
  • Use call.Argument(...) / nilSafe(...) in runtime.go, builtin_string.go, builtin_object.go, builtin_iterator.go, builtin_array.go, and builtin_typedarrays.go so nil entries in FunctionCall.Arguments / constructor args are consistently treated as undefined without breaking dense arrayObject invariants.
  • Add regression tests in builtin_arrray_test.go, builtin_typedarrays_test.go, and runtime_test.go.

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.

1 participant

0