Title: [201624] trunk
Revision
201624
Author
[email protected]
Date
2016-06-02 16:07:48 -0700 (Thu, 02 Jun 2016)

Log Message

JS parser incorrectly handles invalid utf8 in error messages.
https://bugs.webkit.org/show_bug.cgi?id=158128

Reviewed by Saam Barati.

Source/_javascript_Core:

The bug here was caused by us using PrintStream's toString method
to produce the error message for a parse error, even though toString
may produce a null string in the event of invalid utf8 that causes
the error in first case. So when we try to create an error message
containing the invalid character code, we set m_errorMessage to the
null string, as that signals "no error" we don't stop parsing, and
everything goes down hill from there.

Now we use the new toStringWithLatin1Fallback so that we can always
produce an error message, even if it contains invalid unicode. We
also add an additional fallback so that we can guarantee an error
message is set even if we're given a null string. There's a debug
mode assertion to prevent anyone accidentally attempting to clear
the message via setErrorMessage.

* parser/Parser.cpp:
(JSC::Parser<LexerType>::logError):
* parser/Parser.h:
(JSC::Parser::setErrorMessage):

Source/WTF:

Add a new toStringWithLatin1Fallback that simply uses
String::fromUTF8WithLatin1Fallback, so we can avoid the
standard String::fromUTF8 null return.

* wtf/StringPrintStream.cpp:
(WTF::StringPrintStream::toStringWithLatin1Fallback):
* wtf/StringPrintStream.h:

LayoutTests:

Add a testcase.

* js/invalid-utf8-in-syntax-error-expected.txt: Added.
* js/script-tests/invalid-utf8-in-syntax-error.js: Added.

Modified Paths

Added Paths

Diff

Modified: trunk/LayoutTests/ChangeLog (201623 => 201624)


--- trunk/LayoutTests/ChangeLog	2016-06-02 22:43:25 UTC (rev 201623)
+++ trunk/LayoutTests/ChangeLog	2016-06-02 23:07:48 UTC (rev 201624)
@@ -1,3 +1,15 @@
+2016-06-02  Oliver Hunt  <[email protected]>
+
+        JS parser incorrectly handles invalid utf8 in error messages.
+        https://bugs.webkit.org/show_bug.cgi?id=158128
+
+        Reviewed by Saam Barati.
+
+        Add a testcase.
+
+        * js/invalid-utf8-in-syntax-error-expected.txt: Added.
+        * js/script-tests/invalid-utf8-in-syntax-error.js: Added.
+
 2016-06-02  Michael Saboff  <[email protected]>
 
         REGRESSION(r200694): %ThrowTypeError% is not unique

Added: trunk/LayoutTests/js/invalid-utf8-in-syntax-error-expected.txt (0 => 201624)


--- trunk/LayoutTests/js/invalid-utf8-in-syntax-error-expected.txt	                        (rev 0)
+++ trunk/LayoutTests/js/invalid-utf8-in-syntax-error-expected.txt	2016-06-02 23:07:48 UTC (rev 201624)
@@ -0,0 +1,10 @@
+Ensures that we correctly propagate the error message for lexer errors containing invalid utf8 code sequences
+
+On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
+
+
+PASS ({f("\x{DEAD}")}) threw exception SyntaxError: Unexpected string literal "íº­". Expected a parameter pattern or a ')' in parameter list..
+PASS successfullyParsed is true
+
+TEST COMPLETE
+

Added: trunk/LayoutTests/js/script-tests/invalid-utf8-in-syntax-error.js (0 => 201624)


--- trunk/LayoutTests/js/script-tests/invalid-utf8-in-syntax-error.js	                        (rev 0)
+++ trunk/LayoutTests/js/script-tests/invalid-utf8-in-syntax-error.js	2016-06-02 23:07:48 UTC (rev 201624)
@@ -0,0 +1,6 @@
+description('Ensures that we correctly propagate the error message for lexer errors containing invalid utf8 code sequences');
+
+shouldThrow('({f("\udead")})');
+
+var successfullyParsed = true;
+

