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

ruby-changes:63831

From: Kenta <ko1@a...>
Date: Wed, 2 Dec 2020 10:32:55 +0900 (JST)
Subject: [ruby-changes:63831] 82dc0c6aa3 (master): memory_view.c: Check availability in rb_memory_view_get

https://git.ruby-lang.org/ruby.git/commit/?id=82dc0c6aa3

From 82dc0c6aa380ea736adcd5ea54ec8f77a9269007 Mon Sep 17 00:00:00 2001
From: Kenta Murata <mrkn@m...>
Date: Wed, 2 Dec 2020 09:42:05 +0900
Subject: memory_view.c: Check availability in rb_memory_view_get


diff --git a/ext/-test-/memory_view/memory_view.c b/ext/-test-/memory_view/memory_view.c
index 7f1f007..f7c5090 100644
--- a/ext/-test-/memory_view/memory_view.c
+++ b/ext/-test-/memory_view/memory_view.c
@@ -35,7 +35,8 @@ exportable_string_get_memory_view(VALUE obj, rb_memory_view_t *view, int flags) https://github.com/ruby/ruby/blob/trunk/ext/-test-/memory_view/memory_view.c#L35
 static int
 exportable_string_memory_view_available_p(VALUE obj)
 {
-    return Qtrue;
+    VALUE str = rb_ivar_get(obj, id_str);
+    return !NIL_P(str);
 }
 
 static const rb_memory_view_entry_t exportable_string_memory_view_entry = {
@@ -232,6 +233,9 @@ memory_view_ref_count_while_exporting(VALUE mod, VALUE obj, VALUE n) https://github.com/ruby/ruby/blob/trunk/ext/-test-/memory_view/memory_view.c#L233
 static VALUE
 expstr_initialize(VALUE obj, VALUE s)
 {
+    if (!NIL_P(s)) {
+        Check_Type(s, T_STRING);
+    }
     rb_ivar_set(obj, id_str, s);
     return Qnil;
 }
diff --git a/memory_view.c b/memory_view.c
index aade3a4..4a4245a 100644
--- a/memory_view.c
+++ b/memory_view.c
@@ -592,6 +592,10 @@ rb_memory_view_get(VALUE obj, rb_memory_view_t* view, int flags) https://github.com/ruby/ruby/blob/trunk/memory_view.c#L592
     VALUE klass = CLASS_OF(obj);
     const rb_memory_view_entry_t *entry = lookup_memory_view_entry(klass);
     if (entry) {
+        if (!(*entry->available_p_func)(obj)) {
+            return 0;
+        }
+
         int rv = (*entry->get_func)(obj, view, flags);
         if (rv) {
             register_exported_object(view->obj);
diff --git a/test/ruby/test_memory_view.rb b/test/ruby/test_memory_view.rb
index 668d738..2432f71 100644
--- a/test/ruby/test_memory_view.rb
+++ b/test/ruby/test_memory_view.rb
@@ -197,6 +197,13 @@ class TestMemoryView < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_memory_view.rb#L197
     assert_equal(expected_result, members)
   end
 
+  def test_rb_memory_view_available_p
+    es = MemoryViewTestUtils::ExportableString.new("ruby")
+    assert_equal(true, MemoryViewTestUtils.available?(es))
+    es = MemoryViewTestUtils::ExportableString.new(nil)
+    assert_equal(false, MemoryViewTestUtils.available?(es))
+  end
+
   def test_ref_count_with_exported_object
     es = MemoryViewTestUtils::ExportableString.new("ruby")
     assert_equal(1, MemoryViewTestUtils.ref_count_while_exporting(es, 1))
@@ -223,6 +230,12 @@ class TestMemoryView < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_memory_view.rb#L230
                  memory_view_info)
   end
 
+  def test_rb_memory_view_get_with_memory_view_unavailable_object
+    es = MemoryViewTestUtils::ExportableString.new(nil)
+    memory_view_info = MemoryViewTestUtils.get_memory_view_info(es)
+    assert_nil(memory_view_info)
+  end
+
   def test_rb_memory_view_fill_contiguous_strides
     row_major_strides = MemoryViewTestUtils.fill_contiguous_strides(3, 8, [2, 3, 4], true)
     assert_equal([96, 32, 8],
-- 
cgit v0.10.2


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

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