Skip to content

Commit

Permalink
napi: fix memory corruption vulnerability
Browse files Browse the repository at this point in the history
  • Loading branch information
tniessen authored and NickNaso committed Jun 1, 2020
1 parent f677794 commit 265fea9
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/node_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2238,7 +2238,7 @@ napi_status napi_get_value_string_latin1(napi_env env,
if (!buf) {
CHECK_ARG(env, result);
*result = val.As<v8::String>()->Length();
} else {
} else if (bufsize != 0) {
int copied = val.As<v8::String>()->WriteOneByte(
reinterpret_cast<uint8_t*>(buf), 0, bufsize - 1,
v8::String::NO_NULL_TERMINATION);
Expand All @@ -2247,6 +2247,8 @@ napi_status napi_get_value_string_latin1(napi_env env,
if (result != nullptr) {
*result = copied;
}
} else if (result != nullptr) {
*result = 0;
}

return napi_clear_last_error(env);
Expand Down Expand Up @@ -2274,7 +2276,7 @@ napi_status napi_get_value_string_utf8(napi_env env,
if (!buf) {
CHECK_ARG(env, result);
*result = val.As<v8::String>()->Utf8Length();
} else {
} else if (bufsize != 0) {
int copied = val.As<v8::String>()->WriteUtf8(
buf, bufsize - 1, nullptr, v8::String::REPLACE_INVALID_UTF8 |
v8::String::NO_NULL_TERMINATION);
Expand All @@ -2283,6 +2285,8 @@ napi_status napi_get_value_string_utf8(napi_env env,
if (result != nullptr) {
*result = copied;
}
} else if (result != nullptr) {
*result = 0;
}

return napi_clear_last_error(env);
Expand Down Expand Up @@ -2311,7 +2315,7 @@ napi_status napi_get_value_string_utf16(napi_env env,
CHECK_ARG(env, result);
// V8 assumes UTF-16 length is the same as the number of characters.
*result = val.As<v8::String>()->Length();
} else {
} else if (bufsize != 0) {
int copied = val.As<v8::String>()->Write(
reinterpret_cast<uint16_t*>(buf), 0, bufsize - 1,
v8::String::NO_NULL_TERMINATION);
Expand All @@ -2320,6 +2324,8 @@ napi_status napi_get_value_string_utf16(napi_env env,
if (result != nullptr) {
*result = copied;
}
} else if (result != nullptr) {
*result = 0;
}

return napi_clear_last_error(env);
Expand Down

0 comments on commit 265fea9

Please sign in to comment.