-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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 1 commit
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 |
---|---|---|
|
@@ -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 (location_len != -1) { | ||
if (static_cast<int>(location_len) != -1) { | ||
location_string = reinterpret_cast<char*>( | ||
malloc(location_len * sizeof(char) + 1)); | ||
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. 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. 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 (message_len != -1) { | ||
if (static_cast<int>(message_len) != -1) { | ||
message_string = reinterpret_cast<char*>( | ||
malloc(message_len * sizeof(char) + 1)); | ||
strncpy(message_string, message, message_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.
size_t might allow for larger values than signed ints and we're wrongly casting
2^n-1
to-1
(wheren
is how big size_t is). If we want to allow-1
here, why don't we change the parameter type? Or could we use0
instead of-1
to signal that we don't want to set themessage_string
?