Skip to content

Commit

Permalink
Replace usages of sprintf with snprintf (#3658)
Browse files Browse the repository at this point in the history
* Replace usages of sprintf with snprintf

* Add constexpr for _DoubleFormat length
  • Loading branch information
nickbianco authored Dec 20, 2023
1 parent ecdfba1 commit 88da27e
Show file tree
Hide file tree
Showing 12 changed files with 26 additions and 20 deletions.
4 changes: 2 additions & 2 deletions OpenSim/Analyses/PointKinematics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,12 @@ constructDescription()
strcat(descrip,"(position, velocity, or acceleration) of\n");

if(_relativeToBody){
sprintf(tmp,"point (%lf, %lf, %lf) on body %s relative to body %s of model %s.\n",
snprintf(tmp, BUFFER_LENGTH, "point (%lf, %lf, %lf) on body %s relative to body %s of model %s.\n",
_point[0],_point[1],_point[2],_body->getName().c_str(),
_relativeToBody->getName().c_str(), _model->getName().c_str());
}
else{
sprintf(tmp,"point (%lf, %lf, %lf) on the %s of model %s.\n",
snprintf(tmp, BUFFER_LENGTH, "point (%lf, %lf, %lf) on the %s of model %s.\n",
_point[0],_point[1],_point[2],_body->getName().c_str(),
_model->getName().c_str());
}
Expand Down
2 changes: 1 addition & 1 deletion OpenSim/Common/About.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ static const char* OpenSimVersion = GET_OSIM_VERSION;

std::string GetVersionAndDate() {
char buffer[256];
sprintf(buffer,"version %s, build date %s %s",
snprintf(buffer, 256, "version %s, build date %s %s",
OpenSimVersion, __TIME__, __DATE__);
return std::string(buffer);
}
Expand Down
16 changes: 10 additions & 6 deletions OpenSim/Common/IO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ ConstructDateAndTimeStamp()

// CONSTRUCT STAMP
char *stamp = new char[64];
sprintf(stamp,"%d%02d%02d_%02d%02d%02d",
snprintf(stamp, 64, "%d%02d%02d_%02d%02d%02d",
timeStruct->tm_year+1900,timeStruct->tm_mon+1,timeStruct->tm_mday,
timeStruct->tm_hour,timeStruct->tm_min,timeStruct->tm_sec);

Expand Down Expand Up @@ -282,18 +282,22 @@ void IO::
ConstructDoubleOutputFormat()
{
if(_GFormatForDoubleOutput) {
sprintf(_DoubleFormat,"%%g");
snprintf(_DoubleFormat, IO_DBLFMTLEN, "%%g");
} else if(_Scientific) {
if(_Pad<0) {
sprintf(_DoubleFormat,"%%.%dle",_Precision);
snprintf(_DoubleFormat, IO_DBLFMTLEN,
"%%.%dle", _Precision);
} else {
sprintf(_DoubleFormat,"%%%d.%dle",_Pad+_Precision,_Precision);
snprintf(_DoubleFormat, IO_DBLFMTLEN,
"%%%d.%dle", _Pad+_Precision, _Precision);
}
} else {
if(_Pad<0) {
sprintf(_DoubleFormat,"%%.%dlf",_Precision);
snprintf(_DoubleFormat, IO_DBLFMTLEN,
"%%.%dlf", _Precision);
} else {
sprintf(_DoubleFormat,"%%%d.%dlf",_Pad+_Precision,_Precision);
snprintf(_DoubleFormat, IO_DBLFMTLEN,
"%%%d.%dlf", _Pad+_Precision, _Precision);
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions OpenSim/Common/IO.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
#include <vector>

// DEFINES
const int IO_STRLEN = 2048;
constexpr int IO_STRLEN = 2048;
constexpr int IO_DBLFMTLEN = 256;


namespace OpenSim {
Expand Down Expand Up @@ -62,7 +63,7 @@ class OSIMCOMMON_API IO {
/** Specifies the precision of number output. */
static int _Precision;
/** The output format string. */
static char _DoubleFormat[256];
static char _DoubleFormat[IO_DBLFMTLEN];
/** Whether offline documents should also be printed when Object::print is called. */
static bool _PrintOfflineDocuments;

Expand Down
2 changes: 1 addition & 1 deletion OpenSim/Common/PropertyDbl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ toString() const
{
if (SimTK::isFinite(_value)) {
char dbl[256];
sprintf(dbl, "%g", _value);
snprintf(dbl, 256, "%g", _value);
return dbl;
}

Expand Down
2 changes: 1 addition & 1 deletion OpenSim/Common/PropertyDblArray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ toString() const
string str = "(";
char dbl[256];
for(int i=0; i < _array.getSize(); i++){
sprintf(dbl, "%g", _array[i]);
snprintf(dbl, 256, "%g", _array[i]);
str += (i>0?" ":"") + string(dbl);
}
str += ")";
Expand Down
2 changes: 1 addition & 1 deletion OpenSim/Common/PropertyInt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,6 @@ string PropertyInt::
toString() const
{
char intString[32];
sprintf(intString, "%d", _value);
snprintf(intString, 32, "%d", _value);
return intString;
}
2 changes: 1 addition & 1 deletion OpenSim/Common/PropertyIntArray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ toString() const
string str = "(";
char intString[256];
for(int i=0; i < _array.getSize(); i++){
sprintf(intString, "%d", _array[i]);
snprintf(intString, 256, "%d", _array[i]);
str += (i>0?" ":"") + string(intString);
}
str += ")";
Expand Down
4 changes: 2 additions & 2 deletions OpenSim/Common/StateVector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ print(FILE *fp) const

// TIME
char format[IO_STRLEN];
sprintf(format,"%s",IO::GetDoubleOutputFormat());
snprintf(format, IO_STRLEN, "%s", IO::GetDoubleOutputFormat());
int n=0,nTotal=0;
n = fprintf(fp,format,_t);
if(n<0) {
Expand All @@ -553,7 +553,7 @@ print(FILE *fp) const
nTotal += n;

// STATES
sprintf(format,"\t%s",IO::GetDoubleOutputFormat());
snprintf(format, IO_STRLEN, "\t%s", IO::GetDoubleOutputFormat());
for(int i=0;i<_data.getSize();i++) {
n = fprintf(fp,format,_data[i]);
if(n<0) {
Expand Down
2 changes: 1 addition & 1 deletion OpenSim/Common/Storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3404,7 +3404,7 @@ bool Storage::makeStorageLabelsUnique() {
int c = 1;
while (exist) {
char cString[20];
sprintf(cString,"%d", c);
snprintf(cString, 20, "%d", c);
newName = std::string(cString) + "_" + offending;
exist = (lbls.findIndex(newName) != -1);
c++;
Expand Down
2 changes: 1 addition & 1 deletion OpenSim/Common/XMLDocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ getVersionAsString(const int aVersion, std::string& aString)
for(int i=0; i<3; i++)
{
int digits = ver / div;
sprintf(pad, "%02d",digits);
snprintf(pad, 3, "%02d", digits);
ver -= div*(ver / div);
div /=100;
aString += string(pad);
Expand Down
3 changes: 2 additions & 1 deletion OpenSim/Simulation/Model/AbstractTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,8 @@ std::string AbstractTool::createExternalLoadsFile(const std::string& oldFile,
ExternalForce* xf = new ExternalForce();
xf->setAppliedToBodyName((f==0)?body1:body2);
char pad[3];
sprintf(pad,"%d", f+1);
snprintf(pad, 3, "%d", f+1);

std::string suffix = "ExternalForce_"+string(pad);
xf->setName(suffix);
_externalLoads.adoptAndAppend(xf);
Expand Down

0 comments on commit 88da27e

Please sign in to comment.