Does RSTRING_PTR() return a '\0' terminated C?

  • Thread starter Iñaki Baz Castillo
  • Start date
I

Iñaki Baz Castillo

Hi, in a Ruby C extension I need to get a '\0' terminated string given
a Ruby string. I'm just interesed in Ruby 1.9.

I'm testing that RSTRING_PTR(str) does returns a '\0' terminated
string, ot it seems so, but I'm not sure. Source code says:

#define RSTRING_PTR(str) \
(!(RBASIC(str)->flags & RSTRING_NOEMBED) ? \
RSTRING(str)->as.ary : \
RSTRING(str)->as.heap.ptr)

Honestly, no idea about what it does :)

So, can somebody ensure me that RSTRING_PTR() returns a pointer to C
char finished with '\0'?

Thanks a lot.


--=20
I=C3=B1aki Baz Castillo
<[email protected]>
 
E

Eric Wong

Iñaki Baz Castillo said:
Hi, in a Ruby C extension I need to get a '\0' terminated string given
a Ruby string. I'm just interesed in Ruby 1.9.

I'm testing that RSTRING_PTR(str) does returns a '\0' terminated
string, ot it seems so, but I'm not sure. Source code says:

#define RSTRING_PTR(str) \
(!(RBASIC(str)->flags & RSTRING_NOEMBED) ? \
RSTRING(str)->as.ary : \
RSTRING(str)->as.heap.ptr)

Honestly, no idea about what it does :)

It's one of my favorite things about Ruby 1.9: small strings (3 words -
1 byte) are embedded directly inside the object to avoid malloc()
overhead.

RSTRING_PTR() checks for the embed flag, and it'll return the embedded C
string, otherwise it'll return the pointer allocated from malloc().

The same thing is done with RArray and RStruct, looking at the structure
definitions will be useful.
So, can somebody ensure me that RSTRING_PTR() returns a pointer to C
char finished with '\0'?

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 :)
 
E

Eric Hodel

I=F1aki Baz Castillo said:
Hi, in a Ruby C extension I need to get a '\0' terminated string = given
a Ruby string. I'm just interesed in Ruby 1.9.
=20
I'm testing that RSTRING_PTR(str) does returns a '\0' terminated
string, ot it seems so, but I'm not sure. Source code says:
=20
#define RSTRING_PTR(str) \
(!(RBASIC(str)->flags & RSTRING_NOEMBED) ? \
RSTRING(str)->as.ary : \
RSTRING(str)->as.heap.ptr)
=20
Honestly, no idea about what it does :)
=20
It's one of my favorite things about Ruby 1.9: small strings (3 words = -
1 byte) are embedded directly inside the object to avoid malloc()
overhead.
=20
RSTRING_PTR() checks for the embed flag, and it'll return the embedded = C
string, otherwise it'll return the pointer allocated from malloc().
=20
The same thing is done with RArray and RStruct, looking at the = structure
definitions will be useful.
=20
So, can somebody ensure me that RSTRING_PTR() returns a pointer to C
char finished with '\0'?
=20
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...)
=20
[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.

If you want a safe C string use StringValueCStr().

PS: See also README.EXT section 1.3=
 
E

Eric Wong

Eric Hodel said:
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.
 
I

Iñaki Baz Castillo

2011/2/5 Yukihiro Matsumoto said:
It was true on 1.8 but no longer on 1.9. =C2=A0If you need NUL terminated
string, use StringValueCStr().

Thanks to all. Indeed StringValueCStr() does the job. However until
now I was using:

c_str =3D RSTRING_PTR(rb_str);

and it always creates a '\0' terminated C string (Ruby 1.9.2). However
I'm using very short strings (domain names), maybe this is the reason.

Thanks a lot.

--=20
I=C3=B1aki Baz Castillo
<[email protected]>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top