From a43eeb5630ec8f68c4842dbf57c4086f01911bbe Mon Sep 17 00:00:00 2001 From: Andrew Dibble Date: Fri, 28 Aug 2020 15:45:12 +0200 Subject: [PATCH] Rename bindings to prevent overflow in Jest This is a fix for https://github.com/node-ffi-napi/ref-napi/issues/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: https://github.com/node-ffi-napi/ref-napi/pull/43 Fixes: https://github.com/node-ffi-napi/ref-napi/issues/16 --- lib/ref.js | 18 +++++++++++------- src/binding.cc | 8 ++++---- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/lib/ref.js b/lib/ref.js index 439b701..6d24375 100644 --- a/lib/ref.js +++ b/lib/ref.js @@ -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. * @@ -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_. * @@ -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_. @@ -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` diff --git a/src/binding.cc b/src/binding.cc index 418bf83..d1fefbd 100644 --- a/src/binding.cc +++ b/src/binding.cc @@ -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; }