From 0b08ea33681bc717ed88993f034912e0215b2db8 Mon Sep 17 00:00:00 2001 From: wmayer Date: Wed, 3 Apr 2024 07:29:10 +0200 Subject: [PATCH] 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 --- src/Base/Parameter.cpp | 17 ++++++++--------- src/Mod/Drawing/Gui/TaskOrthoViews.cpp | 5 +++-- src/Mod/Mesh/App/WildMagic4/Wm4System.cpp | 2 +- tests/src/Misc/fmt.cpp | 6 ++++++ 4 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/Base/Parameter.cpp b/src/Base/Parameter.cpp index 54a33bf907..95dba60791 100644 --- a/src/Base/Parameter.cpp +++ b/src/Base/Parameter.cpp @@ -46,6 +46,7 @@ #endif #include +#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 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 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 ParameterGrp::GetFloats(const char* sFilter) const diff --git a/src/Mod/Drawing/Gui/TaskOrthoViews.cpp b/src/Mod/Drawing/Gui/TaskOrthoViews.cpp index f19c92a064..140ffc4218 100644 --- a/src/Mod/Drawing/Gui/TaskOrthoViews.cpp +++ b/src/Mod/Drawing/Gui/TaskOrthoViews.cpp @@ -26,6 +26,7 @@ #include #endif +#include #include #include #include @@ -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); diff --git a/src/Mod/Mesh/App/WildMagic4/Wm4System.cpp b/src/Mod/Mesh/App/WildMagic4/Wm4System.cpp index 5826665825..7370cc7aae 100644 --- a/src/Mod/Mesh/App/WildMagic4/Wm4System.cpp +++ b/src/Mod/Mesh/App/WildMagic4/Wm4System.cpp @@ -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); diff --git a/tests/src/Misc/fmt.cpp b/tests/src/Misc/fmt.cpp index 5354703906..2a6415e30f 100644 --- a/tests/src/Misc/fmt.cpp +++ b/tests/src/Misc/fmt.cpp @@ -3,6 +3,7 @@ #include "gtest/gtest.h" #include +// 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