rubinius/rubinius — Issue #1579: FFI unexpectedly returning blank string
Author: djberg96
Created: 2012-02-28 20:23:47+00:00
State: open

I'm having an issue with FFI on Rubinius (using the latest source, 28-Feb-2012) where I'm getting blank strings instead of the expected string when referencing a struct member. The following script using getmntinfo64 (on OSX Snow Leopard) demonstrates the issue:

# With MRI or JRuby I get expected results. With Rubinius I get empty strings.

# rubinius 2.0.0dev (1.8.7 057326c1 yyyy-mm-dd JI) [x86_64-apple-darwin10.8.0]

require 'ffi'

class Filesystem
extend FFI::Library

ffi_lib FFI::Library::LIBC

attach_function(:getmntinfo, [:pointer, :int], :int)
attach_function(:getmntinfo64, [:pointer, :int], :int) # OSX
attach_function(:strerror, [:int], :string)

class Statfs < FFI::Struct
layout(
:f_bsize, :uint32,
:f_iosize, :int32,
:f_blocks, :uint64,
:f_bfree, :uint64,
:f_bavail, :uint64,
:f_files, :uint64,
:f_ffree, :uint64,
:f_fsid, [:int32,2],
:f_owner, :int32,
:f_type, :uint32,
:f_flags, :uint32,
:f_fssubtype, :uint32,
:f_fstypename, [:char, 16],
:f_mntonname, [:char, 1024],
:f_mntfromname, [:char, 1024],
:f_reserved, [:uint32, 8]
)
end

def self.mounts
buf = FFI::MemoryPointer.new(:pointer)

num = getmntinfo64(buf, 0)

if num == 0
raise Error, 'getmntinfo() function failed: ' + strerror(FFI.errno)
end

ptr = buf.get_pointer(0)

num.times{ |i|
mnt = Statfs.new(ptr)
p mnt[:f_mntonname] # Works as expected with MRI, JRuby; empty string using rbx
ptr += Statfs.size
}
end
end

Filesystem.mounts