Reviewers: Lasse Reichstein,

Description:
Fix crash: handle all flat string types in regexp replace.

Please review this at http://codereview.chromium.org/2868046/show

Affected files:
  M src/runtime.cc


Index: src/runtime.cc
diff --git a/src/runtime.cc b/src/runtime.cc
index e3a150d907419a10273d1cd8923d6566e1973811..b8ffda0a763cc6af2b835d83498494d8d2f70d20 100644
--- a/src/runtime.cc
+++ b/src/runtime.cc
@@ -2284,8 +2284,8 @@ static Object* StringReplaceRegExpWithString(String* subject,
   return *(builder.ToString());
 }

-template <typename ResultSeqString>
-static Object* StringReplaceRegExpWithEmptyString(ResultSeqString* subject,
+template <typename ResultSeqString, typename InputSeqString>
+static Object* StringReplaceRegExpWithEmptyString(InputSeqString* subject,
                                                   JSRegExp* regexp,
JSArray* last_match_info) {
   ASSERT(subject->IsFlat());
@@ -2440,14 +2440,35 @@ static Object* Runtime_StringReplaceRegExpWithString(Arguments args) {
   ASSERT(last_match_info->HasFastElements());

   if (replacement->length() == 0) {
+    if (subject->IsConsString()) {
+      ASSERT(!ConsString::cast(subject)->first()->IsConsString());
+      ASSERT(ConsString::cast(subject)->second()->length() == 0);
+      subject = ConsString::cast(subject)->first();
+    }
     if (subject->IsAsciiRepresentation()) {
- return StringReplaceRegExpWithEmptyString(SeqAsciiString::cast(subject),
-                                                regexp,
-                                                last_match_info);
+      if (subject->IsSeqAsciiString()) {
+        return StringReplaceRegExpWithEmptyString<SeqAsciiString>(
+            SeqAsciiString::cast(subject),
+            regexp,
+            last_match_info);
+      } else {
+        return StringReplaceRegExpWithEmptyString<SeqAsciiString>(
+            ExternalAsciiString::cast(subject),
+            regexp,
+            last_match_info);
+      }
     } else {
- return StringReplaceRegExpWithEmptyString(SeqTwoByteString::cast(subject),
-                                                regexp,
-                                                last_match_info);
+      if (subject->IsSeqTwoByteString()) {
+        return StringReplaceRegExpWithEmptyString<SeqTwoByteString>(
+            SeqTwoByteString::cast(subject),
+            regexp,
+            last_match_info);
+      } else {
+        return StringReplaceRegExpWithEmptyString<SeqTwoByteString>(
+            ExternalTwoByteString::cast(subject),
+            regexp,
+            last_match_info);
+      }
     }
   }



--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev

Reply via email to