Skip to content

Commit ebef4db

Browse files
eregonmatzbot
authored andcommitted
[ruby/prism] RB_GC_GUARD() the VALUE for RSTRING_PTR() to ensure the char* does not move
* See ruby/prism#3886 (comment). ruby/prism@98603ce8cc
1 parent 4bf1cb0 commit ebef4db

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

prism/extension.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,12 +201,16 @@ build_options_i(VALUE key, VALUE value, VALUE argument) {
201201
const char *version = check_string(value);
202202

203203
if (RSTRING_LEN(value) == 7 && strncmp(version, "current", 7) == 0) {
204-
const char *ruby_version = RSTRING_PTR(rb_const_get(rb_cObject, rb_intern("RUBY_VERSION")));
204+
VALUE ruby_version_string = rb_const_get(rb_cObject, rb_intern("RUBY_VERSION"));
205+
const char *ruby_version = RSTRING_PTR(ruby_version_string);
205206
if (!pm_options_version_set(options, ruby_version, 3)) {
206-
rb_exc_raise(rb_exc_new_cstr(rb_cPrismCurrentVersionError, ruby_version));
207+
rb_exc_raise(rb_exc_new_str(rb_cPrismCurrentVersionError, ruby_version_string));
207208
}
209+
210+
RB_GC_GUARD(ruby_version_string); // Do not move ruby_version while running the code above
208211
} else if (RSTRING_LEN(value) == 7 && strncmp(version, "nearest", 7) == 0) {
209-
const char *ruby_version = RSTRING_PTR(rb_const_get(rb_cObject, rb_intern("RUBY_VERSION")));
212+
VALUE ruby_version_string = rb_const_get(rb_cObject, rb_intern("RUBY_VERSION"));
213+
const char *ruby_version = RSTRING_PTR(ruby_version_string);
210214
const char *nearest_version;
211215

212216
if (ruby_version[0] < '3' || (ruby_version[0] == '3' && ruby_version[2] < '3')) {
@@ -220,6 +224,8 @@ build_options_i(VALUE key, VALUE value, VALUE argument) {
220224
if (!pm_options_version_set(options, nearest_version, 3)) {
221225
rb_raise(rb_eArgError, "invalid nearest version: %s", nearest_version);
222226
}
227+
228+
RB_GC_GUARD(ruby_version_string); // Do not move ruby_version while running the code above
223229
} else if (!pm_options_version_set(options, version, RSTRING_LEN(value))) {
224230
rb_raise(rb_eArgError, "invalid version: %" PRIsVALUE, value);
225231
}

0 commit comments

Comments
 (0)