Skip to content

Commit

Permalink
Fixed sprintf deprecation warnings by using snprintf
Browse files Browse the repository at this point in the history
  • Loading branch information
gateshian committed Apr 21, 2024
1 parent cccf8cc commit 26a430e
Show file tree
Hide file tree
Showing 29 changed files with 74 additions and 74 deletions.
18 changes: 9 additions & 9 deletions FEBio/FEBioApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ bool FEBioApp::ParseCmdLine(int nargs, char* argv[])
{
char szpath[1024] = { 0 };
febio::get_app_path(szpath, 1023);
sprintf(ops.szcnf, "%sfebio.xml", szpath);
snprintf(ops.szcnf, 1024, "%sfebio.xml", szpath);
}

// loop over the arguments
Expand Down Expand Up @@ -337,7 +337,7 @@ bool FEBioApp::ParseCmdLine(int nargs, char* argv[])
if (szext == 0)
{
// we assume a default extension of .feb if none is provided
sprintf(ops.szfile, "%s.feb", argv[i]);
snprintf(ops.szfile, sizeof(ops.szfile), "%s.feb", argv[i]);
}
else strcpy(ops.szfile, argv[i]);
ops.binteractive = false;
Expand Down Expand Up @@ -448,7 +448,7 @@ bool FEBioApp::ParseCmdLine(int nargs, char* argv[])
if (szext == 0)
{
// we assume a default extension of .feb if none is provided
sprintf(ops.szfile, "%s.feb", sz);
snprintf(ops.szfile, sizeof(ops.szfile), "%s.feb", sz);
}
else
{
Expand Down Expand Up @@ -494,9 +494,9 @@ bool FEBioApp::ParseCmdLine(int nargs, char* argv[])
}
else strcpy(szlogbase, szbase);

if (!blog) sprintf(ops.szlog, "%s.log", szlogbase);
if (!bplt) sprintf(ops.szplt, "%s.xplt", szbase);
if (!bdmp) sprintf(ops.szdmp, "%s.dmp", szbase);
if (!blog) snprintf(ops.szlog, sizeof(ops.szlog), "%s.log", szlogbase);
if (!bplt) snprintf(ops.szplt, sizeof(ops.szplt), "%s.xplt", szbase);
if (!bdmp) snprintf(ops.szdmp, sizeof(ops.szdmp), "%s.dmp", szbase);
}
else if (ops.szctrl[0])
{
Expand All @@ -505,9 +505,9 @@ bool FEBioApp::ParseCmdLine(int nargs, char* argv[])
char* ch = strrchr(szbase, '.');
if (ch) *ch = 0;

if (!blog) sprintf(ops.szlog, "%s.log", szbase);
if (!bplt) sprintf(ops.szplt, "%s.xplt", szbase);
if (!bdmp) sprintf(ops.szdmp, "%s.dmp", szbase);
if (!blog) snprintf(ops.szlog, sizeof(ops.szlog), "%s.log", szbase);
if (!bplt) snprintf(ops.szplt, sizeof(ops.szplt), "%s.xplt", szbase);
if (!bdmp) snprintf(ops.szdmp, sizeof(ops.szdmp), "%s.dmp", szbase);
}

return brun;
Expand Down
8 changes: 4 additions & 4 deletions FEBio/FEBioCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ int FEBioCmd_Config::run(int nargs, char* argv[])
}
else if (nargs == 2)
{
sprintf(ops.szcnf, "%s", argv[1]);
snprintf(ops.szcnf, sizeof(ops.szcnf), "%s", argv[1]);
feApp->Configure(ops.szcnf);
}
else return invalid_nr_args();
Expand Down Expand Up @@ -510,7 +510,7 @@ int FEBioCmd_svg::run(int nargs, char **argv)
strcpy(buf, szfile);
char* ch = strrchr(buf, '.');
if (ch) *ch = 0;
sprintf(szsvg, "%s.svg", buf);
snprintf(szsvg, 1024, "%s.svg", buf);

