Fix error messages for incorrect hex literals
authoroliver@apple.com <oliver@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 17 Jun 2014 22:29:56 +0000 (22:29 +0000)
committeroliver@apple.com <oliver@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 17 Jun 2014 22:29:56 +0000 (22:29 +0000)
https://bugs.webkit.org/show_bug.cgi?id=133998

Reviewed by Mark Lam.

Source/JavaScriptCore:
Ensure that the error messages for bogus hex literals actually
make sense.

* parser/Lexer.cpp:
(JSC::Lexer<T>::lex):
* parser/ParserTokens.h:

LayoutTests:
Update tests for sane error messages.

* sputnik/Conformance/07_Lexical_Conventions/7.8_Literals/7.8.3_Numeric_Literals/S7.8.3_A6.1_T1-expected.txt:
* sputnik/Conformance/07_Lexical_Conventions/7.8_Literals/7.8.3_Numeric_Literals/S7.8.3_A6.1_T2-expected.txt:
* sputnik/Conformance/07_Lexical_Conventions/7.8_Literals/7.8.3_Numeric_Literals/S7.8.3_A6.2_T1-expected.txt:
* sputnik/Conformance/07_Lexical_Conventions/7.8_Literals/7.8.3_Numeric_Literals/S7.8.3_A6.2_T2-expected.txt:

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@170079 268f45cc-cd09-0410-ab3c-d52691b4dbfc

LayoutTests/ChangeLog
LayoutTests/js/parser-error-messages-expected.txt [new file with mode: 0644]
LayoutTests/js/parser-error-messages.html [new file with mode: 0644]
LayoutTests/js/script-tests/parser-error-messages.js [new file with mode: 0644]
LayoutTests/sputnik/Conformance/07_Lexical_Conventions/7.8_Literals/7.8.3_Numeric_Literals/S7.8.3_A6.1_T1-expected.txt
LayoutTests/sputnik/Conformance/07_Lexical_Conventions/7.8_Literals/7.8.3_Numeric_Literals/S7.8.3_A6.1_T2-expected.txt
LayoutTests/sputnik/Conformance/07_Lexical_Conventions/7.8_Literals/7.8.3_Numeric_Literals/S7.8.3_A6.2_T1-expected.txt
LayoutTests/sputnik/Conformance/07_Lexical_Conventions/7.8_Literals/7.8.3_Numeric_Literals/S7.8.3_A6.2_T2-expected.txt
Source/JavaScriptCore/ChangeLog
Source/JavaScriptCore/parser/Lexer.cpp
Source/JavaScriptCore/parser/ParserTokens.h

index e9e3013..0bad5c7 100644 (file)
@@ -1,3 +1,17 @@
+2014-06-17  Oliver Hunt  <oliver@apple.com>
+
+        Fix error messages for incorrect hex literals
+        https://bugs.webkit.org/show_bug.cgi?id=133998
+
+        Reviewed by Mark Lam.
+
+        Update tests for sane error messages.
+
+        * sputnik/Conformance/07_Lexical_Conventions/7.8_Literals/7.8.3_Numeric_Literals/S7.8.3_A6.1_T1-expected.txt:
+        * sputnik/Conformance/07_Lexical_Conventions/7.8_Literals/7.8.3_Numeric_Literals/S7.8.3_A6.1_T2-expected.txt:
+        * sputnik/Conformance/07_Lexical_Conventions/7.8_Literals/7.8.3_Numeric_Literals/S7.8.3_A6.2_T1-expected.txt:
+        * sputnik/Conformance/07_Lexical_Conventions/7.8_Literals/7.8.3_Numeric_Literals/S7.8.3_A6.2_T2-expected.txt:
+
 2014-06-17  Carlos Alberto Lopez Perez  <clopez@igalia.com>
 
         [GTK] Fix the handling of resize events after r169505.
