Tamar Shinar <[EMAIL PROTECTED]> writes:
>Hi,
>
>I am trying to write an xs interface to an external library.
>My code takes two perl arrays, allocates and populates corresponding C structures,
>and passes them to the library routine.
>The problem is that the program is unstable. It will sometimes cause core dumps in
>the external library. The library in itself is believed to be stable, so it seems to
>be something in the perl/C interface. I use the following three functions to
>retrieve values from my perl objects in order to copy them into the C structures.
>Does anyone see anything wrong here?
>
>thank you,
>
>Tamar
>_______________________________________________________
>
>char *
>getString(HV * source, char * fieldName)
>{
> SV ** sv = hv_fetch(source, fieldName, strlen(fieldName), FALSE);
> if(!SvOK(*sv)) {
> printf("Error - hv_fetch %s returned bad sv\n", fieldName);
> }
> return SvPV(*sv, PL_na);
>}
The 1st thing to notice is that return from hv_fetch can be NULL if field
does not exist in the hash. So SvOK(*sv) will core dump, also you go on
to de-ref the thing even if your test said SV was not okay.
So you want something like this - you can provide "safe" values for
not-there and undef cases - I have used NULL and "" as examples.
SV **svp = hv_fetch(...)
if (*svp) {
if (SvOK(*svp)) {
/* defined value of some kind (but may not be a string, but SvPV will
force that for us). In perl5.6+ you may need to consider case
of string being in UTF-8
*/
return SvPV_nolen(*svp);
}
else
{
/* undef value */
return "";
}
}
else {
/* No such member */
return NULL;
}
--
Nick Ing-Simmons
http://www.ni-s.u-net.com/