[前][次][番号順一覧][スレッド一覧]

ruby-changes:58703

From: Nobuyoshi <ko1@a...>
Date: Mon, 11 Nov 2019 09:49:23 +0900 (JST)
Subject: [ruby-changes:58703] ade0388894 (master): Fixed embedded document with EOF char

https://git.ruby-lang.org/ruby.git/commit/?id=ade0388894

From ade038889468e7755d7ebfe75975e0e77d1e1dec Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <nobu@r...>
Date: Mon, 11 Nov 2019 09:35:37 +0900
Subject: Fixed embedded document with EOF char


diff --git a/parse.y b/parse.y
index e234392..e84468d 100644
--- a/parse.y
+++ b/parse.y
@@ -7391,6 +7391,19 @@ whole_match_p(struct parser_params *p, const char *eos, long len, int indent) https://github.com/ruby/ruby/blob/trunk/parse.y#L7391
     return strncmp(eos, ptr, len) == 0;
 }
 
+static int
+word_match_p(struct parser_params *p, const char *word, long len)
+{
+    if (strncmp(p->lex.pcur, word, len)) return 0;
+    if (p->lex.pcur + len == p->lex.pend) return 1;
+    int c = (unsigned char)p->lex.pcur[len];
+    if (ISSPACE(c)) return 1;
+    switch (c) {
+      case '\0': case '\004': case '\032': return 1;
+    }
+    return 0;
+}
+
 #define NUM_SUFFIX_R   (1<<0)
 #define NUM_SUFFIX_I   (1<<1)
 #define NUM_SUFFIX_ALL 3
@@ -8976,7 +8989,7 @@ parser_yylex(struct parser_params *p) https://github.com/ruby/ruby/blob/trunk/parse.y#L8989
       case '=':
 	if (was_bol(p)) {
 	    /* skip embedded rd document */
-	    if (strncmp(p->lex.pcur, "begin", 5) == 0 && ISSPACE(p->lex.pcur[5])) {
+	    if (word_match_p(p, "begin", 5)) {
 		int first_p = TRUE;
 
 		lex_goto_eol(p);
@@ -8992,11 +9005,10 @@ parser_yylex(struct parser_params *p) https://github.com/ruby/ruby/blob/trunk/parse.y#L9005
 			compile_error(p, "embedded document meets end of file");
 			return 0;
 		    }
-		    if (c != '=') continue;
-		    if (c == '=' && strncmp(p->lex.pcur, "end", 3) == 0 &&
-			(p->lex.pcur + 3 == p->lex.pend || ISSPACE(p->lex.pcur[3]))) {
+		    if (c == '=' && word_match_p(p, "end", 3)) {
 			break;
 		    }
+		    pushback(p, c);
 		}
 		lex_goto_eol(p);
 		dispatch_scan_event(p, tEMBDOC_END);
diff --git a/test/ruby/test_parse.rb b/test/ruby/test_parse.rb
index ee5f815..2622ec5 100644
--- a/test/ruby/test_parse.rb
+++ b/test/ruby/test_parse.rb
@@ -709,6 +709,15 @@ x = __ENCODING__ https://github.com/ruby/ruby/blob/trunk/test/ruby/test_parse.rb#L709
 
   def test_embedded_rd
     assert_valid_syntax("=begin\n""=end")
+    assert_valid_syntax("=begin\n""=end\0")
+    assert_valid_syntax("=begin\n""=end\C-d")
+    assert_valid_syntax("=begin\n""=end\C-z")
+  end
+
+  def test_embedded_rd_error
+    error = 'embedded document meets end of file'
+    assert_syntax_error("=begin\n", error)
+    assert_syntax_error("=begin", error)
   end
 
   def test_float
-- 
cgit v0.10.2


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]