C++: replace deprecated vsprintf and sprintf

With future C++ compilers the methods vsprintf and sprintf are declared as deprecated due to security issues.
They are replaced with the secure counterpart of the fmt library

Fixes the compiler warnings -Wdeprecated-declarations
This commit is contained in:
wmayer
2024-04-03 07:29:10 +02:00
committed by wwmayer
parent d8a194a70b
commit 0b08ea3368
4 changed files with 18 additions and 12 deletions

View File

@@ -46,6 +46,7 @@
#endif
#include <boost/algorithm/string.hpp>
#include "fmt/printf.h"
#include "Parameter.h"
#include "Parameter.inl"
@@ -805,9 +806,8 @@ long ParameterGrp::GetInt(const char* Name, long lPreset) const
void ParameterGrp::SetInt(const char* Name, long lValue)
{
char cBuf[256];
sprintf(cBuf, "%li", lValue);
_SetAttribute(ParamType::FCInt, Name, cBuf);
std::string buf = fmt::sprintf("%li", lValue);
_SetAttribute(ParamType::FCInt, Name, buf.c_str());
}
std::vector<long> ParameterGrp::GetInts(const char* sFilter) const
@@ -875,9 +875,8 @@ unsigned long ParameterGrp::GetUnsigned(const char* Name, unsigned long lPreset)
void ParameterGrp::SetUnsigned(const char* Name, unsigned long lValue)
{
char cBuf[256];
sprintf(cBuf, "%lu", lValue);
_SetAttribute(ParamType::FCUInt, Name, cBuf);
std::string buf = fmt::sprintf("%lu", lValue);
_SetAttribute(ParamType::FCUInt, Name, buf.c_str());
}
std::vector<unsigned long> ParameterGrp::GetUnsigneds(const char* sFilter) const
@@ -950,9 +949,9 @@ double ParameterGrp::GetFloat(const char* Name, double dPreset) const
void ParameterGrp::SetFloat(const char* Name, double dValue)
{
char cBuf[256];
sprintf(cBuf, "%.12f", dValue); // use %.12f instead of %f to handle values < 1.0e-6
_SetAttribute(ParamType::FCFloat, Name, cBuf);
// use %.12f instead of %f to handle values < 1.0e-6
std::string buf = fmt::sprintf("%.12f", dValue);
_SetAttribute(ParamType::FCFloat, Name, buf.c_str());
}
std::vector<double> ParameterGrp::GetFloats(const char* sFilter) const

View File

@@ -26,6 +26,7 @@
#include <QMenu>
#endif
#include <fmt/printf.h>
#include <App/Document.h>
#include <Gui/Application.h>
#include <Gui/BitmapFactory.h>
@@ -176,8 +177,8 @@ void orthoview::set_data(int r_x, int r_y)
rel_x = r_x;
rel_y = r_y;
char label[15];
sprintf(label, "Ortho_%i_%i", rel_x, rel_y); // label name for view, based on relative position
// label name for view, based on relative position
std::string label = fmt::sprintf("Ortho_%i_%i", rel_x, rel_y);
this_view->Label.setValue(label);
ortho = ((rel_x * rel_y) == 0);

View File

@@ -893,7 +893,7 @@ int System::Sprintf (char* acDst, size_t uiDstSize, const char* acFormat, ...)
#ifdef WM4_USING_VC80
int iNumWritten = vsprintf_s(acDst,uiDstSize,acFormat,acArgs);
#else
int iNumWritten = vsprintf(acDst,acFormat,acArgs);
int iNumWritten = vsnprintf(acDst,uiDstSize,acFormat,acArgs);
#endif
va_end(acArgs);

View File

@@ -3,6 +3,7 @@
#include "gtest/gtest.h"
#include <stdexcept>
// NOLINTBEGIN
TEST(fmt, fail)
{
EXPECT_NE("abc", fmt::format("{}{}", "a", "b"));
@@ -18,9 +19,14 @@ TEST(fmt, print_pass)
EXPECT_EQ("12", fmt::sprintf("%s%d", "1", 2));
EXPECT_EQ("x", fmt::sprintf("%c", 'x'));
EXPECT_EQ("1.23 2", fmt::sprintf("%.2f %d", 1.23456, 2));
EXPECT_EQ("0.123456789012", fmt::sprintf("%.12f", 0.123456789012));
EXPECT_EQ("-1234", fmt::sprintf("%li", -1234L));
EXPECT_EQ("-1234567890", fmt::sprintf("%li", -1234567890L));
EXPECT_EQ("1234567890", fmt::sprintf("%li", 1234567890UL));
}
TEST(fmt, print_fail)
{
EXPECT_THROW(fmt::printf("%s%d", 1, 2), std::exception);
}
// NOLINTEND