diff options
author | Wayne Meissner <wmeissner@gmail.com> | 2009-12-19 12:53:08 +1000 |
---|---|---|
committer | Wayne Meissner <wmeissner@gmail.com> | 2009-12-19 12:53:08 +1000 |
commit | 610fedd9de7a748221f43a6e94485c23a29f5dff (patch) | |
tree | 5a329065ecb70790b1c3d15608334609e70bcdd3 | |
parent | d6c005312bf35b70135a52838c27d0eff0d6c37b (diff) | |
download | ffi-610fedd9de7a748221f43a6e94485c23a29f5dff.tar.gz |
Add explicit index checking to array fields of Struct
-rw-r--r-- | ext/ffi_c/Struct.c | 17 |
1 files changed, 12 insertions, 5 deletions
diff --git a/ext/ffi_c/Struct.c b/ext/ffi_c/Struct.c index a656f57..710f642 100644 --- a/ext/ffi_c/Struct.c +++ b/ext/ffi_c/Struct.c @@ -63,6 +63,8 @@ typedef struct InlineArray_ { StructField* field; MemoryOp *op; Type* componentType; + ArrayType* arrayType; + unsigned int length; } InlineArray; @@ -568,6 +570,7 @@ inline_array_initialize(VALUE self, VALUE rbMemory, VALUE rbField) Data_Get_Struct(arrayType->rbComponentType, Type, array->componentType); array->op = get_memory_op(array->componentType); + array->length = arrayType->length; return self; } @@ -583,8 +586,12 @@ inline_array_size(VALUE self) } static int -inline_array_offset(InlineArray* array, unsigned int index) +inline_array_offset(InlineArray* array, int index) { + if (index < 0 || index >= array->length) { + rb_raise(rb_eIndexError, "index %d out of bounds", index); + } + return array->field->offset + (index * array->componentType->ffiType->size); } @@ -596,9 +603,9 @@ inline_array_aref(VALUE self, VALUE rbIndex) Data_Get_Struct(self, InlineArray, array); if (array->op != NULL) { - return array->op->get(array->memory, inline_array_offset(array, NUM2UINT(rbIndex))); + return array->op->get(array->memory, inline_array_offset(array, NUM2INT(rbIndex))); } else if (array->componentType->nativeType == NATIVE_STRUCT) { - int offset = inline_array_offset(array, NUM2UINT(rbIndex)); + int offset = inline_array_offset(array, NUM2INT(rbIndex)); VALUE rbOffset = INT2NUM(offset); VALUE rbPointer = rb_funcall2(array->rbMemory, rb_intern("+"), 1, &rbOffset); @@ -620,10 +627,10 @@ inline_array_aset(VALUE self, VALUE rbIndex, VALUE rbValue) Data_Get_Struct(self, InlineArray, array); if (array->op != NULL) { - array->op->put(array->memory, inline_array_offset(array, NUM2UINT(rbIndex)), + array->op->put(array->memory, inline_array_offset(array, NUM2INT(rbIndex)), rbValue); } else if (array->componentType->nativeType == NATIVE_STRUCT) { - int offset = inline_array_offset(array, NUM2UINT(rbIndex)); + int offset = inline_array_offset(array, NUM2INT(rbIndex)); ArrayType* arrayType; Struct* s; |