Skip to content

Guard string VALUEs while parsing their C string - #556

Merged
tompng merged 1 commit into
ruby:masterfrom
tompng:gc_guard_string
Sep 12, 2026
Merged

tompng merged 1 commit into
ruby:masterfrom
tompng:gc_guard_string

Conversation

@tompng

@tompng tompng commented Sep 12, 2026

Copy link
Copy Markdown
Member

StringValueCStr may replace the local VALUE with the result of to_str, leaving the converted String referenced only from a C local. VpAlloc allocates a temporary buffer with rb_str_tmp_new before it scans the C string, so a GC triggered there could free the String while its contents are still being read.

This adds RB_GC_GUARD after the last use of the pointer in the three places that parse a String argument:

  • rb_str_convert_to_BigDecimal (BigDecimal(obj) with a to_str object)
  • BigDecimal_s_interpret_loosely (String#to_d)
  • BigDecimal_load

With the current compiler output the VALUE happens to stay in a stack slot that the conservative GC scans, so this is not reproducible today, but it depends on register/stack allocation choices rather than on anything guaranteed.

🤖 Generated with Claude Code

StringValueCStr may replace the local VALUE with the result of to_str,
leaving the converted String referenced only from a C local. VpAlloc
allocates a temporary buffer with rb_str_tmp_new before it scans the
C string, so a GC triggered there could free the String while its
contents are still being read. Add RB_GC_GUARD after the last use of
the pointer in the three places that parse a String argument.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@tompng
tompng merged commit 7471291 into ruby:master Sep 12, 2026
83 checks passed
@tompng
tompng deleted the gc_guard_string branch September 12, 2026 14:22
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.

1 participant