Forum: Ruby Problem with memcpy, marshal, zero

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Aee77dba395ece0a04c688b05b07cd63?d=identicon&s=25 Daniel Berger (Guest)
on 2007-05-04 22:20
(Received via mailing list)

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 = => 12, :name => 'test')
mm.gvalue = 5
p mm.gvalue # 5

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?


Aa8b5f9730c6a8a934959d62cd064619?d=identicon&s=25 Thomas Hurst (Guest)
on 2007-05-05 18:05
(Received via mailing list)
* Daniel Berger ( 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.