Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug #19597] Freeze script name #7709

Merged
merged 1 commit into from
May 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 10 additions & 5 deletions ruby.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}
}

Expand All @@ -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
Expand Down Expand Up @@ -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));
Expand Down
6 changes: 4 additions & 2 deletions spec/ruby/core/process/argv0_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
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
Expand Down
5 changes: 5 additions & 0 deletions test/ruby/test_process.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1424,6 +1424,11 @@ def test_argv0_keep_alive
REPRO
end

def test_argv0_frozen
assert_predicate Process.argv0, :frozen?
assert_predicate $0, :frozen?
end
Comment on lines +1427 to +1430
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably doesn't matter much, but that's technically a behavior change, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, bug fixes change behaviors.


def test_status
with_tmpchdir do
s = run_in_child("exit 1")
Expand Down
3 changes: 2 additions & 1 deletion vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down