PIKEFUN int `[](mixed index) { struct svalue *s;
simple_array_index_no_free(s, THIS->a, index);
Here you are dereferencing an uninitialized pointer. The result of the index operation is written to the svalue pointed to by s, but s is not pointing to any svalue. => segfault.
/ Marcus Comstedt (ACROSS) (Hail Ilpalazzo!)
Previous text:
2003-01-09 14:54: Subject: simple_array_index_no_free
I'm trying to get the value for an index in a array and use simple_array_index_no_free but only manage to make a coredump. I guess i could have something with the stack to do since when I run a test program like this.
int main() { array a = ({3,4,5,6,7,8,9,10,11}); Test.ArrayAdapter ad = Test.ArrayAdapter(a); write(sprintf("array %d adapter %d\r\n", a[1], ad[1])); for (int n=0; n< 9; n++) { write(sprintf("array %d adapter %d\r\n", a[n], ad[n])); } }
It will produce a coredump. But if replace the ad[1] with ad[1]=11 then it will work.
Does anyone have a idea what I've forgotten? Or is simple_array_index_no_free the wrong way to go? Greatfull for any help. Code follows...
DECLARATIONS
PIKECLASS ArrayAdapter {
CVAR int pos; CVAR struct array *a;
PIKEFUN int `[](mixed index) { struct svalue *s;
simple_array_index_no_free(s, THIS->a, index);
if (s) { push_svalue(s); } else { push_int(0); Pike_sp[-1].subtype=NUMBER_UNDEFINED; } }
PIKEFUN int `[]=(mixed index, mixed value) { simple_set_index(THIS->a, index, value); }
PIKEFUN void create(array a) { add_ref(THIS->a=a); }
INIT { THIS->a=0; THIS->pos=0; }
EXIT { free_array(THIS->a); }
}
/ Peta, jo det är jag