ruby-changes:58704
From: Nobuyoshi <ko1@a...>
Date: Mon, 11 Nov 2019 11:58:19 +0900 (JST)
Subject: [ruby-changes:58704] 69ec3f70fa (master): Warn EOF char in comment
https://git.ruby-lang.org/ruby.git/commit/?id=69ec3f70fa From 69ec3f70fab0c1c537c68fb135cc315181b1d750 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada <nobu@r...> Date: Mon, 11 Nov 2019 09:59:40 +0900 Subject: Warn EOF char in comment diff --git a/parse.y b/parse.y index e84468d..039abc4 100644 --- a/parse.y +++ b/parse.y @@ -6191,7 +6191,30 @@ parser_str_new(const char *ptr, long len, rb_encoding *enc, int func, rb_encodin https://github.com/ruby/ruby/blob/trunk/parse.y#L6191 return str; } -#define lex_goto_eol(p) ((p)->lex.pcur = (p)->lex.pend) +static const char * +eof_char(int c) +{ + switch (c) { + case '\0': return "\\0"; + case '\004': return "^D"; + case '\032': return "^Z"; + } + return 0; +} + +static void +lex_goto_eol(struct parser_params *p) +{ + const char *pcur = p->lex.pcur, *pend = p->lex.pend; + for (; pcur < pend; pcur++) { + const char *eof = eof_char(*pcur); + if (eof) { + rb_warning1("encountered %s in comment, just ignored in this version", WARN_S(eof)); + break; + } + } + p->lex.pcur = pend; /* pcur */ +} #define lex_eol_p(p) ((p)->lex.pcur >= (p)->lex.pend) #define lex_eol_n_p(p,n) ((p)->lex.pcur+(n) >= (p)->lex.pend) #define peek(p,c) peek_n(p, (c), 0) @@ -7398,9 +7421,7 @@ word_match_p(struct parser_params *p, const char *word, long len) https://github.com/ruby/ruby/blob/trunk/parse.y#L7421 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; - } + if (eof_char(c)) return 1; return 0; } diff --git a/test/ruby/test_parse.rb b/test/ruby/test_parse.rb index 2622ec5..17a0c8a 100644 --- a/test/ruby/test_parse.rb +++ b/test/ruby/test_parse.rb @@ -720,6 +720,15 @@ x = __ENCODING__ https://github.com/ruby/ruby/blob/trunk/test/ruby/test_parse.rb#L720 assert_syntax_error("=begin", error) end + def test_embedded_rd_warning + [["\0", "\\0"], ["\C-d", "^D"], ["\C-z", "^Z"]].each do |eof, mesg| + mesg = /encountered #{Regexp.quote(mesg)}/ + assert_warning(mesg) {eval("=begin\n#{eof}\n=end")} + assert_warning(mesg) {eval("=begin#{eof}\n=end")} + assert_warning(mesg) {eval("=begin\n=end#{eof}\n")} + end + end + def test_float assert_equal(1.0/0, eval("1e10000")) assert_syntax_error('1_E', /trailing `_'/) diff --git a/test/ruby/test_syntax.rb b/test/ruby/test_syntax.rb index 5fb4dfa..d0748e7 100644 --- a/test/ruby/test_syntax.rb +++ b/test/ruby/test_syntax.rb @@ -943,6 +943,12 @@ eom https://github.com/ruby/ruby/blob/trunk/test/ruby/test_syntax.rb#L943 end end + def test_warning_for_eof_in_comment + assert_warning(/encountered \\0/) {eval("#\0")} + assert_warning(/encountered \^D/) {eval("#\C-d")} + assert_warning(/encountered \^Z/) {eval("#\C-z")} + end + def test_unexpected_fraction msg = /unexpected fraction/ assert_syntax_error("0x0.0", msg) -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/