[前][次][番号順一覧][スレッド一覧]

ruby-changes:65113

From: Aaron <ko1@a...>
Date: Tue, 2 Feb 2021 05:20:59 +0900 (JST)
Subject: [ruby-changes:65113] 8ef30bcc04 (master): Fix GC compatibility: Don't stash encodings in global constants

https://git.ruby-lang.org/ruby.git/commit/?id=8ef30bcc04

From 8ef30bcc047341b2b7e6ec9b545dda975cdd4ab2 Mon Sep 17 00:00:00 2001
From: Aaron Patterson <tenderlove@r...>
Date: Mon, 1 Feb 2021 11:10:22 -0800
Subject: Fix GC compatibility: Don't stash encodings in global constants

This value should either be pinned, or looked up when needed at runtime.
Without pinning, the GC may move the encoding object, and that could
cause a crash.

In this case it is easier to find the value at runtime, and there is no
performance penalty (as Ruby caches encoding indexes).  We can shorten
the code, be compaction friendly, and incur no performance penalty.
---
 ext/json/generator/generator.c | 12 +-----------
 1 file changed, 1 insertion(+), 11 deletions(-)

diff --git a/ext/json/generator/generator.c b/ext/json/generator/generator.c
index 407c1af..e3a8347 100644
--- a/ext/json/generator/generator.c
+++ b/ext/json/generator/generator.c
@@ -1,11 +1,6 @@ https://github.com/ruby/ruby/blob/trunk/ext/json/generator/generator.c#L1
 #include "../fbuffer/fbuffer.h"
 #include "generator.h"
 
-#ifdef HAVE_RUBY_ENCODING_H
-static VALUE CEncoding_UTF_8;
-static ID i_encoding, i_encode;
-#endif
-
 static VALUE mJSON, mExt, mGenerator, cState, mGeneratorMethods, mObject,
              mHash, mArray,
 #ifdef RUBY_INTEGER_UNIFICATION
@@ -948,7 +943,7 @@ static void generate_json_string(FBuffer *buffer, VALUE Vstate, JSON_Generator_S https://github.com/ruby/ruby/blob/trunk/ext/json/generator/generator.c#L943
     fbuffer_append_char(buffer, '"');
 #ifdef HAVE_RUBY_ENCODING_H
     if (!enc_utf8_compatible_p(rb_enc_get(obj))) {
-        obj = rb_str_encode(obj, CEncoding_UTF_8, 0, Qnil);
+        obj = rb_str_export_to_enc(obj, rb_utf8_encoding());
     }
 #endif
     if (state->ascii_only) {
@@ -1610,9 +1605,4 @@ void Init_generator(void) https://github.com/ruby/ruby/blob/trunk/ext/json/generator/generator.c#L1605
     i_match = rb_intern("match");
     i_keys = rb_intern("keys");
     i_dup = rb_intern("dup");
-#ifdef HAVE_RUBY_ENCODING_H
-    CEncoding_UTF_8 = rb_funcall(rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("utf-8"));
-    i_encoding = rb_intern("encoding");
-    i_encode = rb_intern("encode");
-#endif
 }
-- 
cgit v1.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]