Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Define NAPI_AUTO_LENGTH for string length
  • Loading branch information
Sampson Gao committed Sep 21, 2017
commit b1bf44a2220633d94c144149787c8135caf581e6
6 changes: 3 additions & 3 deletions src/node_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ struct napi_env__ {
} while (0)

#define CHECK_NEW_FROM_UTF8(env, result, str) \
CHECK_NEW_FROM_UTF8_LEN((env), (result), (str), -1)
CHECK_NEW_FROM_UTF8_LEN((env), (result), (str), NAPI_AUTO_LENGTH)

#define GET_RETURN_STATUS(env) \
(!try_catch.HasCaught() ? napi_ok \
Expand Down Expand Up @@ -920,13 +920,13 @@ NAPI_NO_RETURN void napi_fatal_error(const char* location,
size_t message_len) {
char* location_string = const_cast<char*>(location);
char* message_string = const_cast<char*>(message);
if (static_cast<int>(location_len) != -1) {
if (static_cast<int>(location_len) != NAPI_AUTO_LENGTH) {
Copy link
Copy Markdown
Member

@fhinkel fhinkel Sep 22, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that size_t always fits into an int. Why do we need to cast here? (Feel free to land once this is addressed if I don't get around to change my review to approve in time.)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree it should not be needed in this check. In places where it is passed on to v8 then it is needed.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mhdawson No, the issue isn’t that it’s in the wrong place – it’s that a downcast is a real bug here, because it truncates location_len

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@addaleax, to clarify we know that size_t does not fit into an int, so where its going to be passed to v8 a cast is required to avoid warnings. In this case we should just be comparing NAPI_AUTO_LENGTH directly against location_len.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mhdawson Yeah, but we can’t just downcast without any checks in this case. Imagine a platform that has 32-bit int, and 64-bit size_t. When an addon tries to create a N-API string that contains (2^32 + 1) characters – not knowing that that doesn’t work, because, how would it tell? – N-API would currently silently truncate that size argument to 1, and happily create a one-character string. I think that qualifies as a bug.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'm still missing something here, sorry. If we change to be:

if (location_len != NAPI_AUTO_LENGTH)

is it that there is a problem later on ?

location_string = reinterpret_cast<char*>(
malloc(location_len * sizeof(char) + 1));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aside: sizeof(char) is 1 by definition and reinterpret_cast is overkill here, static_cast would have sufficed.

What's more, I don't understand why this code makes copies. This function does not return. It seems like completely unneeded complexity.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason for doing copy here is to account for strings that are not null terminated.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, but the other two points still stand. As well, you don't handle nullptr return values. If you use node::Malloc(), that's taken care of.

Or use std::string, that will also stop Coverity from complaining about memory leaks.

strncpy(location_string, location, location_len);
location_string[location_len] = '\0';
}
if (static_cast<int>(message_len) != -1) {
if (static_cast<int>(message_len) != NAPI_AUTO_LENGTH) {
message_string = reinterpret_cast<char*>(
malloc(message_len * sizeof(char) + 1));
strncpy(message_string, message, message_len);
Expand Down
2 changes: 2 additions & 0 deletions src/node_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ typedef struct {
#define NAPI_MODULE(modname, regfunc) \
NAPI_MODULE_X(modname, regfunc, NULL, 0)

#define NAPI_AUTO_LENGTH SIZE_MAX

EXTERN_C_START

NAPI_EXTERN void napi_module_register(napi_module* mod);
Expand Down
8 changes: 4 additions & 4 deletions test/addons-napi/test_async/test_async.cc
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ napi_value Test(napi_env env, napi_callback_info info) {
NAPI_CALL(env,
napi_create_reference(env, argv[2], 1, &the_carrier._callback));

NAPI_CALL(env,
napi_create_string_utf8(env, "TestResource", -1, &resource_name));
NAPI_CALL(env, napi_create_string_utf8(
env, "TestResource", NAPI_AUTO_LENGTH, &resource_name));
NAPI_CALL(env, napi_create_async_work(env, argv[1], resource_name,
Execute, Complete, &the_carrier, &the_carrier._request));
NAPI_CALL(env,
Expand Down Expand Up @@ -145,8 +145,8 @@ napi_value TestCancel(napi_env env, napi_callback_info info) {
napi_value resource_name;
void* data;

NAPI_CALL(env,
napi_create_string_utf8(env, "TestResource", -1, &resource_name));
NAPI_CALL(env, napi_create_string_utf8(
env, "TestResource", NAPI_AUTO_LENGTH, &resource_name));

// make sure the work we are going to cancel will not be
// able to start by using all the threads in the pool
Expand Down
2 changes: 1 addition & 1 deletion test/addons-napi/test_constructor/test_constructor.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ napi_value Init(napi_env env, napi_value exports) {
};

napi_value cons;
NAPI_CALL(env, napi_define_class(env, "MyObject", -1, New,
NAPI_CALL(env, napi_define_class(env, "MyObject", NAPI_AUTO_LENGTH, New,
NULL, sizeof(properties)/sizeof(*properties), properties, &cons));

NAPI_CALL(env, napi_create_reference(env, cons, 1, &constructor_));
Expand Down
3 changes: 2 additions & 1 deletion test/addons-napi/test_conversions/test_conversions.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ napi_value AsString(napi_env env, napi_callback_info info) {
napi_get_value_string_utf8(env, args[0], value, sizeof(value), NULL));

napi_value output;
NAPI_CALL(env, napi_create_string_utf8(env, value, -1, &output));
NAPI_CALL(env, napi_create_string_utf8(
env, value, NAPI_AUTO_LENGTH, &output));

return output;
}
Expand Down
3 changes: 2 additions & 1 deletion test/addons-napi/test_env_sharing/compare_env.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ napi_value compare(napi_env env, napi_callback_info info) {
}

napi_value Init(napi_env env, napi_value exports) {
NAPI_CALL(env, napi_create_function(env, "exports", -1, compare, NULL, &exports));
NAPI_CALL(env, napi_create_function(
env, "exports", NAPI_AUTO_LENGTH, compare, NULL, &exports));
return exports;
}

Expand Down
28 changes: 18 additions & 10 deletions test/addons-napi/test_error/test_error.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ napi_value checkError(napi_env env, napi_callback_info info) {
napi_value throwExistingError(napi_env env, napi_callback_info info) {
napi_value message;
napi_value error;
NAPI_CALL(env, napi_create_string_utf8(env, "existing error", -1, &message));
NAPI_CALL(env, napi_create_string_utf8(
env, "existing error", NAPI_AUTO_LENGTH, &message));
NAPI_CALL(env, napi_create_error(env, nullptr, message, &error));
NAPI_CALL(env, napi_throw(env, error));
return nullptr;
Expand Down Expand Up @@ -62,23 +63,26 @@ napi_value throwTypeErrorCode(napi_env env, napi_callback_info info) {
napi_value createError(napi_env env, napi_callback_info info) {
napi_value result;
napi_value message;
NAPI_CALL(env, napi_create_string_utf8(env, "error", -1, &message));
NAPI_CALL(env, napi_create_string_utf8(
env, "error", NAPI_AUTO_LENGTH, &message));
NAPI_CALL(env, napi_create_error(env, nullptr, message, &result));
return result;
}

napi_value createRangeError(napi_env env, napi_callback_info info) {
napi_value result;
napi_value message;
NAPI_CALL(env, napi_create_string_utf8(env, "range error", -1, &message));
NAPI_CALL(env, napi_create_string_utf8(
env, "range error", NAPI_AUTO_LENGTH, &message));
NAPI_CALL(env, napi_create_range_error(env, nullptr, message, &result));
return result;
}

napi_value createTypeError(napi_env env, napi_callback_info info) {
napi_value result;
napi_value message;
NAPI_CALL(env, napi_create_string_utf8(env, "type error", -1, &message));
NAPI_CALL(env, napi_create_string_utf8(
env, "type error", NAPI_AUTO_LENGTH, &message));
NAPI_CALL(env, napi_create_type_error(env, nullptr, message, &result));
return result;
}
Expand All @@ -87,8 +91,10 @@ napi_value createErrorCode(napi_env env, napi_callback_info info) {
napi_value result;
napi_value message;
napi_value code;
NAPI_CALL(env, napi_create_string_utf8(env, "Error [error]", -1, &message));
NAPI_CALL(env, napi_create_string_utf8(env, "ERR_TEST_CODE", -1, &code));
NAPI_CALL(env, napi_create_string_utf8(
env, "Error [error]", NAPI_AUTO_LENGTH, &message));
NAPI_CALL(env, napi_create_string_utf8(
env, "ERR_TEST_CODE", NAPI_AUTO_LENGTH, &code));
NAPI_CALL(env, napi_create_error(env, code, message, &result));
return result;
}
Expand All @@ -99,9 +105,10 @@ napi_value createRangeErrorCode(napi_env env, napi_callback_info info) {
napi_value code;
NAPI_CALL(env, napi_create_string_utf8(env,
"RangeError [range error]",
-1,
NAPI_AUTO_LENGTH,
&message));
NAPI_CALL(env, napi_create_string_utf8(env, "ERR_TEST_CODE", -1, &code));
NAPI_CALL(env, napi_create_string_utf8(
env, "ERR_TEST_CODE", NAPI_AUTO_LENGTH, &code));
NAPI_CALL(env, napi_create_range_error(env, code, message, &result));
return result;
}
Expand All @@ -112,9 +119,10 @@ napi_value createTypeErrorCode(napi_env env, napi_callback_info info) {
napi_value code;
NAPI_CALL(env, napi_create_string_utf8(env,
"TypeError [type error]",
-1,
NAPI_AUTO_LENGTH,
&message));
NAPI_CALL(env, napi_create_string_utf8(env, "ERR_TEST_CODE", -1, &code));
NAPI_CALL(env, napi_create_string_utf8(
env, "ERR_TEST_CODE", NAPI_AUTO_LENGTH, &code));
NAPI_CALL(env, napi_create_type_error(env, code, message, &result));
return result;
}
Expand Down
3 changes: 2 additions & 1 deletion test/addons-napi/test_fatal/test_fatal.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
#include "../common.h"

napi_value Test(napi_env env, napi_callback_info info) {
napi_fatal_error("test_fatal::Test", -1, "fatal message", -1);
napi_fatal_error("test_fatal::Test", NAPI_AUTO_LENGTH,
"fatal message", NAPI_AUTO_LENGTH);
return NULL;
}

Expand Down
4 changes: 2 additions & 2 deletions test/addons-napi/test_function/test_function.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ napi_value TestFunctionName(napi_env env, napi_callback_info info) {
napi_value Init(napi_env env, napi_value exports) {
napi_value fn1;
NAPI_CALL(env, napi_create_function(
env, NULL, -1, TestCallFunction, NULL, &fn1));
env, NULL, NAPI_AUTO_LENGTH, TestCallFunction, NULL, &fn1));

napi_value fn2;
NAPI_CALL(env, napi_create_function(
env, "Name", -1, TestFunctionName, NULL, &fn2));
env, "Name", NAPI_AUTO_LENGTH, TestFunctionName, NULL, &fn2));

napi_value fn3;
NAPI_CALL(env, napi_create_function(
Expand Down
26 changes: 17 additions & 9 deletions test/addons-napi/test_general/test_general.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ napi_value testGetNodeVersion(napi_env env, napi_callback_info info) {
NAPI_CALL(env, napi_create_uint32(env, node_version->patch, &patch));
NAPI_CALL(env, napi_create_string_utf8(env,
node_version->release,
(size_t)-1,
NAPI_AUTO_LENGTH,
&release));
NAPI_CALL(env, napi_create_array_with_length(env, 4, &result));
NAPI_CALL(env, napi_set_element(env, result, 0, major));
Expand Down Expand Up @@ -120,21 +120,29 @@ napi_value testNapiTypeof(napi_env env, napi_callback_info info) {

napi_value result = NULL;
if (argument_type == napi_number) {
NAPI_CALL(env, napi_create_string_utf8(env, "number", -1, &result));
NAPI_CALL(env, napi_create_string_utf8(
env, "number", NAPI_AUTO_LENGTH, &result));
} else if (argument_type == napi_string) {
NAPI_CALL(env, napi_create_string_utf8(env, "string", -1, &result));
NAPI_CALL(env, napi_create_string_utf8(
env, "string", NAPI_AUTO_LENGTH, &result));
} else if (argument_type == napi_function) {
NAPI_CALL(env, napi_create_string_utf8(env, "function", -1, &result));
NAPI_CALL(env, napi_create_string_utf8(
env, "function", NAPI_AUTO_LENGTH, &result));
} else if (argument_type == napi_object) {
NAPI_CALL(env, napi_create_string_utf8(env, "object", -1, &result));
NAPI_CALL(env, napi_create_string_utf8(
env, "object", NAPI_AUTO_LENGTH, &result));
} else if (argument_type == napi_boolean) {
NAPI_CALL(env, napi_create_string_utf8(env, "boolean", -1, &result));
NAPI_CALL(env, napi_create_string_utf8(
env, "boolean", NAPI_AUTO_LENGTH, &result));
} else if (argument_type == napi_undefined) {
NAPI_CALL(env, napi_create_string_utf8(env, "undefined", -1, &result));
NAPI_CALL(env, napi_create_string_utf8(
env, "undefined", NAPI_AUTO_LENGTH, &result));
} else if (argument_type == napi_symbol) {
NAPI_CALL(env, napi_create_string_utf8(env, "symbol", -1, &result));
NAPI_CALL(env, napi_create_string_utf8(
env, "symbol", NAPI_AUTO_LENGTH, &result));
} else if (argument_type == napi_null) {
NAPI_CALL(env, napi_create_string_utf8(env, "null", -1, &result));
NAPI_CALL(env, napi_create_string_utf8(
env, "null", NAPI_AUTO_LENGTH, &result));
}
return result;
}
Expand Down
6 changes: 4 additions & 2 deletions test/addons-napi/test_make_callback/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ napi_value MakeCallback(napi_env env, napi_callback_info info) {
NAPI_CALL(env, napi_typeof(env, func, &func_type));

napi_value resource_name;
NAPI_CALL(env, napi_create_string_utf8(env, "test", -1, &resource_name));
NAPI_CALL(env, napi_create_string_utf8(
env, "test", NAPI_AUTO_LENGTH, &resource_name));

napi_async_context context;
NAPI_CALL(env, napi_async_init(env, func, resource_name, &context));
Expand All @@ -45,7 +46,8 @@ napi_value MakeCallback(napi_env env, napi_callback_info info) {

napi_value Init(napi_env env, napi_value exports) {
napi_value fn;
NAPI_CALL(env, napi_create_function(env, NULL, -1, MakeCallback, NULL, &fn));
NAPI_CALL(env, napi_create_function(
env, NULL, NAPI_AUTO_LENGTH, MakeCallback, NULL, &fn));
NAPI_CALL(env, napi_set_named_property(env, exports, "makeCallback", fn));
return exports;
}
Expand Down
3 changes: 2 additions & 1 deletion test/addons-napi/test_make_callback_recurse/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ napi_value MakeCallback(napi_env env, napi_callback_info info) {

napi_value Init(napi_env env, napi_value exports) {
napi_value fn;
NAPI_CALL(env, napi_create_function(env, NULL, -1, MakeCallback, NULL, &fn));
NAPI_CALL(env, napi_create_function(
env, NULL, NAPI_AUTO_LENGTH, MakeCallback, NULL, &fn));
NAPI_CALL(env, napi_set_named_property(env, exports, "makeCallback", fn));
return exports;
}
Expand Down
4 changes: 2 additions & 2 deletions test/addons-napi/test_properties/test_properties.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,14 @@ napi_value Init(napi_env env, napi_value exports) {
napi_value name_value;
NAPI_CALL(env, napi_create_string_utf8(env,
"NameKeyValue",
-1,
NAPI_AUTO_LENGTH,
&name_value));

napi_value symbol_description;
napi_value name_symbol;
NAPI_CALL(env, napi_create_string_utf8(env,
"NameKeySymbol",
-1,
NAPI_AUTO_LENGTH,
&symbol_description));
NAPI_CALL(env, napi_create_symbol(env,
symbol_description,
Expand Down