-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
n-api: fix warning about size_t compare with int #15508
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 \ | ||
|
|
@@ -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) { | ||
| location_string = reinterpret_cast<char*>( | ||
| malloc(location_len * sizeof(char) + 1)); | ||
|
Member
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. Aside: What's more, I don't understand why this code makes copies. This function does not return. It seems like completely unneeded complexity.
Author
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. The reason for doing copy here is to account for strings that are not null terminated.
Member
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. Okay, but the other two points still stand. As well, you don't handle nullptr return values. If you use Or use |
||
| 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); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
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 don't think that
size_talways fits into anint. 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 toapprovein time.)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 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 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_lenThere 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, 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 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-bitsize_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.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 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 ?