diff --git a/LayoutTests/js/parser-error-messages-expected.txt b/LayoutTests/js/parser-error-messages-expected.txt
new file mode 100644 (file)
index 0000000..f405ed4
--- /dev/null
@@ -0,0 +1,14 @@
+Tests error messages to make sure that they're sane
+
+On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
+
+
+PASS parseTest('0x') is "No hexadecimal digits after '0x'"
+PASS parseTest('0xg') is "No hexadecimal digits after '0x'"
+PASS parseTest('0x1.2') is "Unexpected number '.2'. Parse error."
+PASS parseTest('0x1g') is "No space between hexadecimal literal and identifier"
+PASS parseTest('0x1in') is "No space between hexadecimal literal and identifier"
+PASS successfullyParsed is true
+
+TEST COMPLETE
+
diff --git a/LayoutTests/js/parser-error-messages.html b/LayoutTests/js/parser-error-messages.html
new file mode 100644 (file)
index 0000000..5491d46
--- /dev/null
@@ -0,0 +1,10 @@
+<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
+<html>
+<head>
+<script src="../resources/js-test-pre.js"></script>
+</head>
+<body>
+<script src="script-tests/parser-error-messages.js"></script>
+<script src="../resources/js-test-post.js"></script>
+</body>
+</html>
diff --git a/LayoutTests/js/script-tests/parser-error-messages.js b/LayoutTests/js/script-tests/parser-error-messages.js
new file mode 100644 (file)
index 0000000..dce2571
--- /dev/null
@@ -0,0 +1,17 @@
+description("Tests error messages to make sure that they're sane");
+
+function parseTest(source)
+{
+    try {
+        eval(source);
+    } catch (e) {
+        return e.message
+    }
+}
+
+shouldBe("parseTest('0x')", "\"No hexadecimal digits after '0x'\"");
+shouldBe("parseTest('0xg')", "\"No hexadecimal digits after '0x'\"");
+shouldBe("parseTest('0x1.2')", "\"Unexpected number '.2'. Parse error.\"");
+shouldBe("parseTest('0x1g')", "\"No space between hexadecimal literal and identifier\"");
+shouldBe("parseTest('0x1in')", "\"No space between hexadecimal literal and identifier\"");
+
index d9734bc..4a551d2 100644 (file)
@@ -1,3 +1,17 @@
+2014-06-17  Oliver Hunt  <oliver@apple.com>
+
+        Fix error messages for incorrect hex literals
+        https://bugs.webkit.org/show_bug.cgi?id=133998
+
+        Reviewed by Mark Lam.
+
+        Ensure that the error messages for bogus hex literals actually
+        make sense.
+
+        * parser/Lexer.cpp:
+        (JSC::Lexer<T>::lex):
+        * parser/ParserTokens.h:
+
 2014-06-17  Matthew Mirman  <mmirman@apple.com>
 
         Fixes bug where building JSC sometimes crashes at build-symbol-table-index.py. Also adds licenses. 
index c7fc50c..9ca7610 100644 (file)
@@ -1646,20 +1646,32 @@ start:
         goto inNumberAfterDecimalPoint;
     case CharacterZero:
         shift();
-        if ((m_current | 0x20) == 'x' && isASCIIHexDigit(peek(1))) {
+        if ((m_current | 0x20) == 'x') {
+            if (!isASCIIHexDigit(peek(1))) {
+                m_lexErrorMessage = "No hexadecimal digits after '0x'";
+                token = INVALID_HEX_NUMBER_ERRORTOK;
+                goto returnError;
+            }
             parseHex(tokenData->doubleValue);
+            if (isIdentStart(m_current)) {
+                m_lexErrorMessage = "No space between hexadecimal literal and identifier";
+                token = INVALID_HEX_NUMBER_ERRORTOK;
+                goto returnError;
+            }
             token = NUMBER;
-        } else {
-            record8('0');
-            if (isASCIIOctalDigit(m_current)) {
-                if (parseOctal(tokenData->doubleValue)) {
-                    if (strictMode) {
-                        m_lexErrorMessage = "Octal escapes are forbidden in strict mode";
-                        token = INVALID_OCTAL_NUMBER_ERRORTOK;
-                        goto returnError;
-                    }
-                    token = NUMBER;
+            m_buffer8.resize(0);
+            break;
+        }
+
+        record8('0');
+        if (isASCIIOctalDigit(m_current)) {
+            if (parseOctal(tokenData->doubleValue)) {
+                if (strictMode) {
+                    m_lexErrorMessage = "Octal escapes are forbidden in strict mode";
+                    token = INVALID_OCTAL_NUMBER_ERRORTOK;
+                    goto returnError;
                 }
+                token = NUMBER;
             }
         }
         FALLTHROUGH;
index 8c87ad9..a6758d0 100644 (file)
@@ -149,7 +149,8 @@ enum JSTokenType {
     INVALID_NUMERIC_LITERAL_ERRORTOK = 7 | ErrorTokenFlag,
     UNTERMINATED_STRING_LITERAL_ERRORTOK = 8 | ErrorTokenFlag | UnterminatedErrorTokenFlag,
     INVALID_STRING_LITERAL_ERRORTOK = 9 | ErrorTokenFlag,
-    INVALID_PRIVATE_NAME_ERRORTOK = 10 | ErrorTokenFlag
+    INVALID_PRIVATE_NAME_ERRORTOK = 10 | ErrorTokenFlag,
+    INVALID_HEX_NUMBER_ERRORTOK = 11 | ErrorTokenFlag
 };
 
 struct JSTextPosition {