Subject:
[ruby-ffi] checkBounds in Pointer#slice
From:
Charles Strahan
Date:
4/13/11 11:27 PM
To:
ruby-ffi@googlegroups.com

Hello,

I've found a potential problem with Pointer#slice:

irb(main):078:0> mem = FFI::MemoryPointer.new(1,8)
=> #<FFI::MemoryPointer address=0x2e997d0 size=8>
irb(main):079:0> mem.slice(0,9)
=> #<FFI::Pointer address=0x2e997d0 size=9>


As you can see, it's possible to extend the size of the pointer. However, it's not possible to offset the pointer past the total original size:

irb(main):080:0> mem.slice(8,9)
IndexError: Memory access offset=8 size=1 is out of bounds
        from (irb):80:in `slice'
        from (irb):80
        from C:/Ruby192/bin/irb:12:in `<main>'



I see that Pointer#slice is defined as follows:

static VALUE
slice(VALUE self, long offset, long size)
{
    AbstractMemory* ptr;
    Pointer* p;
    VALUE retval;
    
    Data_Get_Struct(self, AbstractMemory, ptr);
    checkBounds(ptr, offset, 1);

    retval = Data_Make_Struct(rbffi_PointerClass, Pointer, ptr_mark, -1, p);

    p->memory.address = ptr->address + offset;
    p->memory.size = size;
    p->memory.flags = ptr->flags;
    p->memory.typeSize = ptr->typeSize;
    p->parent = self;

    return retval;
}


Perhaps `checkBounds(ptr, offset, 1);' should be replaced with something like the following?

checkBounds(ptr, offset, size == MAX_LONG ? 1 : size);        

... or is the original behavior by design?


-Charles