Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamically allocate char buffers when converting from Matlab strings #89

Open
wants to merge 4 commits into
base: matlab
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Examples/test-suite/matlab/li_std_string_extra_runme.m
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,10 @@
%if (li_std_string_extra.c_null() ~= None)
% error
%end

% Long strings
s = repmat('hello',1,100);
sout = li_std_string_extra.test_reference_inout(s);
if (s ~= sout)
error
end
20 changes: 13 additions & 7 deletions Lib/matlab/matlabprimtypes.swg
Original file line number Diff line number Diff line change
Expand Up @@ -231,18 +231,24 @@ SWIGINTERN int
SWIG_AsCharPtrAndSize(mxArray* pm, char** cptr, size_t* psize, int *alloc)
{
if(!mxIsChar(pm) || (mxGetNumberOfElements(pm) != 0 && mxGetM(pm)!=1)) return SWIG_TypeError;
size_t len=mxGetN(pm);
static char buf[256];
int flag = mxGetString(pm,buf,(mwSize)sizeof(buf));
if(flag) return SWIG_TypeError;
size_t len=mxGetM(pm) * mxGetN(pm) + 1;
char* buf = (char*)malloc(sizeof(char) * len);
int flag = mxGetString(pm,buf,len);
if(flag) {
free(buf);
return SWIG_TypeError;
}

if (alloc) {
*cptr = %new_copy_array(buf, len + 1, char);
*cptr = %new_copy_array(buf, len, char);
*alloc = SWIG_NEWOBJ;
} else if (cptr)
free(buf);
} else if (cptr) {
*cptr = buf;
} else
free(buf);
if (psize)
*psize = len + 1;
*psize = len;
return SWIG_OK;
}
}
Expand Down