瀏覽代碼

Fix Issue #3261: Need to detect invalid null character at the end of the source string

Curtis Man 8 年之前
父節點
當前提交
2a32090679

+ 1 - 1
bin/ch/WScriptJsrt.cpp

@@ -945,7 +945,7 @@ bool WScriptJsrt::Initialize()
             ;
 
         JsValueRef $262ScriptRef;
-        IfJsrtErrorFailLogAndRetFalse(ChakraRTInterface::JsCreateStringUtf16((uint16_t*)$262, _countof($262), &$262ScriptRef));
+        IfJsrtErrorFailLogAndRetFalse(ChakraRTInterface::JsCreateStringUtf16((uint16_t*)$262, _countof($262) - 1, &$262ScriptRef));
 
         JsValueRef fname;
         IfJsrtErrorFailLogAndRetFalse(ChakraRTInterface::JsCreateString("$262", strlen("$262"), &fname));

+ 9 - 6
lib/Parser/Scan.cpp

@@ -1147,7 +1147,7 @@ LEcmaLineBreak:
             goto LMainDefault;
 
         case kchNUL:
-            if (p >= last)
+            if (p > last)
             {
                 m_currentCharacter = p - 1;
                 Error(ERRnoStrEnd);
@@ -1723,14 +1723,14 @@ LLoop:
         case '\0':
             // Put back the null in case we get called again.
             p--;
-LEof:
-            token = tkEOF;
-
-            if (p + 1 < last)
+            if (p < last)
             {
                 // A \0 prior to the end of the text is an invalid character.
                 Error(ERRillegalChar);
             }
+LEof:
+            Assert(p >= last);
+            token = tkEOF;
             break;
 
         case 0x0009:
@@ -2031,7 +2031,10 @@ LCommentLineBreak:
                         m_parser->ReduceDeferredScriptLength((ULONG)(p - m_pchMinTok));
                         break;
                     case kchNUL:
-                        if (p >= last)
+                        // Because we used ReadFirst, we have advanced p. The character that we are looking at is actually is p - 1.
+                        // If p == last, we are looking at p - 1, it is still within the source buffer, and we need to consider it part of the comment
+                        // Only if p > last that we have pass the source buffer and consider it a line break
+                        if (p > last)
                         {
                             p--;
                             goto LCommentLineBreak;

+ 1 - 0
test/GlobalFunctions/evalNullsNewlines.baseline

@@ -5,3 +5,4 @@
 SyntaxError: Invalid character
 1
 1
+SyntaxError: Invalid character

+ 8 - 7
test/GlobalFunctions/evalNullsNewlines.js

@@ -14,10 +14,11 @@ function write(str) {
     }
 }
 
-write("--- 1 ---");                                                                   // CHROME          IE8
-try { write(eval('1+//\0\n1')); } catch (e) { write(e); }                             // 2               !
-try { write(eval('"a\0b"').length); } catch (e) { write(e); }                         // 3               !
-try { write(eval('\'a\0b\'').length); } catch (e) { write(e); }                       // 3               !
-try { write(eval('\0 = 1')); } catch (e) { write(e); }                                // !               undefined
-try { write(eval('/*\0*/1')); } catch (e) { write(e); }                               // 1               !
-try { write(eval('1//\0')); } catch (e) { write(e); }                                 // 1               1
+write("--- 1 ---");                                                               
+try { write(eval('1+//\0\n1')); } catch (e) { write(e); }                             // 2      
+try { write(eval('"a\0b"').length); } catch (e) { write(e); }                         // 3       
+try { write(eval('\'a\0b\'').length); } catch (e) { write(e); }                       // 3         
+try { write(eval('\0 = 1')); } catch (e) { write(e); }                                // !          
+try { write(eval('/*\0*/1')); } catch (e) { write(e); }                               // 1              
+try { write(eval('1//\0')); } catch (e) { write(e); }                                 // 1               
+try { write(eval('1\0')); } catch (e) { write(e); }                                 // !