Reviewers: Jakob,

Description:
Throw on range error when creating a string via API.

[email protected]
BUG=v8:3853
LOG=Y

Please review this at https://codereview.chromium.org/867373003/

Base URL: https://chromium.googlesource.com/v8/v8.git@master

Affected files (+41, -8 lines):
  M include/v8.h
  M src/api.cc
  M test/cctest/test-api.cc


Index: include/v8.h
diff --git a/include/v8.h b/include/v8.h
index 28b140ea70e75ca3cc4241b52cf80059198d1216..7773428edd479288b881cbd220a14de21af67e95 100644
--- a/include/v8.h
+++ b/include/v8.h
@@ -2135,10 +2135,9 @@ class V8_EXPORT String : public Name {
   };

   /** Allocates a new string from UTF-8 data.*/
-  static Local<String> NewFromUtf8(Isolate* isolate,
-                                  const char* data,
-                                  NewStringType type = kNormalString,
-                                  int length = -1);
+  static Local<String> NewFromUtf8(Isolate* isolate, const char* data,
+                                   NewStringType type = kNormalString,
+                                   int length = -1);

   /** Allocates a new string from Latin-1 data.*/
   static Local<String> NewFromOneByte(
Index: src/api.cc
diff --git a/src/api.cc b/src/api.cc
index 304d880d3da3017f3f21a8a3b39b3ccc9456c09f..a683add29bff17877b4a2b26adb960e0650e983b 100644
--- a/src/api.cc
+++ b/src/api.cc
@@ -5438,10 +5438,12 @@ inline Local<String> NewString(Isolate* v8_isolate,
   ENTER_V8(isolate);
   if (length == -1) length = StringLength(data);
   // We do not expect this to fail. Change this if it does.
-  i::Handle<i::String> result = NewString(
-      isolate->factory(),
-      type,
-      i::Vector<const Char>(data, length)).ToHandleChecked();
+  EXCEPTION_PREAMBLE(isolate);
+  i::Handle<i::String> result;
+  has_pending_exception =
+ !NewString(isolate->factory(), type, i::Vector<const Char>(data, length))
+           .ToHandle(&result);
+  EXCEPTION_BAILOUT_CHECK(isolate, Local<String>());
   if (type == String::kUndetectableString) {
     result->MarkAsUndetectable();
   }
Index: test/cctest/test-api.cc
diff --git a/test/cctest/test-api.cc b/test/cctest/test-api.cc
index 753b4716b0f6e5b778c7853ea823cbeaa581ffe9..7b5cca51196abeac5012ae59c0cbc71c24d634c0 100644
--- a/test/cctest/test-api.cc
+++ b/test/cctest/test-api.cc
@@ -21984,3 +21984,35 @@ TEST(StreamingScriptWithSourceMappingURLInTheMiddle) { RunStreamingTest(chunks, v8::ScriptCompiler::StreamedSource::UTF8, true, NULL,
                    "bar2.js");
 }
+
+
+TEST(NewStringRangeError) {
+  v8::Isolate* isolate = CcTest::isolate();
+  v8::HandleScope handle_scope(isolate);
+  LocalContext env;
+  const int length = i::String::kMaxLength + 1;
+  const int buffer_size = length * sizeof(uint16_t);
+  void* buffer = malloc(buffer_size);
+  memset(buffer, 'A', buffer_size);
+  {
+    v8::TryCatch try_catch;
+    char* data = reinterpret_cast<char*>(buffer);
+    CHECK(v8::String::NewFromUtf8(isolate, data, v8::String::kNormalString,
+                                  length).IsEmpty());
+    CHECK(try_catch.HasCaught());
+  }
+  {
+    v8::TryCatch try_catch;
+    uint8_t* data = reinterpret_cast<uint8_t*>(buffer);
+ CHECK(v8::String::NewFromOneByte(isolate, data, v8::String::kNormalString,
+                                     length).IsEmpty());
+    CHECK(try_catch.HasCaught());
+  }
+  {
+    v8::TryCatch try_catch;
+    uint16_t* data = reinterpret_cast<uint16_t*>(buffer);
+ CHECK(v8::String::NewFromTwoByte(isolate, data, v8::String::kNormalString,
+                                     length).IsEmpty());
+    CHECK(try_catch.HasCaught());
+  }
+}


--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
--- You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to