From 940b7ea01f38848cdcbd991f91528169e195b164 Mon Sep 17 00:00:00 2001 From: Ray Speth Date: Mon, 17 Oct 2016 23:35:27 -0400 Subject: [PATCH] [Matlab] Always allocate string buffers of an appropriate size Never use fixed size buffers which can force content to be truncated Resolves #118. --- src/matlab/kineticsmethods.cpp | 11 +++++++---- src/matlab/onedimmethods.cpp | 8 +++++--- src/matlab/phasemethods.cpp | 26 ++++++++++++++++---------- src/matlab/xmlmethods.cpp | 16 +++++++++++++--- 4 files changed, 41 insertions(+), 20 deletions(-) diff --git a/src/matlab/kineticsmethods.cpp b/src/matlab/kineticsmethods.cpp index 50255e7b64..99d4a9e926 100644 --- a/src/matlab/kineticsmethods.cpp +++ b/src/matlab/kineticsmethods.cpp @@ -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 { diff --git a/src/matlab/onedimmethods.cpp b/src/matlab/onedimmethods.cpp index 96b1a62834..ea42b81988 100644 --- a/src/matlab/onedimmethods.cpp +++ b/src/matlab/onedimmethods.cpp @@ -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; diff --git a/src/matlab/phasemethods.cpp b/src/matlab/phasemethods.cpp index 040049583e..06653c7844 100644 --- a/src/matlab/phasemethods.cpp +++ b/src/matlab/phasemethods.cpp @@ -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 { diff --git a/src/matlab/xmlmethods.cpp b/src/matlab/xmlmethods.cpp index aa86284627..8b2d78e0ae 100644 --- a/src/matlab/xmlmethods.cpp +++ b/src/matlab/xmlmethods.cpp @@ -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");