Skip to content

Commit da50b51

Browse files
JckXiamhdawson
authored andcommitted
test: dd check for nullptr inside String init
PR-URL: #1015 Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: Kevin Eady <[email protected]>
1 parent 627dbf3 commit da50b51

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

napi-inl.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -900,6 +900,11 @@ inline String String::New(napi_env env, const std::u16string& val) {
900900
}
901901

902902
inline String String::New(napi_env env, const char* val) {
903+
if (val == nullptr) {
904+
NAPI_THROW(
905+
TypeError::New(env, "String::New received a nullpointer as a value"),
906+
Napi::String());
907+
}
903908
napi_value value;
904909
napi_status status = napi_create_string_utf8(env, val, std::strlen(val), &value);
905910
NAPI_THROW_IF_FAILED(env, status, String());

test/name.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,18 @@ Value CheckSymbol(const CallbackInfo& info) {
8282
return Boolean::New(info.Env(), info[0].Type() == napi_symbol);
8383
}
8484

85+
void AssertErrorThrownWhenPassedNullptr(const CallbackInfo& info) {
86+
const char* nullStr = nullptr;
87+
String::New(info.Env(), nullStr);
88+
}
89+
8590
Object InitName(Env env) {
8691
Object exports = Object::New(env);
8792

8893
exports["echoString"] = Function::New(env, EchoString);
8994
exports["createString"] = Function::New(env, CreateString);
95+
exports["nullStringShouldThrow"] =
96+
Function::New(env, AssertErrorThrownWhenPassedNullptr);
9097
exports["checkString"] = Function::New(env, CheckString);
9198
exports["createSymbol"] = Function::New(env, CreateSymbol);
9299
exports["checkSymbol"] = Function::New(env, CheckSymbol);

test/name.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ module.exports = require('./common').runTest(test);
77
function test(binding) {
88
const expected = '123456789';
99

10+
11+
assert.throws(binding.name.nullStringShouldThrow, {
12+
name: 'TypeError',
13+
message: 'String::New received a nullpointer as a value',
14+
});
1015
assert.ok(binding.name.checkString(expected, 'utf8'));
1116
assert.ok(binding.name.checkString(expected, 'utf16'));
1217
assert.ok(binding.name.checkString(expected.substr(0, 3), 'utf8', 3));

0 commit comments

Comments
 (0)