logo       

member strings one character too long: msg#00124

programming.swig

Subject: member strings one character too long

hello,

i have encountered a problem with character array
members in classes in swig-1.3.22. here is an example:

the interface is for the following test case...

class zap {
public:
char str[4];
zap() {
strncpy(str, "FOO", 3);
str[3] = 0;
}
};

the wrapper code has

static PyObject *_wrap_zap_str_get(PyObject *, PyObject
*args) {
PyObject *resultobj;
zap *arg1 = (zap *) 0 ;
char *result;
PyObject * obj0 = 0 ;

if(!PyArg_ParseTuple(args,(char
*)"O:zap_str_get",&obj0)) goto fail;
if ((SWIG_ConvertPtr(obj0,(void
**)(&arg1),SWIGTYPE_p_zap,
SWIG_POINTER_EXCEPTION | 0)) == -1) SWIG_fail;
result = (char *)(char *) ((arg1)->str);

resultobj = SWIG_FromCharArray(result, 4);
return resultobj;
fail:
return NULL;
}

which appears to make the returned string one character
too long (ie. it includes the terminating NULL). the
outcome is that, if you do something like:

import example

z = example.zap()

print z.str

and redirect the output to a file (you don't see the
NULL in a terminal!), the it looks like:

FOO^@

i trust that i am not doing something stupid here
(although that is always a possibility!).

i have investigated this a little further. the same
example code with swig-1.3.17, gives:

static PyObject *_wrap_zap_str_get(PyObject *self, PyObject
*args) {
PyObject *resultobj;
zap *arg1 = (zap *) 0 ;
char *result;
PyObject * obj0 = 0 ;

if(!PyArg_ParseTuple(args,(char
*)"O:zap_str_get",&obj0)) goto fail;
if ((SWIG_ConvertPtr(obj0,(void **) &arg1,
SWIGTYPE_p_zap,SWIG_POINTER_EXCEPTION | 0 )) == -1) SWIG_fail;
result = (char *)(char *) ((arg1)->str);

resultobj = PyString_FromString(result);
return resultobj;
fail:
return NULL;
}

this works perfectly, as i would expect.

does this mean that something unintended has been introduced
between versions 1.3.17 and 1.3.22?

cheers,
andrew.

--
Andrew B. Collier

Antarctic Research Fellow tel: +27 31 2601157
Space Physics Research Institute fax: +27 31 2616550
University of KwaZulu-Natal, Durban, 4041, South Africa
_______________________________________________
Swig maillist - Swig@xxxxxxxxxxxxxxx
http://mailman.cs.uchicago.edu/mailman/listinfo/swig



<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise