Skip to content

Commit a43eeb5

Browse files
authored
Rename bindings to prevent overflow in Jest
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
1 parent cac6977 commit a43eeb5

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

lib/ref.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,14 @@ exports._attach = function _attach (buf, obj) {
683683
buf[kAttachedRefs].push(obj);
684684
}
685685

686-
exports._writeObject = exports.writeObject;
686+
/**
687+
* @param {Buffer} buffer
688+
* @param {Number} offset
689+
* @param {Object} object
690+
* @name _writeObject
691+
* @api private
692+
*/
693+
687694
/**
688695
* Writes a pointer to _object_ into _buffer_ at the specified _offset.
689696
*
@@ -716,11 +723,10 @@ exports.writeObject = function writeObject (buf, offset, obj) {
716723
* @param {Buffer} buffer A Buffer instance to write _pointer to.
717724
* @param {Number} offset The offset on the Buffer to start writing at.
718725
* @param {Buffer} pointer The Buffer instance whose memory address will be written to _buffer_.
726+
* @name _writePointer
719727
* @api private
720728
*/
721729

722-
exports._writePointer = exports.writePointer;
723-
724730
/**
725731
* Writes the memory address of _pointer_ to _buffer_ at the specified _offset_.
726732
*
@@ -753,11 +759,10 @@ exports.writePointer = function writePointer (buf, offset, ptr) {
753759
* @param {Number} size The `length` property of the returned Buffer.
754760
* @param {Number} offset The offset of the Buffer to begin from.
755761
* @return {Buffer} A new Buffer instance with the same memory address as _buffer_, and the requested _size_.
762+
* @name _reinterpret
756763
* @api private
757764
*/
758765

759-
exports._reinterpret = exports.reinterpret;
760-
761766
/**
762767
* Returns a new Buffer instance with the specified _size_, with the same memory
763768
* address as _buffer_.
@@ -787,11 +792,10 @@ exports.reinterpret = function reinterpret (buffer, size, offset) {
787792
* @param {Number} size The number of sequential, aligned `NULL` bytes that are required to terminate the buffer.
788793
* @param {Number} offset The offset of the Buffer to begin from.
789794
* @return {Buffer} A new Buffer instance with the same memory address as _buffer_, and a variable `length` that is terminated by _size_ NUL bytes.
795+
* @name _reinterpretUntilZeros
790796
* @api private
791797
*/
792798

793-
exports._reinterpretUntilZeros = exports.reinterpretUntilZeros;
794-
795799
/**
796800
* Accepts a `Buffer` instance and a number of `NULL` bytes to read from the
797801
* pointer. This function will scan past the boundary of the Buffer's `length`

src/binding.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -677,16 +677,16 @@ Object Init(Env env, Object exports) {
677677
exports["hexAddress"] = Function::New(env, HexAddress);
678678
exports["isNull"] = Function::New(env, IsNull);
679679
exports["readObject"] = Function::New(env, ReadObject);
680-
exports["writeObject"] = Function::New(env, WriteObject);
680+
exports["_writeObject"] = Function::New(env, WriteObject);
681681
exports["readPointer"] = Function::New(env, ReadPointer);
682-
exports["writePointer"] = Function::New(env, WritePointer);
682+
exports["_writePointer"] = Function::New(env, WritePointer);
683683
exports["readInt64"] = Function::New(env, ReadInt64);
684684
exports["writeInt64"] = Function::New(env, WriteInt64);
685685
exports["readUInt64"] = Function::New(env, ReadUInt64);
686686
exports["writeUInt64"] = Function::New(env, WriteUInt64);
687687
exports["readCString"] = Function::New(env, ReadCString);
688-
exports["reinterpret"] = Function::New(env, ReinterpretBuffer);
689-
exports["reinterpretUntilZeros"] = Function::New(env, ReinterpretBufferUntilZeros);
688+
exports["_reinterpret"] = Function::New(env, ReinterpretBuffer);
689+
exports["_reinterpretUntilZeros"] = Function::New(env, ReinterpretBufferUntilZeros);
690690
return exports;
691691
}
692692

0 commit comments

Comments
 (0)