In extensions to ruby RARRAY_PTR() which returns a VALUE* for a given ruby array is dangerous, since it does no type checks. For example this will probably segfault:
VALUE a= rb_hash_new();
RARRAY_PTR(a)[999];
I sometimes find myself storing an array into an instance variable and then later doing RARRAY_PTR() on this instance variable. This can be dangerous since there are ways to tamper with any objects instance variables. If someone does this, I could be ending up doing RARRAY_PTR() one something which is not an array.
Is it bad pratice to rely on otheres not changing my instance variables, or is it there fault if then the ruby interpreter segfaults?
I wouldn’t worry about it that much. If someone is messing around with your instance variables then they’re responsible for whatever damage they cause.
If someone uses
instance_varible_setto change an instance variable from an array to a Fixnum in a pure Ruby class, then you’ll probably end up with aNoMethodError; if they do it to your C backed class, they’ll get a segfault.Basically, if someone wants to smash themselves in the face with a brick by messing around with an object’s internals, then it isn’t your problem.