[#8815] Segfault in libc strlen, via rb_str_new2 — "Sean E. Russell" <ser@...>

Howdy,

12 messages 2006/09/09
[#8817] Re: Segfault in libc strlen, via rb_str_new2 — Eric Hodel <drbrain@...7.net> 2006/09/09

On Sep 8, 2006, at 10:10 PM, Sean E. Russell wrote:

Re: Segfault in libc strlen, via rb_str_new2

From: Steven Jenkins <steven.jenkins@...>
Date: 2006-09-15 23:41:17 UTC
List: ruby-core #8883
Marc Haisenko wrote:
> On Friday 15 September 2006 16:46, Vincent Fourmond wrote:
>>> One is NEVER allowed to assume that a pointer can be stored in an 
>>> arithmetic  
>>> value, e.g. it's possible that on some architecture arithmetic values are 
>>> 32  
>>> bit and pointers are 36 bit (IIRC there is an architecture where this is 
>>> really true).
>>   Unless maybe in when compiling some extension for ruby ?
>>
>> ruby.h: (at least in version 1.8.4)
>>
>> #if SIZEOF_LONG != SIZEOF_VOIDP
>> # error ---->> ruby requires sizeof(void*) == sizeof(long) to be
>> compiled. <<----
>>
>> 	Vince
> 
> The fact that people do this stuff doesn't mean it's correct to do so.
> 
> At least someone knew that this is dangerous and added a check so when Ruby 
> gets compiled on an architecture where pointers and arithmetic values are of 
> different size (AFAIK that boils down to the address registers being of 
> different size than "normal" registers) you get a meaningful error instead of 
> obscure crashes :-)
> 
> I guess the assumption that sizeof(void*) == sizeof(long) is correct on most 
> hardware plattforms that float around today, but there's just no guarantee 
> that it's true for ALL plattforms. When writing portable software you have to 
> keep stuff like this in mind (or at least add checks ;-)

Right. To be strictly pedantic, however, the check should be

sizeof (void *) >= sizeof (long)

The C standard guarantees that a pointer can be assigned to an integral
type large enough to hold it (if such a type exists), and that
converting such an integer back to a pointer to the original type will
yield the original pointer value. It just doesn't guarantee that such a
type exists on any given platform.

Steve

In This Thread

Prev Next