Skip to content

TypedArray: map()/subarray() result SIGSEGVs on JSON.stringify, corrupts under Array.from/spread (slice/filter OK) #5111

Description

@proggeramlug

Found via a node --experimental-strip-types differential sweep on main (macOS arm64).

The TypedArray returned by .map() and .subarray() is usable for direct element access but is structurally broken for the object/iteration paths: JSON.stringify of it segfaults, and Array.from(...) / spread read garbage. .slice() and .filter() results are fine.

Repro

const m = new Int32Array([1, 2, 3]).map(x => x * 2);
console.log(m[0], m[1], m.length);   // 2 4 3      OK (direct access works)
console.log(JSON.stringify(m));      // SIGSEGV    (node: {"0":2,"1":4,"2":6})

const sub = new Int32Array([1, 2, 3, 4]).subarray(1, 3);
console.log(JSON.stringify(sub));    // SIGSEGV    (node: {"0":2,"1":3})

// Array.from corrupts instead of crashing:
console.log(Array.from(new Int32Array([1,2,3]).map(x=>x*2)));
// node:  [ 2, 4, 6 ]   perry: garbage (wrong element kind/offset)
console.log(Array.from(new Float64Array([1.5,2.5]).map(x=>x*2)));
// node:  [ 3, 5 ]      perry: garbage

What works vs. doesn't

op result direct [i] / .length JSON.stringify Array.from / spread
.map(cb) new TA works SIGSEGV garbage
.subarray(a,b) view works SIGSEGV garbage
.slice(a,b) new TA works works works
.filter(cb) new TA works works works

Notes

Severity: crash on a common operation (JSON.stringify(typedArray.map(...))).

Activity

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