1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Fix dangling path name from fstring

* parse.y (yycompile): make sure in advance that the `__FILE__`
  object shares a fstring, to get rid of dangling path name.
  Fixed up 53e9908d8a.  [Bug #16041]

* vm_eval.c (eval_make_iseq): ditto.
This commit is contained in:
Nobuyoshi Nakada 2019-08-03 13:41:55 +09:00
parent 688a59f8ac
commit 5931857281
No known key found for this signature in database
GPG key ID: 4BC7D6DF58D8DF60
3 changed files with 15 additions and 3 deletions

View file

@ -5812,7 +5812,7 @@ yycompile(VALUE vparser, struct parser_params *p, VALUE fname, int line)
p->ruby_sourcefile = "(none)";
}
else {
p->ruby_sourcefile_string = rb_str_new_frozen(fname);
p->ruby_sourcefile_string = rb_fstring(fname);
p->ruby_sourcefile = StringValueCStr(fname);
}
p->ruby_sourceline = line - 1;
@ -9773,7 +9773,7 @@ gettable(struct parser_params *p, ID id, const YYLTYPE *loc)
if (NIL_P(file))
file = rb_str_new(0, 0);
else
file = rb_str_dup(rb_fstring(file));
file = rb_str_dup(file);
node = NEW_STR(add_mark_object(p, file), loc);
}
return node;

View file

@ -498,6 +498,17 @@ class TestEval < Test::Unit::TestCase
}, '[Bug #10368]'
end
def test_gced_eval_location
Dir.mktmpdir do |d|
File.write("#{d}/2.rb", "")
File.write("#{d}/1.rb", "require_relative '2'\n""__FILE__\n")
file = "1.rb"
path = File.expand_path(file, d)
assert_equal(path, eval(File.read(path), nil, File.expand_path(file, d)))
assert_equal(path, eval(File.read(path), nil, File.expand_path(file, d)))
end
end
def orphan_proc
proc {eval("return :ng")}
end

View file

@ -1284,6 +1284,7 @@ eval_make_iseq(VALUE src, VALUE fname, int line, const rb_binding_t *bind,
}
if (fname != Qundef) {
if (!NIL_P(fname)) fname = rb_fstring(fname);
realpath = fname;
}
else if (bind) {
@ -1293,7 +1294,7 @@ eval_make_iseq(VALUE src, VALUE fname, int line, const rb_binding_t *bind,
rb_parser_warn_location(parser, TRUE);
}
else {
fname = rb_usascii_str_new_cstr("(eval)");
fname = rb_fstring_lit("(eval)");
}
rb_parser_set_context(parser, base_block, FALSE);