Modified: trunk/Source/_javascript_Core/ChangeLog (201623 => 201624)


--- trunk/Source/_javascript_Core/ChangeLog	2016-06-02 22:43:25 UTC (rev 201623)
+++ trunk/Source/_javascript_Core/ChangeLog	2016-06-02 23:07:48 UTC (rev 201624)
@@ -1,3 +1,30 @@
+2016-06-02  Oliver Hunt  <[email protected]>
+
+        JS parser incorrectly handles invalid utf8 in error messages.
+        https://bugs.webkit.org/show_bug.cgi?id=158128
+
+        Reviewed by Saam Barati.
+
+        The bug here was caused by us using PrintStream's toString method
+        to produce the error message for a parse error, even though toString
+        may produce a null string in the event of invalid utf8 that causes
+        the error in first case. So when we try to create an error message
+        containing the invalid character code, we set m_errorMessage to the
+        null string, as that signals "no error" we don't stop parsing, and
+        everything goes down hill from there.
+
+        Now we use the new toStringWithLatin1Fallback so that we can always
+        produce an error message, even if it contains invalid unicode. We
+        also add an additional fallback so that we can guarantee an error
+        message is set even if we're given a null string. There's a debug
+        mode assertion to prevent anyone accidentally attempting to clear
+        the message via setErrorMessage.
+
+        * parser/Parser.cpp:
+        (JSC::Parser<LexerType>::logError):
+        * parser/Parser.h:
+        (JSC::Parser::setErrorMessage):
+
 2016-06-02  Saam Barati  <[email protected]>
 
         Teach bytecode liveness about the debugger

Modified: trunk/Source/_javascript_Core/parser/Parser.cpp (201623 => 201624)


--- trunk/Source/_javascript_Core/parser/Parser.cpp	2016-06-02 22:43:25 UTC (rev 201623)
+++ trunk/Source/_javascript_Core/parser/Parser.cpp	2016-06-02 23:07:48 UTC (rev 201624)
@@ -93,7 +93,7 @@
         return;
     StringPrintStream stream;
     printUnexpectedTokenText(stream);
-    setErrorMessage(stream.toString());
+    setErrorMessage(stream.toStringWithLatin1Fallback());
 }
 
 template <typename LexerType> template <typename A>
@@ -107,7 +107,7 @@
         stream.print(". ");
     }
     stream.print(value1, ".");
-    setErrorMessage(stream.toString());
+    setErrorMessage(stream.toStringWithLatin1Fallback());
 }
 
 template <typename LexerType> template <typename A, typename B>
@@ -121,7 +121,7 @@
         stream.print(". ");
     }
     stream.print(value1, value2, ".");
-    setErrorMessage(stream.toString());
+    setErrorMessage(stream.toStringWithLatin1Fallback());
 }
 
 template <typename LexerType> template <typename A, typename B, typename C>
@@ -135,7 +135,7 @@
         stream.print(". ");
     }
     stream.print(value1, value2, value3, ".");
-    setErrorMessage(stream.toString());
+    setErrorMessage(stream.toStringWithLatin1Fallback());
 }
 
 template <typename LexerType> template <typename A, typename B, typename C, typename D>
@@ -149,7 +149,7 @@
         stream.print(". ");
     }
     stream.print(value1, value2, value3, value4, ".");
-    setErrorMessage(stream.toString());
+    setErrorMessage(stream.toStringWithLatin1Fallback());
 }
 
 template <typename LexerType> template <typename A, typename B, typename C, typename D, typename E>
@@ -163,7 +163,7 @@
         stream.print(". ");
     }
     stream.print(value1, value2, value3, value4, value5, ".");
-    setErrorMessage(stream.toString());
+    setErrorMessage(stream.toStringWithLatin1Fallback());
 }
 
 template <typename LexerType> template <typename A, typename B, typename C, typename D, typename E, typename F>
@@ -177,7 +177,7 @@
         stream.print(". ");
     }
     stream.print(value1, value2, value3, value4, value5, value6, ".");
-    setErrorMessage(stream.toString());
+    setErrorMessage(stream.toStringWithLatin1Fallback());
 }
 
 template <typename LexerType> template <typename A, typename B, typename C, typename D, typename E, typename F, typename G>
@@ -191,7 +191,7 @@
         stream.print(". ");
     }
     stream.print(value1, value2, value3, value4, value5, value6, value7, ".");
-    setErrorMessage(stream.toString());
+    setErrorMessage(stream.toStringWithLatin1Fallback());
 }
 
 template <typename LexerType>

Modified: trunk/Source/_javascript_Core/parser/Parser.h (201623 => 201624)


--- trunk/Source/_javascript_Core/parser/Parser.h	2016-06-02 22:43:25 UTC (rev 201623)
+++ trunk/Source/_javascript_Core/parser/Parser.h	2016-06-02 23:07:48 UTC (rev 201624)
@@ -1276,7 +1276,10 @@
 
     void setErrorMessage(const String& message)
     {
+        ASSERT_WITH_MESSAGE(!message.isEmpty(), "Attempted to set the empty string as an error message. Likely caused by invalid UTF8 used when creating the message.");
         m_errorMessage = message;
+        if (m_errorMessage.isEmpty())
+            m_errorMessage = ASCIILiteral("Unparseable script");
     }
     
     NEVER_INLINE void logError(bool);

Modified: trunk/Source/WTF/ChangeLog (201623 => 201624)


--- trunk/Source/WTF/ChangeLog	2016-06-02 22:43:25 UTC (rev 201623)
+++ trunk/Source/WTF/ChangeLog	2016-06-02 23:07:48 UTC (rev 201624)
@@ -1,3 +1,18 @@
+2016-06-02  Oliver Hunt  <[email protected]>
+
+        JS parser incorrectly handles invalid utf8 in error messages.
+        https://bugs.webkit.org/show_bug.cgi?id=158128
+
+        Reviewed by Saam Barati.
+
+        Add a new toStringWithLatin1Fallback that simply uses
+        String::fromUTF8WithLatin1Fallback, so we can avoid the
+        standard String::fromUTF8 null return.
+
+        * wtf/StringPrintStream.cpp:
+        (WTF::StringPrintStream::toStringWithLatin1Fallback):
+        * wtf/StringPrintStream.h:
+
 2016-06-02  Keith Miller  <[email protected]>
 
         Unreviewed, reland r201532. The associated regressions have been fixed

Modified: trunk/Source/WTF/wtf/StringPrintStream.cpp (201623 => 201624)


--- trunk/Source/WTF/wtf/StringPrintStream.cpp	2016-06-02 22:43:25 UTC (rev 201623)
+++ trunk/Source/WTF/wtf/StringPrintStream.cpp	2016-06-02 23:07:48 UTC (rev 201624)
@@ -100,6 +100,12 @@
     return String::fromUTF8(m_buffer, m_next);
 }
 
+String StringPrintStream::toStringWithLatin1Fallback()
+{
+    ASSERT(m_next == strlen(m_buffer));
+    return String::fromUTF8WithLatin1Fallback(m_buffer, m_next);
+}
+
 void StringPrintStream::increaseSize(size_t newSize)
 {
     ASSERT_WITH_SECURITY_IMPLICATION(newSize > m_size);

Modified: trunk/Source/WTF/wtf/StringPrintStream.h (201623 => 201624)


--- trunk/Source/WTF/wtf/StringPrintStream.h	2016-06-02 22:43:25 UTC (rev 201623)
+++ trunk/Source/WTF/wtf/StringPrintStream.h	2016-06-02 23:07:48 UTC (rev 201624)
@@ -43,6 +43,7 @@
     
     WTF_EXPORT_PRIVATE CString toCString();
     WTF_EXPORT_PRIVATE String toString();
+    WTF_EXPORT_PRIVATE String toStringWithLatin1Fallback();
     WTF_EXPORT_PRIVATE void reset();
     
 private:
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to