Skip to content

Commit

Permalink
Rename bindings to prevent overflow in Jest
Browse files Browse the repository at this point in the history
This is a fix for #16, an issue that appears (especially in tests) when `writePointer` turns into an infinitely recursive call and overflows the stack.

This PR renames the native binding to already have the underscore to prevent the loop from ever being created.

This problem seems to surface mostly when using this library inside of Jest tests.

PR-URL: #43
Fixes: #16
  • Loading branch information
acdibble authored Aug 28, 2020
1 parent cac6977 commit a43eeb5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
18 changes: 11 additions & 7 deletions lib/ref.js
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,14 @@ exports._attach = function _attach (buf, obj) {
buf[kAttachedRefs].push(obj);
}

exports._writeObject = exports.writeObject;
/**
* @param {Buffer} buffer
* @param {Number} offset
* @param {Object} object
* @name _writeObject
* @api private
*/

/**
* Writes a pointer to _object_ into _buffer_ at the specified _offset.
*
Expand Down Expand Up @@ -716,11 +723,10 @@ exports.writeObject = function writeObject (buf, offset, obj) {
* @param {Buffer} buffer A Buffer instance to write _pointer to.
* @param {Number} offset The offset on the Buffer to start writing at.
* @param {Buffer} pointer The Buffer instance whose memory address will be written to _buffer_.
* @name _writePointer
* @api private
*/

exports._writePointer = exports.writePointer;

/**
* Writes the memory address of _pointer_ to _buffer_ at the specified _offset_.
*
Expand Down Expand Up @@ -753,11 +759,10 @@ exports.writePointer = function writePointer (buf, offset, ptr) {
* @param {Number} size The `length` property of the returned Buffer.
* @param {Number} offset The offset of the Buffer to begin from.
* @return {Buffer} A new Buffer instance with the same memory address as _buffer_, and the requested _size_.
* @name _reinterpret
* @api private
*/

exports._reinterpret = exports.reinterpret;

/**
* Returns a new Buffer instance with the specified _size_, with the same memory
* address as _buffer_.
Expand Down Expand Up @@ -787,11 +792,10 @@ exports.reinterpret = function reinterpret (buffer, size, offset) {
* @param {Number} size The number of sequential, aligned `NULL` bytes that are required to terminate the buffer.
* @param {Number} offset The offset of the Buffer to begin from.
* @return {Buffer} A new Buffer instance with the same memory address as _buffer_, and a variable `length` that is terminated by _size_ NUL bytes.
* @name _reinterpretUntilZeros
* @api private
*/

exports._reinterpretUntilZeros = exports.reinterpretUntilZeros;

/**
* Accepts a `Buffer` instance and a number of `NULL` bytes to read from the
* pointer. This function will scan past the boundary of the Buffer's `length`
Expand Down
8 changes: 4 additions & 4 deletions src/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -677,16 +677,16 @@ Object Init(Env env, Object exports) {
exports["hexAddress"] = Function::New(env, HexAddress);
exports["isNull"] = Function::New(env, IsNull);
exports["readObject"] = Function::New(env, ReadObject);
exports["writeObject"] = Function::New(env, WriteObject);
exports["_writeObject"] = Function::New(env, WriteObject);
exports["readPointer"] = Function::New(env, ReadPointer);
exports["writePointer"] = Function::New(env, WritePointer);
exports["_writePointer"] = Function::New(env, WritePointer);
exports["readInt64"] = Function::New(env, ReadInt64);
exports["writeInt64"] = Function::New(env, WriteInt64);
exports["readUInt64"] = Function::New(env, ReadUInt64);
exports["writeUInt64"] = Function::New(env, WriteUInt64);
exports["readCString"] = Function::New(env, ReadCString);
exports["reinterpret"] = Function::New(env, ReinterpretBuffer);
exports["reinterpretUntilZeros"] = Function::New(env, ReinterpretBufferUntilZeros);
exports["_reinterpret"] = Function::New(env, ReinterpretBuffer);
exports["_reinterpretUntilZeros"] = Function::New(env, ReinterpretBufferUntilZeros);
return exports;
}

Expand Down

0 comments on commit a43eeb5

Please sign in to comment.