diff --git a/ruby.c b/ruby.c index 109e1734fa..8f4659db01 100644 --- a/ruby.c +++ b/ruby.c @@ -2616,12 +2616,18 @@ external_str_new_cstr(const char *p) #endif } +static void +set_progname(VALUE name) +{ + rb_orig_progname = rb_progname = name; + rb_vm_set_progname(rb_progname); +} + void ruby_script(const char *name) { if (name) { - rb_orig_progname = rb_progname = external_str_new_cstr(name); - rb_vm_set_progname(rb_progname); + set_progname(rb_str_freeze(external_str_new_cstr(name))); } } @@ -2632,8 +2638,7 @@ ruby_script(const char *name) void ruby_set_script_name(VALUE name) { - rb_orig_progname = rb_progname = rb_str_dup(name); - rb_vm_set_progname(rb_progname); + set_progname(rb_str_new_frozen(name)); } static void @@ -2757,7 +2762,7 @@ ruby_process_options(int argc, char **argv) origarg.argc = argc; origarg.argv = argv; } - ruby_script(script_name); /* for the time being */ + set_progname(external_str_new_cstr(script_name)); /* for the time being */ rb_argv0 = rb_str_new4(rb_progname); rb_gc_register_mark_object(rb_argv0); iseq = process_options(argc, argv, cmdline_options_init(&opt)); diff --git a/spec/ruby/core/process/argv0_spec.rb b/spec/ruby/core/process/argv0_spec.rb index 0d02248b6d..7c2342f959 100644 --- a/spec/ruby/core/process/argv0_spec.rb +++ b/spec/ruby/core/process/argv0_spec.rb @@ -13,8 +13,10 @@ describe "Process.argv0" do end end - it "returns a non frozen object" do - Process.argv0.should_not.frozen? + ruby_bug "#19597", ""..."3.3" do + it "returns a frozen object" do + Process.argv0.should.frozen? + end end it "returns every time the same object" do diff --git a/test/ruby/test_process.rb b/test/ruby/test_process.rb index a259764041..66624644e9 100644 --- a/test/ruby/test_process.rb +++ b/test/ruby/test_process.rb @@ -1424,6 +1424,11 @@ class TestProcess < Test::Unit::TestCase REPRO end + def test_argv0_frozen + assert_predicate Process.argv0, :frozen? + assert_predicate $0, :frozen? + end + def test_status with_tmpchdir do s = run_in_child("exit 1") diff --git a/vm.c b/vm.c index 20cd8edd7c..b6b8ca80f5 100644 --- a/vm.c +++ b/vm.c @@ -3979,7 +3979,8 @@ rb_vm_set_progname(VALUE filename) rb_control_frame_t *cfp = (void *)(th->ec->vm_stack + th->ec->vm_stack_size); --cfp; - rb_iseq_pathobj_set(cfp->iseq, rb_str_dup(filename), rb_iseq_realpath(cfp->iseq)); + filename = rb_str_new_frozen(filename); + rb_iseq_pathobj_set(cfp->iseq, filename, rb_iseq_realpath(cfp->iseq)); } extern const struct st_hash_type rb_fstring_hash_type;