-
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
node-api: avoid crashing on passed-in null string #38923
Closed
gabrielschulhof
wants to merge
2
commits into
nodejs:master
from
gabrielschulhof:node-api-fix-string-null
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
#include <js_native_api.h> | ||
|
||
#include "../common.h" | ||
#include "test_null.h" | ||
|
||
#define DECLARE_TEST(charset, str_arg) \ | ||
static napi_value \ | ||
test_create_##charset(napi_env env, napi_callback_info info) { \ | ||
napi_value return_value, result; \ | ||
NODE_API_CALL(env, napi_create_object(env, &return_value)); \ | ||
\ | ||
add_returned_status(env, \ | ||
"envIsNull", \ | ||
return_value, \ | ||
"Invalid argument", \ | ||
napi_invalid_arg, \ | ||
napi_create_string_##charset(NULL, \ | ||
(str_arg), \ | ||
NAPI_AUTO_LENGTH, \ | ||
&result)); \ | ||
\ | ||
napi_create_string_##charset(env, NULL, NAPI_AUTO_LENGTH, &result); \ | ||
add_last_status(env, "stringIsNullNonZeroLength", return_value); \ | ||
\ | ||
napi_create_string_##charset(env, NULL, 0, &result); \ | ||
add_last_status(env, "stringIsNullZeroLength", return_value); \ | ||
\ | ||
napi_create_string_##charset(env, (str_arg), NAPI_AUTO_LENGTH, NULL); \ | ||
add_last_status(env, "resultIsNull", return_value); \ | ||
\ | ||
return return_value; \ | ||
} | ||
|
||
static const char16_t something[] = { | ||
(char16_t)'s', | ||
(char16_t)'o', | ||
(char16_t)'m', | ||
(char16_t)'e', | ||
(char16_t)'t', | ||
(char16_t)'h', | ||
(char16_t)'i', | ||
(char16_t)'n', | ||
(char16_t)'g', | ||
(char16_t)'\0' | ||
}; | ||
|
||
DECLARE_TEST(utf8, "something") | ||
DECLARE_TEST(latin1, "something") | ||
DECLARE_TEST(utf16, something) | ||
|
||
void init_test_null(napi_env env, napi_value exports) { | ||
napi_value test_null; | ||
|
||
const napi_property_descriptor test_null_props[] = { | ||
DECLARE_NODE_API_PROPERTY("test_create_utf8", test_create_utf8), | ||
DECLARE_NODE_API_PROPERTY("test_create_latin1", test_create_latin1), | ||
DECLARE_NODE_API_PROPERTY("test_create_utf16", test_create_utf16), | ||
}; | ||
|
||
NODE_API_CALL_RETURN_VOID(env, napi_create_object(env, &test_null)); | ||
NODE_API_CALL_RETURN_VOID(env, napi_define_properties( | ||
env, test_null, sizeof(test_null_props) / sizeof(*test_null_props), | ||
test_null_props)); | ||
|
||
const napi_property_descriptor test_null_set = { | ||
"testNull", NULL, NULL, NULL, NULL, test_null, napi_enumerable, NULL | ||
}; | ||
|
||
NODE_API_CALL_RETURN_VOID(env, | ||
napi_define_properties(env, exports, 1, &test_null_set)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#ifndef TEST_JS_NATIVE_API_TEST_STRING_TEST_NULL_H_ | ||
#define TEST_JS_NATIVE_API_TEST_STRING_TEST_NULL_H_ | ||
|
||
#include <js_native_api.h> | ||
|
||
void init_test_null(napi_env env, napi_value exports); | ||
|
||
#endif // TEST_JS_NATIVE_API_TEST_STRING_TEST_NULL_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
'use strict'; | ||
const common = require('../../common'); | ||
const assert = require('assert'); | ||
|
||
// Test passing NULL to object-related N-APIs. | ||
const { testNull } = require(`./build/${common.buildType}/test_string`); | ||
|
||
const expectedResult = { | ||
envIsNull: 'Invalid argument', | ||
stringIsNullNonZeroLength: 'Invalid argument', | ||
stringIsNullZeroLength: 'napi_ok', | ||
resultIsNull: 'Invalid argument', | ||
}; | ||
|
||
assert.deepStrictEqual(expectedResult, testNull.test_create_latin1()); | ||
assert.deepStrictEqual(expectedResult, testNull.test_create_utf8()); | ||
assert.deepStrictEqual(expectedResult, testNull.test_create_utf16()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The naming of these macros is really unfortunate. Everywhere else,
CHECK_*
generally implies an assert.