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

Replace usages of sprintf with snprintf #3658

Merged
merged 2 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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
12 changes: 6 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,18 @@ void IO::
ConstructDoubleOutputFormat()
{
if(_GFormatForDoubleOutput) {
sprintf(_DoubleFormat,"%%g");
snprintf(_DoubleFormat, 256, "%%g");
} else if(_Scientific) {
if(_Pad<0) {
sprintf(_DoubleFormat,"%%.%dle",_Precision);
snprintf(_DoubleFormat, 256, "%%.%dle", _Precision);
} else {
sprintf(_DoubleFormat,"%%%d.%dle",_Pad+_Precision,_Precision);
snprintf(_DoubleFormat, 256, "%%%d.%dle", _Pad+_Precision, _Precision);
}
} else {
if(_Pad<0) {
sprintf(_DoubleFormat,"%%.%dlf",_Precision);
snprintf(_DoubleFormat, 256, "%%.%dlf", _Precision);
} else {
sprintf(_DoubleFormat,"%%%d.%dlf",_Pad+_Precision,_Precision);
snprintf(_DoubleFormat, 256, "%%%d.%dlf", _Pad+_Precision, _Precision);
}
}
}
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