From: Eric Wong Date: 2011-02-05T05:54:48+09:00 Subject: Re: Does RSTRING_PTR() return a '\0' terminated C? Eric Hodel wrote: > On Feb 4, 2011, at 12:07 PM, Eric Wong wrote: > > It's always '\0'-terminated in Ruby[1], it makes life much easier for > > interacting with existing C functions that assume C strings (e.g. > > *printf("%s"), open(), rename(), unlink(), etc...) > > > > [1] - unless there's a bug somewhere, or somebody hell bent on using the > > extra byte :) > I don't think this is true, use RSTRING_LEN with RSTRING_PTR to get > the length since the string may contain embedded NULLs. Yes, embedded NULLs are always a problem :/ > If you want a safe C string use StringValueCStr(). Strange that StringValueCStr() NULL terminates explicitly, everywhere else I look in string.c there's always an extra byte allocated for NULL, but I suppose one can't be too careful in C. -- Eric Wong