std::filebuf fb;
fb.open(szsvg, std::ios::out);
Expand Down Expand Up @@ -547,8 +547,8 @@ int FEBioCmd_out::run(int nargs, char **argv)
strcpy(buf, szfile);
char* ch = strrchr(buf, '.');
if (ch) *ch = 0;
sprintf(szK, "%s.out", buf);
sprintf(szR, "%s_rhs.out", buf);
snprintf(szK, 1024, "%s.out", buf);
snprintf(szR, 1024, "%s_rhs.out", buf);

febio::write_hb(*A, szK, mode);
febio::write_vector(R, szR, mode);
Expand Down
2 changes: 1 addition & 1 deletion FEBio/console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ void Console::SetTitle(const char* sz, ...)
// make the message
char sztitle[512];
va_start(args, sz);
vsprintf(sztitle, sz, args);
vsnprintf(sztitle, 512, sz, args);
va_end(args);

#ifdef WIN32
Expand Down
4 changes: 2 additions & 2 deletions FEBio/febio_cb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ bool update_console_cb(FEModel* pfem, unsigned int nwhen, void* pd)

char szvers[64] = {0};
#ifndef NDEBUG
sprintf(szvers, "FEBio (DEBUG) %s", szver);
snprintf(szvers, 64, "FEBio (DEBUG) %s", szver);
#else
sprintf(szvers, "FEBio %s", szver);
snprintf(szvers, 64, "FEBio %s", szver);
#endif

// print progress in title bar
Expand Down
4 changes: 2 additions & 2 deletions FEBioLib/FEBioModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1388,7 +1388,7 @@ void FEBioModel::SerializeIOData(DumpStream &ar)
// set the software string
const char* szver = febio::getVersionString();
char szbuf[256] = { 0 };
sprintf(szbuf, "FEBio %s", szver);
snprintf(szbuf, 256, "FEBio %s", szver);
xplt->SetSoftwareString(szbuf);

m_plot = xplt;
Expand Down Expand Up @@ -1480,7 +1480,7 @@ bool FEBioModel::InitPlotFile()
// set the software string
const char* szver = febio::getVersionString();
char szbuf[256] = { 0 };
sprintf(szbuf, "FEBio %s", szver);
snprintf(szbuf, 256, "FEBio %s", szver);
xplt->SetSoftwareString(szbuf);

m_plot = xplt;
Expand Down
2 changes: 1 addition & 1 deletion FEBioLib/LogStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void LogStream::printf(const char* sz, ...)
// make the message
char sztxt[1024] = { 0 };
va_start(args, sz);
vsprintf(sztxt, sz, args);
vsnprintf(sztxt, 1024, sz, args);
va_end(args);

print(sztxt);
Expand Down
16 changes: 8 additions & 8 deletions FEBioLib/Logfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ void Logfile::printf(const char* sz, ...)
// make the message
char sztxt[2048] = {0};
va_start(args, sz);
vsprintf(sztxt, sz, args);
vsnprintf(sztxt, 2048, sz, args);
va_end(args);

// print to file
Expand All @@ -123,13 +123,13 @@ void Logfile::printbox(const char* sztitle, const char* sz, ...)
// make the message
char sztxt[1024] = {0};
va_start(args, sz);
vsprintf(sztxt, sz, args);
vsnprintf(sztxt, 1024, sz, args);
va_end(args);

// print the box
char szmsg[1024] = {0};
char* ch = szmsg;
sprintf(szmsg,"\n *************************************************************************\n"); ch += strlen(ch);
snprintf(szmsg,1024, "\n *************************************************************************\n"); ch += strlen(ch);
// print the title
if (sztitle)
{
Expand All @@ -138,9 +138,9 @@ void Logfile::printbox(const char* sztitle, const char* sz, ...)
char right[60] = {0};
strncpy(left, sztitle, l/2);
strncpy(right, sztitle+l/2, l - l/2);
sprintf(ch," * %33s", left); ch += strlen(ch);
sprintf(ch,"%-36s *\n", right); ch += strlen(ch);
// sprintf(ch," *%71s*\n", ""); ch += strlen(ch);
snprintf(ch,1024, " * %33s", left); ch += strlen(ch);
snprintf(ch,1024, "%-36s *\n", right); ch += strlen(ch);
// snprintf(ch,1024, " *%71s*\n", ""); ch += strlen(ch);
}

