Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: make napi_get_buffer_info check if passed buffer is valid #108

Merged
merged 2 commits into from
Feb 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading