jaeandersson / swig

SWIG is a software development tool that connects programs written in C and C++ with a variety of high-level programming languages.
http://www.swig.org
Other
23 stars 19 forks source link

std::string with more than 255 characters fails type check #88

Open VladimirIvan opened 7 years ago

VladimirIvan commented 7 years ago

An std::string passed as an argument to a function from matlab fails the type check when the string length is greater than 255 characters. The SWIG_Matlab_ConvertPtrAndOwn method returns error in the first condition in it's body when checking for mxGetNumberOfElements(pm_ptr) != 1. The exact same code works correctly for shorter strings.

To reproduce this, compile the following input file:

%module test

%{

#include <string>
#include <iostream>
void myStringFunction(std::string str)
{
  std::cout << "Your string was:\n" << str << "\n";
}

%}

%include std_string.i

%inline %{
 void myStringFunction(std::string str);
%}

In matlab:

s=repmat('test string ',1,100);
test.myStringFunction(s(1:255)); % Works fine
test.myStringFunction(s); % Fails inside type check

I have compiled the code with c++11 flags enabled.

Alzathar commented 7 years ago

The problem is related to the internal buffer used for the conversion. See this line: https://github.com/jaeandersson/swig/blob/matlab/Lib/matlab/matlabprimtypes.swg#L235

It might be needed to increase the size of this static buffer or use a dynamic array.