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

n-api: fix warning about size_t compare with int #15508

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
40 changes: 23 additions & 17 deletions src/node_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,14 @@ struct napi_env__ {
#define CHECK_NEW_FROM_UTF8_LEN(env, result, str, len) \
do { \
auto str_maybe = v8::String::NewFromUtf8( \
(env)->isolate, (str), v8::NewStringType::kInternalized, (len)); \
(env)->isolate, (str), v8::NewStringType::kInternalized, \
static_cast<int>(len)); \
Copy link
Member

Choose a reason for hiding this comment

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

It would be nice to have a static_assert(static_cast<int>(NAPI_AUTO_LENGTH) == -1) somewhere

Copy link
Member

Choose a reason for hiding this comment

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

@addaleax that is a good idea.

Do you think it would make sense to add it just after

namespace {
namespace v8impl {

in node_api.cc

along with a comment indicating the v8 implementation of n-api depends on it being true ?

Copy link
Member

Choose a reason for hiding this comment

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

@mhdawson I like to put that near where it’s actually used (i.e. around here) but feel free to put it wherever you like :)

Copy link
Member

Choose a reason for hiding this comment

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

I was thinking you had somewhere else in mind, I'm good with it going into the macro itself

Copy link
Member

Choose a reason for hiding this comment

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

@mhdawson The issue remains that this static_cast is invalid if len > INT_MAX && len != NAPI_AUTO_LENGTH, there needs to code that checks for that condition and errors out if it is met … if necessary, I guess that can be done in a subsequent PR, but it’s a bug that ought to be fixed.

Copy link
Member

Choose a reason for hiding this comment

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

@addaleax thanks for the clarification, will look at that. Since we want to have the breaking changes in before 8.6 and we are almost out of time I may do it in a follow up PR, but I'll see on Monday.

Copy link
Member

@mhdawson mhdawson Sep 22, 2017

Choose a reason for hiding this comment

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

I think this check may do the trick

RETURN_STATUS_IF_FALSE((env), len <= INT_MAX, (napi_invalid_arg));

Copy link
Member

Choose a reason for hiding this comment

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

I do think you need to check for NAPI_AUTO_LENGTH because that value is usually larger than INT_MAX :) But yes, something like that sounds good

Copy link
Member

Choose a reason for hiding this comment

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

@addaleax agreed I also need to check for NAPI_AUTO_LENGTH. I'm going to land this PR to make sure we don't fix the Tuesday cutoff, and I'll submit a separate one to fix the bug caused by the cast on Monday.

Copy link
Member

Choose a reason for hiding this comment

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

To be clear, the current check does not have well-defined behavior. Compiler A might do the right thing, compiler B might do something completely different. IOW, this is not release-ready code.

CHECK_MAYBE_EMPTY((env), str_maybe, napi_generic_failure); \
(result) = str_maybe.ToLocalChecked(); \
} 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 @@ -918,21 +919,26 @@ NAPI_NO_RETURN void napi_fatal_error(const char* location,
size_t location_len,
const char* message,
size_t message_len) {
char* location_string = const_cast<char*>(location);
char* message_string = const_cast<char*>(message);
if (location_len != -1) {
location_string = reinterpret_cast<char*>(
malloc(location_len * sizeof(char) + 1));
strncpy(location_string, location, location_len);
location_string[location_len] = '\0';
}
if (message_len != -1) {
message_string = reinterpret_cast<char*>(
malloc(message_len * sizeof(char) + 1));
strncpy(message_string, message, message_len);
message_string[message_len] = '\0';
}
node::FatalError(location_string, message_string);
std::string location_string;
std::string message_string;

if (static_cast<int>(location_len) != NAPI_AUTO_LENGTH) {
Copy link
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
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
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
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
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
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.assign(
const_cast<char*>(location), location_len);
} else {
location_string.assign(
const_cast<char*>(location), strlen(location));
}

if (static_cast<int>(message_len) != NAPI_AUTO_LENGTH) {
message_string.assign(
const_cast<char*>(message), message_len);
} else {
message_string.assign(
const_cast<char*>(message), strlen(message));
}

node::FatalError(location_string.c_str(), message_string.c_str());
}

napi_status napi_create_function(napi_env env,
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