[Matlab] Always allocate string buffers of an appropriate size

Never use fixed size buffers which can force content to be truncated

Resolves #118.
This commit is contained in:
Ray Speth 2016-10-17 23:35:27 -04:00
parent 8a9eabeeac
commit 940b7ea01f
4 changed files with 41 additions and 20 deletions

View file

@ -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 {

View file

@ -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;

View file

@ -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 {

View file

@ -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");