Skip to content

Commit

Permalink
[Matlab] Always allocate string buffers of an appropriate size
Browse files Browse the repository at this point in the history
Never use fixed size buffers which can force content to be truncated

Resolves #118.
  • Loading branch information
speth committed Oct 18, 2016
1 parent 8a9eabe commit 940b7ea
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 20 deletions.
11 changes: 7 additions & 4 deletions src/matlab/kineticsmethods.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,16 +129,19 @@ void kineticsmethods(int nlhs, mxArray* plhs[],
}
} else if (job < 40) {
char* buf;
int iok = -1, buflen = 80;
int iok = -1, buflen;
switch (job) {
case 31:
buf = (char*)mxCalloc(buflen, sizeof(char));
iok = kin_getReactionString(kin, irxn-1, buflen, buf);
buflen = kin_getReactionString(kin, irxn-1, 0, 0);
if (buflen > 0) {
buf = (char*) mxCalloc(buflen, sizeof(char));
iok = kin_getReactionString(kin, irxn-1, buflen, buf);
}
break;
default:
;
}
if (iok >= 0) {
if (iok == 0) {
plhs[0] = mxCreateString(buf);
return;
} else {
Expand Down
8 changes: 5 additions & 3 deletions src/matlab/onedimmethods.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,11 @@ void onedimmethods(int nlhs, mxArray* plhs[],
switch (job) {
case 40:
icomp = getInt(prhs[3]) - 1;
buflen = 40;
output_buf = (char*)mxCalloc(buflen, sizeof(char));
iok = domain_componentName(dom, icomp, buflen, output_buf);
buflen = domain_componentName(dom, icomp, 0, 0);
if (buflen > 0) {
output_buf = (char*) mxCalloc(buflen, sizeof(char));
iok = domain_componentName(dom, icomp, buflen, output_buf);
}
break;
default:
iok = -1;
Expand Down
26 changes: 16 additions & 10 deletions src/matlab/phasemethods.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,25 +246,31 @@ void phasemethods(int nlhs, mxArray* plhs[],
switch (job) {
case 40:
ksp = getInt(prhs[3]);
buflen = 40;
output_buf = (char*)mxCalloc(buflen, sizeof(char));
iok = thermo_getSpeciesName(ph, ksp-1, buflen, output_buf);
buflen = thermo_getSpeciesName(ph, ksp-1, 0, 0);
if (buflen > 0) {
output_buf = (char*)mxCalloc(buflen, sizeof(char));
iok = thermo_getSpeciesName(ph, ksp-1, buflen, output_buf);
}
break;
case 41:
mel = getInt(prhs[3]);
buflen = 40;
output_buf = (char*)mxCalloc(buflen, sizeof(char));
iok = thermo_getElementName(ph, mel-1, buflen, output_buf);
buflen = thermo_getElementName(ph, mel-1, 0, 0);
if (buflen > 0) {
output_buf = (char*)mxCalloc(buflen, sizeof(char));
iok = thermo_getElementName(ph, mel-1, buflen, output_buf);
}
break;
case 42:
buflen = 40;
output_buf = (char*)mxCalloc(buflen, sizeof(char));
iok = thermo_getName(ph, buflen, output_buf);
buflen = thermo_getName(ph, 0, 0);
if (buflen > 0) {
output_buf = (char*)mxCalloc(buflen, sizeof(char));
iok = thermo_getName(ph, buflen, output_buf);
}
break;
default:
iok = -1;
}
if (iok >= 0) {
if (iok == 0) {
plhs[0] = mxCreateString(output_buf);
return;
} else {
Expand Down
16 changes: 13 additions & 3 deletions src/matlab/xmlmethods.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,26 @@ void xmlmethods(int nlhs, mxArray* plhs[],
}

// options that return strings
char* v = (char*)mxCalloc(80, sizeof(char));
char* v;
int buflen;
iok = -1;
switch (job) {
case 20:
// return an attribute
key = getString(prhs[3]);
iok = xml_attrib(i, key, 80, v);
buflen = xml_attrib(i, key, 0, 0);
if (buflen > 0) {
v = (char*) mxCalloc(buflen, sizeof(char));
iok = xml_attrib(i, key, buflen, v);
}
break;
case 21:
// return the value of the node
iok = xml_value(i, 80, v);
buflen = xml_value(i, 0, 0);
if (buflen > 0) {
v = (char*) mxCalloc(buflen, sizeof(char));
iok = xml_value(i, buflen, v);
}
break;
default:
mexErrMsgTxt("unknown job parameter");
Expand Down

0 comments on commit 940b7ea

Please sign in to comment.