// print the message
Expand All @@ -162,13 +162,13 @@ void Logfile::printbox(const char* sztitle, const char* sz, ...)
if (cn) *cn = '\n';
cn = ct + n;
}
sprintf(ch," * %-69s *\n", ct); ch += strlen(ch);
snprintf(ch,1024," * %-69s *\n", ct); ch += strlen(ch);
if (wrap) { ct[n] = tmp; }
if (cn) ct = cn+1;
}
while (cn);
// sprintf(ch," * *\n"); ch += strlen(ch);
sprintf(ch," *************************************************************************\n");
snprintf(ch,1024," *************************************************************************\n");

// print the message
printf(szmsg);
Expand Down
18 changes: 9 additions & 9 deletions FEBioLib/cmdoptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ bool febio::ProcessOptionsString(const std::string& s, CMDOPTIONS& ops)
{
char szpath[1024] = { 0 };
febio::get_app_path(szpath, 1023);
sprintf(ops.szcnf, "%sfebio.xml", szpath);
snprintf(ops.szcnf, 1024, "%sfebio.xml", szpath);
}

// loop over the arguments
Expand Down Expand Up @@ -151,7 +151,7 @@ bool febio::ProcessOptionsString(const std::string& s, CMDOPTIONS& ops)
if (szext == 0)
{
// we assume a default extension of .feb if none is provided
sprintf(ops.szfile, "%s.feb", sz);
snprintf(ops.szfile, 1024, "%s.feb", sz);
}
else strcpy(ops.szfile, sz);
ops.binteractive = false;
Expand Down Expand Up @@ -225,7 +225,7 @@ bool febio::ProcessOptionsString(const std::string& s, CMDOPTIONS& ops)
if (szext == 0)
{
// we assume a default extension of .feb if none is provided
sprintf(ops.szfile, "%s.feb", sz);
snprintf(ops.szfile, sizeof(ops.szfile), "%s.feb", sz);
}
else
{
Expand Down Expand Up @@ -271,9 +271,9 @@ bool febio::ProcessOptionsString(const std::string& s, CMDOPTIONS& ops)
}
else strcpy(szlogbase, szbase);

if (!blog) sprintf(ops.szlog, "%s.log", szlogbase);
if (!bplt) sprintf(ops.szplt, "%s.xplt", szbase);
if (!bdmp) sprintf(ops.szdmp, "%s.dmp", szbase);
if (!blog) snprintf(ops.szlog, sizeof(ops.szlog), "%s.log", szlogbase);
if (!bplt) snprintf(ops.szplt, sizeof(ops.szplt), "%s.xplt", szbase);
if (!bdmp) snprintf(ops.szdmp, sizeof(ops.szdmp), "%s.dmp", szbase);
}
else if (ops.szctrl[0])
{
Expand All @@ -282,9 +282,9 @@ bool febio::ProcessOptionsString(const std::string& s, CMDOPTIONS& ops)
char* ch = strrchr(szbase, '.');
if (ch) *ch = 0;

if (!blog) sprintf(ops.szlog, "%s.log", szbase);
if (!bplt) sprintf(ops.szplt, "%s.xplt", szbase);
if (!bdmp) sprintf(ops.szdmp, "%s.dmp", szbase);
if (!blog) snprintf(ops.szlog, sizeof(ops.szlog), "%s.log", szbase);
if (!bplt) snprintf(ops.szplt, sizeof(ops.szplt), "%s.xplt", szbase);
if (!bdmp) snprintf(ops.szdmp, sizeof(ops.szdmp), "%s.dmp", szbase);
}

return true;
Expand Down
4 changes: 2 additions & 2 deletions FEBioLib/hello.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ int febio::Hello(LogStream& log)
{
char szversion[128] = { 0 };
char* szvernum = getVersionString();
sprintf(szversion, " version %s\n", szvernum);
snprintf(szversion, 128, " version %s\n", szvernum);

log.print("===========================================================================\n");
log.print(" ________ _________ _______ __ _________ \n");
Expand All @@ -62,4 +62,4 @@ int febio::Hello(LogStream& log)

return 0;
}


2 changes: 1 addition & 1 deletion FEBioLib/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ void FEBioModel::print_parameter(FEParam& p, int level)
{
char sz[512] = {0};
int l = (int)strlen(p.name()) + 2*level;
sprintf(sz, "\t%*s %.*s", l, p.name(), 50-l, "..................................................");
snprintf(sz, 512, "\t%*s %.*s", l, p.name(), 50-l, "..................................................");
if (p.dim() == 1)
{
switch (p.type())
Expand Down
6 changes: 3 additions & 3 deletions FEBioLib/version.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ char* febio::getVersionString()
static char version[32];

#ifndef DEVCOMMIT
sprintf(version, "%d.%d.%d", VERSION, SUBVERSION, SUBSUBVERSION);
snprintf(version, 32, "%d.%d.%d", VERSION, SUBVERSION, SUBSUBVERSION);
#else
sprintf(version, "%d.%d.%d.%s", VERSION, SUBVERSION, SUBSUBVERSION, DEVCOMMIT);
snprintf(version, 32, "%d.%d.%d.%s", VERSION, SUBVERSION, SUBSUBVERSION, DEVCOMMIT);
#endif

return version;
}
}
2 changes: 1 addition & 1 deletion FEBioOpt/FEOptimize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ FEOptimize::FEOptimize(FEModel* pfem) : FECoreTask(pfem), m_opt(pfem)
bool FEOptimize::Init(const char* szfile)
{
char szversion[32] = { 0 };
sprintf(szversion, "version %d.%d", VERSION, SUB_VERSION);
snprintf(szversion, 32, "version %d.%d", VERSION, SUB_VERSION);
feLog("P A R A M E T E R O P T I M I Z A T I O N M O D U L E\n%s\n\n", szversion);

// read the data from the xml input file
Expand Down
2 changes: 1 addition & 1 deletion FEBioXML/FEBioGeometrySection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1043,7 +1043,7 @@ void FEBioGeometrySection25::ParsePartSection(XMLTag& tag)

// redirect input to another file
char xpath[256] = {0};
sprintf(xpath, "febio_spec/Geometry/Part[@name=%s]", szname);
snprintf(xpath, 256, "febio_spec/Geometry/Part[@name=%s]", szname);
XMLReader xml;
if (xml.Open(szfrom) == false) throw XMLReader::InvalidAttributeValue(tag, "from", szfrom);
XMLTag tag2;
Expand Down
2 changes: 1 addition & 1 deletion FEBioXML/FEBioGeometrySection3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ void FEBioGeometrySection3::ParsePartSection(XMLTag& tag)

// redirect input to another file
char xpath[256] = {0};
sprintf(xpath, "febio_spec/Geometry/Part[@name=%s]", szname);
snprintf(xpath, 256, "febio_spec/Geometry/Part[@name=%s]", szname);
XMLReader xml;
if (xml.Open(szfrom) == false) throw XMLReader::InvalidAttributeValue(tag, "from", szfrom);
XMLTag tag2;
Expand Down
8 changes: 4 additions & 4 deletions FEBioXML/FEBioImport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ bool FEBioImport::ReadFile(const char* szfile, bool broot)

// find the section we are looking for
char sz[512] = {0};
sprintf(sz, "febio_spec/%s", tag.Name());
snprintf(sz, 512, "febio_spec/%s", tag.Name());
if (xml2.FindTag(sz, tag2) == false) return errf("FATAL ERROR: Couldn't find %s section in file %s.\n\n", tag.Name(), szinc);

// parse the section
Expand Down Expand Up @@ -528,9 +528,9 @@ void FEBioImport::ParseVersion(XMLTag &tag)
}

