[api] Fix external strings of length 0

External string resources of length {0} have a {nullptr} data since
https://crrev.com/c/1424861.
This CL fixes allocation of an external one-byte string from such a
resource.

R=ishell@chromium.org

Bug: chromium:995108
Change-Id: Ic8ef507d808583c1b529085d990d705058953f03
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1781051
Reviewed-by: Igor Sheludko <ishell@chromium.org>
Commit-Queue: Clemens Hammacher <clemensh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#63543}
This commit is contained in:
Clemens Hammacher 2019-09-02 14:21:24 +02:00 committed by Commit Bot
parent afca89f848
commit 6c06e62eeb

View File

@ -6251,7 +6251,7 @@ MaybeLocal<String> v8::String::NewExternalTwoByte(
MaybeLocal<String> v8::String::NewExternalOneByte( MaybeLocal<String> v8::String::NewExternalOneByte(
Isolate* isolate, v8::String::ExternalOneByteStringResource* resource) { Isolate* isolate, v8::String::ExternalOneByteStringResource* resource) {
CHECK(resource && resource->data()); CHECK_NOT_NULL(resource);
// TODO(dcarney): throw a context free exception. // TODO(dcarney): throw a context free exception.
if (resource->length() > static_cast<size_t>(i::String::kMaxLength)) { if (resource->length() > static_cast<size_t>(i::String::kMaxLength)) {
return MaybeLocal<String>(); return MaybeLocal<String>();
@ -6259,16 +6259,16 @@ MaybeLocal<String> v8::String::NewExternalOneByte(
i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate); ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate);
LOG_API(i_isolate, String, NewExternalOneByte); LOG_API(i_isolate, String, NewExternalOneByte);
if (resource->length() > 0) { if (resource->length() == 0) {
i::Handle<i::String> string = i_isolate->factory()
->NewExternalStringFromOneByte(resource)
.ToHandleChecked();
return Utils::ToLocal(string);
} else {
// The resource isn't going to be used, free it immediately. // The resource isn't going to be used, free it immediately.
resource->Dispose(); resource->Dispose();
return Utils::ToLocal(i_isolate->factory()->empty_string()); return Utils::ToLocal(i_isolate->factory()->empty_string());
} }
CHECK_NOT_NULL(resource->data());
i::Handle<i::String> string = i_isolate->factory()
->NewExternalStringFromOneByte(resource)
.ToHandleChecked();
return Utils::ToLocal(string);
} }
bool v8::String::MakeExternal(v8::String::ExternalStringResource* resource) { bool v8::String::MakeExternal(v8::String::ExternalStringResource* resource) {