* ext/dl/cptr.c (rb_dlptr_aref, rb_dlptr_aset): check NULL pointer
dereference. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@32720 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
ad8ab9c013
commit
eda13c7ee2
@ -1,4 +1,7 @@
|
|||||||
Thu Jul 28 12:32:46 2011 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
Thu Jul 28 12:32:49 2011 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
|
* ext/dl/cptr.c (rb_dlptr_aref, rb_dlptr_aset): check NULL pointer
|
||||||
|
dereference.
|
||||||
|
|
||||||
* ext/dl/cptr.c (rb_dlptr_s_to_ptr): use rb_check_funcall.
|
* ext/dl/cptr.c (rb_dlptr_s_to_ptr): use rb_check_funcall.
|
||||||
|
|
||||||
|
@ -502,16 +502,19 @@ rb_dlptr_aref(int argc, VALUE argv[], VALUE self)
|
|||||||
VALUE arg0, arg1;
|
VALUE arg0, arg1;
|
||||||
VALUE retval = Qnil;
|
VALUE retval = Qnil;
|
||||||
size_t offset, len;
|
size_t offset, len;
|
||||||
|
struct ptr_data *data;
|
||||||
|
|
||||||
|
TypedData_Get_Struct(self, struct ptr_data, &dlptr_data_type, data);
|
||||||
|
if (!data->ptr) rb_raise(rb_eDLError, "NULL pointer dereference");
|
||||||
switch( rb_scan_args(argc, argv, "11", &arg0, &arg1) ){
|
switch( rb_scan_args(argc, argv, "11", &arg0, &arg1) ){
|
||||||
case 1:
|
case 1:
|
||||||
offset = NUM2ULONG(arg0);
|
offset = NUM2ULONG(arg0);
|
||||||
retval = INT2NUM(*((char*)RPTR_DATA(self)->ptr + offset));
|
retval = INT2NUM(*((char *)data->ptr + offset));
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
offset = NUM2ULONG(arg0);
|
offset = NUM2ULONG(arg0);
|
||||||
len = NUM2ULONG(arg1);
|
len = NUM2ULONG(arg1);
|
||||||
retval = rb_tainted_str_new((char *)RPTR_DATA(self)->ptr + offset, len);
|
retval = rb_tainted_str_new((char *)data->ptr + offset, len);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
rb_bug("rb_dlptr_aref()");
|
rb_bug("rb_dlptr_aref()");
|
||||||
@ -535,17 +538,20 @@ rb_dlptr_aset(int argc, VALUE argv[], VALUE self)
|
|||||||
VALUE retval = Qnil;
|
VALUE retval = Qnil;
|
||||||
size_t offset, len;
|
size_t offset, len;
|
||||||
void *mem;
|
void *mem;
|
||||||
|
struct ptr_data *data;
|
||||||
|
|
||||||
|
TypedData_Get_Struct(self, struct ptr_data, &dlptr_data_type, data);
|
||||||
|
if (!data->ptr) rb_raise(rb_eDLError, "NULL pointer dereference");
|
||||||
switch( rb_scan_args(argc, argv, "21", &arg0, &arg1, &arg2) ){
|
switch( rb_scan_args(argc, argv, "21", &arg0, &arg1, &arg2) ){
|
||||||
case 2:
|
case 2:
|
||||||
offset = NUM2ULONG(arg0);
|
offset = NUM2ULONG(arg0);
|
||||||
((char*)RPTR_DATA(self)->ptr)[offset] = NUM2UINT(arg1);
|
((char*)data->ptr)[offset] = NUM2UINT(arg1);
|
||||||
retval = arg1;
|
retval = arg1;
|
||||||
break;
|
break;
|
||||||
case 3:
|
case 3:
|
||||||
offset = NUM2ULONG(arg0);
|
offset = NUM2ULONG(arg0);
|
||||||
len = NUM2ULONG(arg1);
|
len = NUM2ULONG(arg1);
|
||||||
if( TYPE(arg2) == T_STRING ){
|
if (RB_TYPE_P(arg2, T_STRING)) {
|
||||||
mem = StringValuePtr(arg2);
|
mem = StringValuePtr(arg2);
|
||||||
}
|
}
|
||||||
else if( rb_obj_is_kind_of(arg2, rb_cDLCPtr) ){
|
else if( rb_obj_is_kind_of(arg2, rb_cDLCPtr) ){
|
||||||
@ -554,7 +560,7 @@ rb_dlptr_aset(int argc, VALUE argv[], VALUE self)
|
|||||||
else{
|
else{
|
||||||
mem = NUM2PTR(arg2);
|
mem = NUM2PTR(arg2);
|
||||||
}
|
}
|
||||||
memcpy((char *)RPTR_DATA(self)->ptr + offset, mem, len);
|
memcpy((char *)data->ptr + offset, mem, len);
|
||||||
retval = arg2;
|
retval = arg2;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
@ -212,5 +212,11 @@ module DL
|
|||||||
assert_equal ptr3.to_i, ptr[0,2] = ptr3.to_i
|
assert_equal ptr3.to_i, ptr[0,2] = ptr3.to_i
|
||||||
check.call(str, ptr)
|
check.call(str, ptr)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_null_pointer
|
||||||
|
nullpo = CPtr.new(0)
|
||||||
|
assert_raise(DLError) {nullpo[0]}
|
||||||
|
assert_raise(DLError) {nullpo[0] = 1}
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user