//-----------------------------------------------------------------------------
void FEBioImport::SetDumpfileName(const char* sz) { sprintf(m_szdmp, "%s", sz); }
void FEBioImport::SetLogfileName (const char* sz) { sprintf(m_szlog, "%s", sz); }
void FEBioImport::SetPlotfileName(const char* sz) { sprintf(m_szplt, "%s", sz); }
void FEBioImport::SetDumpfileName(const char* sz) { snprintf(m_szdmp, sizeof(m_szdmp), "%s", sz); }
void FEBioImport::SetLogfileName (const char* sz) { snprintf(m_szlog, sizeof(m_szlog), "%s", sz); }
void FEBioImport::SetPlotfileName(const char* sz) { snprintf(m_szplt, sizeof(m_szplt), "%s", sz); }

//-----------------------------------------------------------------------------
void FEBioImport::AddDataRecord(DataRecord* pd)
Expand Down
2 changes: 1 addition & 1 deletion FEBioXML/FEBioIncludeSection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void FEBioIncludeSection::Parse(XMLTag& tag)
if (ch==0)
{
// pre-pend the name with the input path
sprintf(szin, "%s%s", GetFileReader()->GetFilePath(), tag.szvalue());
snprintf(szin, 512, "%s%s", GetFileReader()->GetFilePath(), tag.szvalue());
}

// read the file
Expand Down
6 changes: 3 additions & 3 deletions FEBioXML/FEBioOutputSection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ void FEBioOutputSection::ParseLogfile(XMLTag &tag)
if(!FSPath::isPath(szlog))
{
char szfile[1024] = {0};
sprintf(szfile, "%s%s", szpath, szlog);
snprintf(szfile, 1024, "%s%s", szpath, szlog);
GetFEBioImport()->SetLogfileName(szfile);
}
else
Expand All @@ -126,7 +126,7 @@ void FEBioOutputSection::ParseLogfile(XMLTag &tag)
// if we have a path, prepend the path's name
if (szpath && szpath[0])
{
sprintf(szfilename, "%s%s", szpath, szfile);
snprintf(szfilename, 1024, "%s%s", szpath, szfile);
}
else strcpy(szfilename, szfile);
szfile = szfilename;
Expand Down Expand Up @@ -317,7 +317,7 @@ void FEBioOutputSection::ParsePlotfile(XMLTag &tag)
const char* szpath = GetFileReader()->GetFilePath();

char szfile[1024] = {0};
sprintf(szfile, "%s%s", szpath, szplt);
snprintf(szfile, 1024, "%s%s", szpath, szplt);
GetFEBioImport()->SetPlotfileName(szfile);
}
else
Expand Down
6 changes: 3 additions & 3 deletions FEBioXML/FileImport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ FEFileException::FEFileException(const char* sz, ...)

// make the message
va_start(args, sz);
vsprintf(m_szerr, sz, args);
vsnprintf(m_szerr, 2048, sz, args);
va_end(args);
}

Expand All @@ -167,7 +167,7 @@ void FEFileException::SetErrorString(const char* sz, ...)

// make the message
va_start(args, sz);
vsprintf(m_szerr, sz, args);
vsnprintf(m_szerr, sizeof(m_szerr), sz, args);
va_end(args);
}

Expand Down Expand Up @@ -1530,7 +1530,7 @@ bool FEFileImport::errf(const char* szerr, ...)

// copy to string
va_start(args, szerr);
vsprintf(m_szerr, szerr, args);
vsnprintf(m_szerr, sizeof(m_szerr), szerr, args);
va_end(args);

// close the file
Expand Down
2 changes: 1 addition & 1 deletion FECore/FEException.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ void FEException::what(const char* msg, ...)
// make the message
char sztxt[1024] = { 0 };
va_start(args, msg);
vsprintf(sztxt, msg, args);
vsnprintf(sztxt, 1024, msg, args);
va_end(args);

m_what = sztxt;
Expand Down
Loading

0 comments on commit 26a430e

Please sign in to comment.