Change the length of strings created from fixed-size buffers.

Use the usual C rule for NUL-terminated strings instead of discarding all the
trailing NUL characters.

This was unexpected (as buffers in C code are not necessarily always padded
with NULs to their full length) and also inconsistent among languages as this
was only done for those of them using typemaps/strings.swg but not for C# or
Java, for example, which terminated the string at the first NUL even before
this change.

Notice that this patch couldn't use strlen() or wcslen() with possibly not
NUL-terminated strings, so we had to add [our own equivalents of] strnlen()
and wcsnlen() and use them instead. This required adding yet another parameter
to string typemap macros, so update the example using them accordingly too.
This commit is contained in:
Vadim Zeitlin 2013-12-18 01:28:32 +01:00 committed by William S Fulton
commit 88a0e228a9
8 changed files with 75 additions and 20 deletions

View file

@ -93,6 +93,17 @@ SWIG_FromXMLChPtrAndSize(const XMLCh* input, size_t size)
}
}
%fragment("SWIG_XMLStringNLen","header") {
size_t
SWIG_XMLStringNLen(const XMLCh* s, size_t maxlen)
{
const XMLCh *p;
for (p = s; maxlen-- && *p; p++)
;
return p - s;
}
}
%init {
if (!SWIG_UTF8Transcoder()) {
croak("ERROR: XML::Xerces: INIT: Could not create UTF-8 transcoder");
@ -106,6 +117,7 @@ SWIG_FromXMLChPtrAndSize(const XMLCh* input, size_t size)
SWIG_AsXMLChPtrAndSize,
SWIG_FromXMLChPtrAndSize,
XERCES_CPP_NAMESPACE::XMLString::stringLen,
SWIG_XMLStringNLen,
"<XMLCh.h>", INT_MIN, INT_MAX);

View file

@ -166,7 +166,7 @@ $t->{var_paramd} = $primitive_types::sct_paramd;
$t->{var_paramc} = $primitive_types::sct_paramc;
ok($t->v_check(), 'v_check');
is($primitive_types::def_namet, "ho\0la", "namet");
is($primitive_types::def_namet, "hola", "namet");
$t->{var_namet} = $primitive_types::def_namet;
is($t->{var_namet}, $primitive_types::def_namet, "namet");

View file

@ -185,7 +185,7 @@
char* const def_pchar = (char *const)"hello";
const char* const def_pcharc = "hija";
const namet def_namet = {'h','o',0, 'l','a'};
const namet def_namet = {'h','o','l','a', 0};
extern namet gbl_namet;

View file

@ -165,7 +165,7 @@ t.var_paramc = sct_paramc
t.v_check()
# this value contains a '0' char!
if def_namet != 'ho\0la':
if def_namet != 'hola':
print "bad namet", def_namet
raise RuntimeError