Skip to content

string.c: guard tmp in rb_str_format_m#16280

Merged
XrXr merged 2 commits intoruby:masterfrom
dummyx:fix-rb_str_format_m-tmp-guard
Mar 2, 2026
Merged

string.c: guard tmp in rb_str_format_m#16280
XrXr merged 2 commits intoruby:masterfrom
dummyx:fix-rb_str_format_m-tmp-guard

Conversation

@dummyx
Copy link
Copy Markdown
Contributor

@dummyx dummyx commented Mar 2, 2026

The tmp in rb_str_format_m should be guarded by RB_GC_GUARD.

This script reproduces the crash:

GC.stress = true

class Args
  def to_ary
    Array.new(64) { "x" * 1024 }
  end
end

fmt = "%s" * 64
100_000.times do
  fmt % Args.new
end

segfault.txt

rb_str_format_m passes RARRAY_CONST_PTR(tmp) to rb_str_format. Without an explicit GC guard, compiler optimization can make tmp appear dead too early while the derived pointer is still in use in rb_str_format.

This PR adds the RB_GC_GUARD call.

dummyx and others added 2 commits March 2, 2026 12:38
Keep tmp alive while RARRAY_CONST_PTR(tmp) is used by rb_str_format.
it's intended to be used to extend the liverange of a VALUE
@XrXr XrXr enabled auto-merge (squash) March 2, 2026 17:16
@XrXr XrXr merged commit 726205b into ruby:master Mar 2, 2026
91 checks passed
byroot pushed a commit to byroot/ruby that referenced this pull request Mar 3, 2026
[Bug #21931]

Keep tmp alive while RARRAY_CONST_PTR(tmp) is used by rb_str_format.

[alan: sunk the guard below usage]
Reviewed-by: Alan Wu <XrXr@users.noreply.github.com>
byroot pushed a commit to byroot/ruby that referenced this pull request Mar 3, 2026
[Bug #21931]

Keep tmp alive while RARRAY_CONST_PTR(tmp) is used by rb_str_format.

[alan: sunk the guard below usage]
Reviewed-by: Alan Wu <XrXr@users.noreply.github.com>
byroot pushed a commit to byroot/ruby that referenced this pull request Mar 3, 2026
[Bug #21931]

Keep tmp alive while RARRAY_CONST_PTR(tmp) is used by rb_str_format.

[alan: sunk the guard below usage]
Reviewed-by: Alan Wu <XrXr@users.noreply.github.com>
k0kubun pushed a commit that referenced this pull request Mar 3, 2026
[Bug #21931]

Keep tmp alive while RARRAY_CONST_PTR(tmp) is used by rb_str_format.

[alan: sunk the guard below usage]
Reviewed-by: Alan Wu <XrXr@users.noreply.github.com>
hsbt pushed a commit that referenced this pull request Mar 4, 2026
[Bug #21931]

Keep tmp alive while RARRAY_CONST_PTR(tmp) is used by rb_str_format.

[alan: sunk the guard below usage]
Reviewed-by: Alan Wu <XrXr@users.noreply.github.com>
luke-gru pushed a commit that referenced this pull request Mar 6, 2026
[Bug #21931]

Keep tmp alive while RARRAY_CONST_PTR(tmp) is used by rb_str_format.

[alan: sunk the guard below usage]
Reviewed-by: Alan Wu <XrXr@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants