Hi,
I am experiencing a strange problem with a very simple SWIG interface to
a C library used from Perl. The used C-function simply generates a
filename and returns a pointer to a static character buffer:
char * fnl_request_filename(const char * filename,=20
const char * separation_value,=20
const char *src_id,=20
int sequence_update) {
static char fname[MAXNAMELEN];
/*=20
* create filename=20
*/
fprintf(stderr, "fname pointer : %p\n", fname);
return fname;
}
So, nothing fancy in it. The SWIG generated wrapper code is listed
below:
XS(_wrap_fnl_request_filename) {
char _swigmsg[SWIG_MAX_ERRMSG] =3D "";
const char *_swigerr =3D _swigmsg;
{
char *arg1 ;
char *arg2 ;
char *arg3 ;
int arg4 ;
char *result;
int argvi =3D 0;
dXSARGS;
=20
if ((items < 4) || (items > 4)) {
SWIG_croak("Usage:
fnl_request_filename(filename,separation_value,src_id,sequence_update);"
);
}
if (!SvOK((SV*) ST(0))) arg1 =3D "";
else arg1 =3D (char *) SvPV(ST(0), PL_na);
if (!SvOK((SV*) ST(1))) arg2 =3D "";
else arg2 =3D (char *) SvPV(ST(1), PL_na);
if (!SvOK((SV*) ST(2))) arg3 =3D "";
else arg3 =3D (char *) SvPV(ST(2), PL_na);
arg4 =3D (int) SvIV(ST(3));
result =3D (char *)fnl_request_filename((char const *)arg1,(char
const *)arg2,(char const *)arg3,arg4);
fprintf(stderr, "result : %p\n", result);
fprintf(stderr, "Filename is : ");
fprintf(stderr, "%s\n", result);
=20
ST(argvi) =3D sv_newmortal();
if (result) {
sv_setpv((SV*)ST(argvi++), (char *) result);
fprintf(stderr, "Result stacked\n");
}else {
sv_setsv((SV*)ST(argvi++), &PL_sv_undef);
}
fprintf(stderr, "Performing XSRETURN\n");
XSRETURN(argvi);
fail:
(void) _swigerr;
}
croak(_swigerr);
}
Note the additional fprint -statements for debugging purposes.
The perl scirpt uses the function like this:
my $name =3D fnl_request_filename("DataFile", undef, undef, 1);
When I run my perl script I get the following:
sol9dev> perl example.pl
fname pointer : ffffffff77d2cf88
result : 77d2cf88
Filename is : Segmentation Fault
The first line comes from the used C-library itself and the pointer
address is valid, the next two lines are printed from the SWIG wrapper
code. As can be seen the address where the pointer points to has been
tampered and all 'f' characters have been removed rendering the address
invalid.
Does anyone have any ideas what is going on? Is this SWIG related at
all?
SWIG Version 1.3.15u-20021129-1508
Platform: Sun Solaris 9, 64-bit, sparcv9
/jUSSi
--=20
perl -e '$@=3D[[0,0112,0,0],[0,0101,0,0],
[0,0120,0,0],[0,0110,0,0]];$[=3D256/8-0x1D;
print(chr(@{$_}[+($[+1-1+1)]))for(@{$@});'
Disclaimer: This message and any attachments thereto are intended solely=
for
the addressed recipient(s) and may contain confidential information. If you
are not the intended recipient, please notify the sender by reply e-mail=
and
delete the e-mail (including any attachments thereto) without producing,
distributing or retaining any copies thereof. Any review, dissemination or
other use of, or taking of any action in reliance upon, this information by
persons or entities other than the intended recipient(s) is prohibited.
Thank you.
|