Skip to content

Commit f5de7bb

Browse files
committed
[GR-18163] Check the argument is an array for FFI::Pointer#write_array_of_*.
PullRequest: truffleruby/1770
2 parents 414a17f + f776928 commit f5de7bb

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

src/main/ruby/truffleruby/core/truffle/ffi/pointer_access.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ def read_array_of_char(length)
6969
alias_method :read_array_of_int8, :read_array_of_char
7070

7171
def write_array_of_char(ary)
72+
Truffle::Type.rb_check_type(ary, ::Array)
7273
ary.each_with_index do |value, i|
7374
Primitive.pointer_write_char address + (i * 1), Primitive.rb_to_int(value)
7475
end
@@ -119,6 +120,7 @@ def read_array_of_uchar(length)
119120
alias_method :read_array_of_uint8, :read_array_of_uchar
120121

121122
def write_array_of_uchar(ary)
123+
Truffle::Type.rb_check_type(ary, ::Array)
122124
ary.each_with_index do |value, i|
123125
Primitive.pointer_write_uchar address + (i * 1), Primitive.rb_to_int(value)
124126
end
@@ -169,6 +171,7 @@ def read_array_of_short(length)
169171
alias_method :read_array_of_int16, :read_array_of_short
170172

171173
def write_array_of_short(ary)
174+
Truffle::Type.rb_check_type(ary, ::Array)
172175
ary.each_with_index do |value, i|
173176
Primitive.pointer_write_short address + (i * 2), Primitive.rb_to_int(value)
174177
end
@@ -219,6 +222,7 @@ def read_array_of_ushort(length)
219222
alias_method :read_array_of_uint16, :read_array_of_ushort
220223

221224
def write_array_of_ushort(ary)
225+
Truffle::Type.rb_check_type(ary, ::Array)
222226
ary.each_with_index do |value, i|
223227
Primitive.pointer_write_ushort address + (i * 2), Primitive.rb_to_int(value)
224228
end
@@ -269,6 +273,7 @@ def read_array_of_int(length)
269273
alias_method :read_array_of_int32, :read_array_of_int
270274

271275
def write_array_of_int(ary)
276+
Truffle::Type.rb_check_type(ary, ::Array)
272277
ary.each_with_index do |value, i|
273278
Primitive.pointer_write_int address + (i * 4), Primitive.rb_to_int(value)
274279
end
@@ -319,6 +324,7 @@ def read_array_of_uint(length)
319324
alias_method :read_array_of_uint32, :read_array_of_uint
320325

321326
def write_array_of_uint(ary)
327+
Truffle::Type.rb_check_type(ary, ::Array)
322328
ary.each_with_index do |value, i|
323329
Primitive.pointer_write_uint address + (i * 4), Primitive.rb_to_int(value)
324330
end
@@ -374,6 +380,7 @@ def read_array_of_long(length)
374380
alias_method :read_array_of_long_long, :read_array_of_long
375381

376382
def write_array_of_long(ary)
383+
Truffle::Type.rb_check_type(ary, ::Array)
377384
ary.each_with_index do |value, i|
378385
Primitive.pointer_write_long address + (i * 8), Primitive.rb_to_int(value)
379386
end
@@ -432,6 +439,7 @@ def read_array_of_ulong(length)
432439
alias_method :read_array_of_ulong_long, :read_array_of_ulong
433440

434441
def write_array_of_ulong(ary)
442+
Truffle::Type.rb_check_type(ary, ::Array)
435443
ary.each_with_index do |value, i|
436444
Primitive.pointer_write_ulong address + (i * 8), Primitive.rb_to_int(value)
437445
end
@@ -485,6 +493,7 @@ def read_array_of_float(length)
485493
alias_method :read_array_of_float32, :read_array_of_float
486494

487495
def write_array_of_float(ary)
496+
Truffle::Type.rb_check_type(ary, ::Array)
488497
ary.each_with_index do |value, i|
489498
Primitive.pointer_write_float address + (i * 4), Truffle::Type.rb_to_f(value)
490499
end
@@ -535,6 +544,7 @@ def read_array_of_double(length)
535544
alias_method :read_array_of_float64, :read_array_of_double
536545

537546
def write_array_of_double(ary)
547+
Truffle::Type.rb_check_type(ary, ::Array)
538548
ary.each_with_index do |value, i|
539549
Primitive.pointer_write_double address + (i * 8), Truffle::Type.rb_to_f(value)
540550
end
@@ -594,6 +604,7 @@ def read_array_of_pointer(length)
594604
end
595605

596606
def write_array_of_pointer(ary)
607+
Truffle::Type.rb_check_type(ary, ::Array)
597608
ary.each_with_index do |value, i|
598609
Primitive.pointer_write_pointer address + (i * 8), get_pointer_value(value)
599610
end

src/main/ruby/truffleruby/core/type.rb

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,12 @@ def self.check_funcall_callable(obj, name)
7272
Primitive.vm_method_lookup obj, name
7373
end
7474

75-
##
7675
# Returns an object of given class. If given object already is one, it is
7776
# returned. Otherwise tries obj.meth and returns the result if it is of the
78-
# right kind. TypeErrors are raised if the conversion method fails or the
77+
# right kind. TypeError is raised if the conversion method fails or the
7978
# conversion result is wrong.
8079
#
81-
# Uses Primitive.object_kind_of? to bypass type check overrides.
82-
#
83-
# Equivalent to MRI's rb_convert_type().
84-
80+
# Prefer the more correct #rb_convert_type.
8581
def self.coerce_to(obj, cls, meth)
8682
if Primitive.object_kind_of?(obj, cls)
8783
obj
@@ -266,6 +262,7 @@ def self.rb_convert_type(obj, cls, meth)
266262
end
267263
end
268264

265+
# MRI: Check_Type / rb_check_type
269266
def self.rb_check_type(obj, cls)
270267
unless Primitive.object_kind_of?(obj, cls)
271268
raise TypeError, "wrong argument type #{object_class(obj)} (expected #{cls})"

tool/generate-pointer-methods.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ def read_array_of_#{type}(length)
147147

148148
code << <<-RUBY
149149
def write_array_of_#{type}(ary)
150+
Truffle::Type.rb_check_type(ary, ::Array)
150151
ary.each_with_index do |value, i|
151152
Primitive.pointer_write_#{type} address + (i * #{SIZEOF[base_type]}), #{transform.call('value')}
152153
end

0 commit comments

Comments
 (0)