Skip to content

Commit 801bd06

Browse files
tniessenNickNaso
authored andcommitted
napi: fix memory corruption vulnerability
Re: https://github.com/nodejs-private/node-private/pull/195
1 parent 5a7f8b2 commit 801bd06

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/node_api.cc

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2238,7 +2238,7 @@ napi_status napi_get_value_string_latin1(napi_env env,
22382238
if (!buf) {
22392239
CHECK_ARG(env, result);
22402240
*result = val.As<v8::String>()->Length();
2241-
} else {
2241+
} else if (bufsize != 0) {
22422242
int copied = val.As<v8::String>()->WriteOneByte(
22432243
reinterpret_cast<uint8_t*>(buf), 0, bufsize - 1,
22442244
v8::String::NO_NULL_TERMINATION);
@@ -2247,6 +2247,8 @@ napi_status napi_get_value_string_latin1(napi_env env,
22472247
if (result != nullptr) {
22482248
*result = copied;
22492249
}
2250+
} else if (result != nullptr) {
2251+
*result = 0;
22502252
}
22512253

22522254
return napi_clear_last_error(env);
@@ -2274,7 +2276,7 @@ napi_status napi_get_value_string_utf8(napi_env env,
22742276
if (!buf) {
22752277
CHECK_ARG(env, result);
22762278
*result = val.As<v8::String>()->Utf8Length();
2277-
} else {
2279+
} else if (bufsize != 0) {
22782280
int copied = val.As<v8::String>()->WriteUtf8(
22792281
buf, bufsize - 1, nullptr, v8::String::REPLACE_INVALID_UTF8 |
22802282
v8::String::NO_NULL_TERMINATION);
@@ -2283,6 +2285,8 @@ napi_status napi_get_value_string_utf8(napi_env env,
22832285
if (result != nullptr) {
22842286
*result = copied;
22852287
}
2288+
} else if (result != nullptr) {
2289+
*result = 0;
22862290
}
22872291

22882292
return napi_clear_last_error(env);
@@ -2311,7 +2315,7 @@ napi_status napi_get_value_string_utf16(napi_env env,
23112315
CHECK_ARG(env, result);
23122316
// V8 assumes UTF-16 length is the same as the number of characters.
23132317
*result = val.As<v8::String>()->Length();
2314-
} else {
2318+
} else if (bufsize != 0) {
23152319
int copied = val.As<v8::String>()->Write(
23162320
reinterpret_cast<uint16_t*>(buf), 0, bufsize - 1,
23172321
v8::String::NO_NULL_TERMINATION);
@@ -2320,6 +2324,8 @@ napi_status napi_get_value_string_utf16(napi_env env,
23202324
if (result != nullptr) {
23212325
*result = copied;
23222326
}
2327+
} else if (result != nullptr) {
2328+
*result = 0;
23232329
}
23242330

23252331
return napi_clear_last_error(env);

0 commit comments

Comments
 (0)