Forum: Ruby Problem with memcpy, marshal, zero

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Daniel B. (Guest)
on 2007-05-05 00:20
(Received via mailing list)
Hi,

Ruby 1.8.6
Windows XP Pro

In the win32-mmap package I have the code setup such that you can
create a dynamic accessor. Internally, I marshal the value (as a hash)
and copy it to an address via memcpy (wrapped using Win32API). So,
when you do something like this:

mm = MMap.new(:size => 12, :name => 'test')
mm.gvalue = 5
p mm.gvalue # 5
mm.close

It's actually doing something like this when you set a value:

h = {"gvalue"=>0}
m = Marshal.dump(h)
p m => "\004\b{\006\"\vgvaluei\000"
memcpy(address, marshal, marshal.length)

The problem is that trailing "\000" gets chopped off by the memcpy
function because it treats it like a trailing null as far as I can
tell. So, when I try to load it again the marshal data isn't
identical, i.e. the trailing "\000" is gone, and I end up getting  a
"marshal data too short" error.

What's the solution here?

Thanks,

Dan
Thomas H. (Guest)
on 2007-05-05 20:05
(Received via mailing list)
* Daniel B. (removed_email_address@domain.invalid) wrote:

>
> What's the solution here?

At the end of Win32API_Call() I see:

    case _T_POINTER:
            return rb_str_new2((char *)ret);

rb_str_new2 does rb_str_new(ptr, strlen(ptr)), so if you're using
Win32API to return any string with a NULL byte, it'll get truncated.  On
the other hand, I've not spotted anything wrt input to suggest NULL is
handled as a delimiter; are you sure the NULL isn't being copied
properly, but not being returned properly by another call?
This topic is locked and can not be replied to.