Skip to content

Commit

Permalink
test: make napi_get_buffer_info check if passed buffer is valid (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
toyobayashi authored Feb 24, 2024
1 parent 107bda9 commit 0472fe4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
6 changes: 2 additions & 4 deletions packages/emnapi/src/value/convert2c.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { SIZE_TYPE, from64, makeGetValue, makeSetValue } from 'emscripten:parse-
import { $emnapiSetValueI64 as emnapiSetValueI64 } from '../util'
import { emnapiString } from '../string'
import { emnapiExternalMemory } from '../memory'
import { $CHECK_ARG, $CHECK_ENV_NOT_IN_GC, $PREAMBLE } from '../macro'
import { $CHECK_ARG, $CHECK_ENV_NOT_IN_GC, $PREAMBLE, $RETURN_STATUS_IF_FALSE } from '../macro'

/**
* @__sig ippp
Expand Down Expand Up @@ -165,9 +165,7 @@ export function napi_get_buffer_info (
const envObject: Env = $CHECK_ENV_NOT_IN_GC!(env)
$CHECK_ARG!(envObject, buffer)
const handle = emnapiCtx.handleStore.get(buffer)!
if (!handle.isBuffer()) {
return envObject.setLastError(napi_status.napi_invalid_arg)
}
$RETURN_STATUS_IF_FALSE!(envObject, handle.isBuffer(), napi_status.napi_invalid_arg)
return napi_get_typedarray_info(env, buffer, 0, length, data, 0, 0)
}

Expand Down
17 changes: 17 additions & 0 deletions packages/test/buffer/binding.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,22 @@ static napi_value staticBuffer(napi_env env, napi_callback_info info) {
return theBuffer;
}

static napi_value invalidObjectAsBuffer(napi_env env, napi_callback_info info) {
size_t argc = 1;
napi_value args[1];
NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
NODE_API_ASSERT(env, argc == 1, "Wrong number of arguments");

napi_value notTheBuffer = args[0];
napi_status status = napi_get_buffer_info(env, notTheBuffer, NULL, NULL);
NODE_API_ASSERT(env,
status == napi_invalid_arg,
"napi_get_buffer_info: should fail with napi_invalid_arg "
"when passed non buffer");

return notTheBuffer;
}

static napi_value getMemoryDataAsArray(napi_env env, napi_callback_info info) {
size_t argc = 1;
napi_value args[1];
Expand Down Expand Up @@ -190,6 +206,7 @@ static napi_value Init(napi_env env, napi_value exports) {
DECLARE_NODE_API_PROPERTY("bufferHasInstance", bufferHasInstance),
DECLARE_NODE_API_PROPERTY("bufferInfo", bufferInfo),
DECLARE_NODE_API_PROPERTY("staticBuffer", staticBuffer),
DECLARE_NODE_API_PROPERTY("invalidObjectAsBuffer", invalidObjectAsBuffer),
DECLARE_NODE_API_PROPERTY("getMemoryDataAsArray", getMemoryDataAsArray),
};

Expand Down
3 changes: 3 additions & 0 deletions packages/test/buffer/buffer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ module.exports = load('buffer').then(async binding => {
assert.strictEqual(binding.bufferInfo(buffer), true)
buffer = null
await common.gcUntil(() => binding.getDeleterCallCount() === 2)

// To test this doesn't crash
binding.invalidObjectAsBuffer({})
})().then(common.mustCall())

process.externalBuffer = binding.newExternalBuffer()
Expand Down

0 comments on commit 0472fe4

Please sign in to comment.