Skip to content

SIGSEGV when reading object before write #5

Closed

Description

@theosotr
'use strict';
const ref = require("./build/Release/binding.node");

const buf = Buffer.alloc(64, 1);
ref.readObject(buf, 0);

leads to

segmentation fault (core dumped)  node repro.js

The problem is that the buffer contents are interpreted as references to object heaps before writing any reference to an object into the buffer.

Activity

  1. self-assigned this
    on Jul 28, 2026
  2. urbanogilson commented on Jul 28, 2026

    @urbanogilson
    Member

    @theosotr thanks for the clear repro, confirmed and root-caused this.

    readObject/writeObject reinterpret the raw Buffer memory directly as a Napi::Reference<Object> (a real C++ object with {env, ref, suppressDestruct} fields), with no check that the bytes at that address actually hold a previously-written reference. Since Buffer.alloc(64, 1) fills the memory with 0x01 bytes, the reinterpreted ref field becomes a bogus non-null "handle," and N-API tries to dereference it, crashing the whole process instead of failing safely. (Zero-filled buffers happened to work by accident, since a null handle short-circuits before touching invalid memory, but nothing actually enforced that requirement.)

  3. urbanogilson commented on Jul 28, 2026

    @urbanogilson
    Member

    I'll tag the memory with a magic marker written by writeObject on first use. readObject now checks for that marker and throws a normal, catchable Error ("Cannot read Object from uninitialized memory") when it's absent, instead of dereferencing untrusted bytes. writeObject on unmarked memory now safely bootstraps a fresh reference in place rather than assuming the existing bytes are valid.

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

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions