-
Notifications
You must be signed in to change notification settings - Fork 30.1k
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
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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)); \ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mhdawson The issue remains that this There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this check may do the trick
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do think you need to check for There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 \ | ||
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think that There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
There was a problem hiding this comment.
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)
somewhereThere was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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 :)
There was a problem hiding this comment.
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