Skip to content

runtime: typed-array view constructor new TA(buffer, byteOffset, length) doesn't validate offset/length (#3662 / #4093 follow-up) #4103

Closed

Description

@proggeramlug

Follow-up to PR #4093 (typed-array constructor length validation).

The 3-argument typed-array view constructor new TA(buffer, byteOffset, length)
does not validate byteOffset / length against the backing ArrayBuffer, so
out-of-range / misaligned views silently succeed instead of throwing the spec
RangeError.

Repro

new Uint8Array(new ArrayBuffer(8), 7, 4);   // Perry: no throw   Node: RangeError (offset+len > buffer)
new Uint32Array(new ArrayBuffer(8), 3);     // Perry: no throw   Node: RangeError (offset % 4 != 0)
new Uint8Array(new ArrayBuffer(8), 16);     // Perry: no throw   Node: RangeError (offset > byteLength)

(new TA(length) negative/out-of-range validation already works — PR #4093.
DataView(buffer, offset) already validates.)

Why it's deferred

Perry does not yet model real typed-array views — the byteOffset getter is
hardcoded to 0 and the multi-arg form lowers to Expr::New { class_name }
which drops the offset/length args at HIR. Proper validation needs the view to
actually honor byteOffset/length (the underlying feature), at which point the
bounds checks (offset % BYTES_PER_ELEMENT, offset + length*BPE <= byteLength)
fall out naturally.

Part of the #3662 cluster.

Activity

  1. proggeramlug commented on Jun 2, 2026

    @proggeramlug
    ContributorAuthor

    Verified fixed on main (d990b021e) by #4107 (which added the typedarray_view module + the codegen view-constructor arm). Byte-identical to node --experimental-strip-types:

    new Uint32Array(new ArrayBuffer(8), 3)      → RangeError (offset not a multiple of 4) ✓
    new Uint32Array(new ArrayBuffer(16), 4)     → length 3 ✓
    new Uint32Array(new ArrayBuffer(16), 4, 2)  → length 2 ✓
    new Uint32Array(new ArrayBuffer(16), 4, 9)  → RangeError (bounds) ✓
    new Uint32Array(new ArrayBuffer(8), 3, 1)   → RangeError (alignment) ✓
    new Uint8Array(new ArrayBuffer(8), 7, 4)    → RangeError ✓
    new Uint8Array(new ArrayBuffer(8), 16)      → RangeError ✓
    

    #4107 referenced but didn't auto-close this. Closing as completed.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions