Avoid disabling MSVC CRT security warnings globally

Use "_s" versions of the standard C functions if possible/simple to do
and disable deprecation warnings explicitly in a couple of places where
this could not be done instead of disabling them globally.

Also stop using strcpy() entirely and use strncpy() instead.

See #1268.
This commit is contained in:
Vadim Zeitlin
2025-06-08 02:15:26 +02:00
parent 85b15d24a6
commit c35b1b6a77
22 changed files with 87 additions and 48 deletions

View File

@@ -14,14 +14,6 @@ set(SOCI_LD "" CACHE STRING "Specify a non-default linker")
if (WIN32)
target_compile_definitions(soci_compiler_interface
INTERFACE
_CRT_SECURE_NO_DEPRECATE
_CRT_SECURE_NO_WARNINGS
_CRT_NONSTDC_NO_WARNING
_SCL_SECURE_NO_WARNINGS
)
# Prevent the Windows header files from defining annoying macros
# and also cut down on the definitions in general
target_compile_definitions(soci_compiler_interface

View File

@@ -51,7 +51,7 @@ double cstring_to_double(char const* s)
// don't seem to be used in practice, so for now keep things simple.
size_t const bufSize = strlen(s) + 1;
char* const buf = new char[bufSize];
strcpy(buf, s);
strncpy(buf, s, bufSize);
buf[end - s] = ',';
d = strtod(buf, &end);
parsedOK = end != buf && *end == '\0';

View File

@@ -23,7 +23,6 @@
#include <windows.h>
#endif
#include <sqlext.h> // ODBC
#include <string.h> // strcpy()
namespace soci
{

View File

@@ -8,11 +8,6 @@
#ifndef SOCI_PLATFORM_H_INCLUDED
#define SOCI_PLATFORM_H_INCLUDED
//disable MSVC deprecated warnings
#if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS)
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <stdarg.h>
#include <string.h>
#include <string>
@@ -121,4 +116,47 @@ private: \
#define SOCI_DUMMY_RETURN(x) return x
#endif
// Provide some wrappers for standard functions avoiding deprecation warnings
// from MSVC.
namespace soci
{
#ifdef _MSC_VER
inline const char* getenv(const char* name)
{
#pragma warning(push)
#pragma warning(disable:4996)
return std::getenv(name);
#pragma warning(pop)
}
inline int sscanf(const char* str, const char* format, ...)
{
va_list args;
va_start(args, format);
const int result = vsscanf_s(str, format, args);
va_end(args);
return result;
}
inline char* strncpy(char* dest, const char* src, size_t n)
{
strncpy_s(dest, n, src, _TRUNCATE);
return dest;
}
#else // !_MSC_VER
using std::getenv;
using std::sscanf;
using std::strncpy;
#endif // MSC_VER/!_MSC_VER
}
#endif // SOCI_PLATFORM_H_INCLUDED

View File

@@ -123,7 +123,7 @@ db2_session_backend::db2_session_backend(
// one of SQL_DRIVER_XXX constants but don't check for the exact value in
// case more of them are added in the future, the ODBC driver will return
// an error if we pass it an invalid value anyhow.
if (std::sscanf(completionString.c_str(), "%u", &completion) != 1)
if (soci::sscanf(completionString.c_str(), "%u", &completion) != 1)
{
throw soci_error("Invalid non-numeric driver completion option value \"" +
completionString + "\".");

View File

@@ -138,14 +138,14 @@ void setTextParam(char const * s, std::size_t size, char * buf_,
|| sqltype == SQL_TYPE_DATE)
{
unsigned short year, month, day, hour, min, sec;
if (std::sscanf(s, "%hu-%hu-%hu %hu:%hu:%hu",
if (soci::sscanf(s, "%hu-%hu-%hu %hu:%hu:%hu",
&year, &month, &day, &hour, &min, &sec) != 6)
{
if (std::sscanf(s, "%hu-%hu-%huT%hu:%hu:%hu",
if (soci::sscanf(s, "%hu-%hu-%huT%hu:%hu:%hu",
&year, &month, &day, &hour, &min, &sec) != 6)
{
hour = min = sec = 0;
if (std::sscanf(s, "%hu-%hu-%hu", &year, &month, &day) != 3)
if (soci::sscanf(s, "%hu-%hu-%hu", &year, &month, &day) != 3)
{
throw soci_error("Could not parse timestamp value.");
}
@@ -165,7 +165,7 @@ void setTextParam(char const * s, std::size_t size, char * buf_,
else if (sqltype == SQL_TYPE_TIME)
{
unsigned short hour, min, sec;
if (std::sscanf(s, "%hu:%hu:%hu", &hour, &min, &sec) != 3)
if (soci::sscanf(s, "%hu:%hu:%hu", &hour, &min, &sec) != 3)
{
throw soci_error("Could not parse timestamp value.");
}

View File

@@ -46,7 +46,7 @@ void mysql_standard_use_type_backend::pre_use(indicator const *ind)
if (ind != NULL && *ind == i_null)
{
buf_ = new char[5];
std::strcpy(buf_, "NULL");
strncpy(buf_, "NULL", 5);
}
else
{
@@ -145,7 +145,7 @@ void mysql_standard_use_type_backend::pre_use(indicator const *ind)
std::string const s = double_to_cstring(d);
buf_ = new char[s.size() + 1];
std::strcpy(buf_, s.c_str());
strncpy(buf_, s.c_str(), s.size() + 1);
}
break;
case x_stdtm:

View File

@@ -55,7 +55,7 @@ void mysql_vector_use_type_backend::pre_use(indicator const *ind)
if (ind != NULL && ind[i] == i_null)
{
buf = new char[5];
std::strcpy(buf, "NULL");
strncpy(buf, "NULL", 5);
}
else
{
@@ -195,7 +195,7 @@ void mysql_vector_use_type_backend::pre_use(indicator const *ind)
std::string const s = double_to_cstring(v[i]);
buf = new char[s.size() + 1];
std::strcpy(buf, s.c_str());
strncpy(buf, s.c_str(), s.size() + 1);
}
break;
case x_stdtm:

View File

@@ -82,10 +82,10 @@ odbc_soci_error::interpret_odbc_error(SQLSMALLINT htype,
if (socierror)
{
// Use our own error message if we failed to retrieve the ODBC one.
strcpy(reinterpret_cast<char*>(message_), socierror);
strncpy(reinterpret_cast<char*>(message_), socierror, sizeof(message_));
// Use "General warning" SQLSTATE code.
strcpy(reinterpret_cast<char*>(sqlstate_), "01000");
strncpy(reinterpret_cast<char*>(sqlstate_), "01000", sizeof(sqlstate_));
sqlcode_ = 0;
}

View File

@@ -70,7 +70,7 @@ odbc_session_backend::odbc_session_backend(
// one of SQL_DRIVER_XXX constants but don't check for the exact value in
// case more of them are added in the future, the ODBC driver will return
// an error if we pass it an invalid value anyhow.
if (std::sscanf(completionString.c_str(), "%u", &completion) != 1)
if (soci::sscanf(completionString.c_str(), "%u", &completion) != 1)
{
throw soci_error("Invalid non-numeric driver completion option value \"" +
completionString + "\".");
@@ -165,7 +165,7 @@ void odbc_session_backend::configure_connection()
// need to parse it fully, we just need the major version which,
// conveniently, comes first.
unsigned major_ver = 0;
if (std::sscanf(product_ver, "%u", &major_ver) != 1)
if (soci::sscanf(product_ver, "%u", &major_ver) != 1)
{
throw soci_error("DBMS version \"" + std::string(product_ver) +
"\" in unrecognizable format.");

View File

@@ -219,7 +219,7 @@ oracle_session_backend::oracle_session_backend(std::string const & serviceName,
nlsServiceLen = serviceName.size();
if (nlsServiceLen < serviceBufLen)
{
std::strcpy(nlsService, serviceName.c_str());
strncpy(nlsService, serviceName.c_str(), nlsServiceLen + 1);
}
else
{
@@ -229,7 +229,7 @@ oracle_session_backend::oracle_session_backend(std::string const & serviceName,
nlsUserNameLen = userName.size();
if (nlsUserNameLen < authBufLen)
{
std::strcpy(nlsUserName, userName.c_str());
strncpy(nlsUserName, userName.c_str(), nlsUserNameLen + 1);
}
else
{
@@ -239,7 +239,7 @@ oracle_session_backend::oracle_session_backend(std::string const & serviceName,
nlsPasswordLen = password.size();
if (nlsPasswordLen < authBufLen)
{
std::strcpy(nlsPassword, password.c_str());
strncpy(nlsPassword, password.c_str(), nlsPasswordLen + 1);
}
else
{

View File

@@ -244,5 +244,5 @@ void postgresql_standard_use_type_backend::clean_up()
void postgresql_standard_use_type_backend::copy_from_string(std::string const& s)
{
buf_ = new char[s.size() + 1];
std::strcpy(buf_, s.c_str());
strncpy(buf_, s.c_str(), s.size() + 1);
}

View File

@@ -99,7 +99,7 @@ void postgresql_vector_use_type_backend::pre_use(indicator const * ind)
std::vector<std::string> & v = *pv;
buf = new char[v[i].size() + 1];
std::strcpy(buf, v[i].c_str());
strncpy(buf, v[i].c_str(), v[i].size() + 1);
}
break;
case x_int8:
@@ -209,7 +209,7 @@ void postgresql_vector_use_type_backend::pre_use(indicator const * ind)
std::string const s = double_to_cstring(v[i]);
buf = new char[s.size() + 1];
std::strcpy(buf, s.c_str());
strncpy(buf, s.c_str(), s.size() + 1);
}
break;
case x_stdtm:
@@ -231,7 +231,7 @@ void postgresql_vector_use_type_backend::pre_use(indicator const * ind)
std::vector<xml_type> & v = *pv;
buf = new char[v[i].value.size() + 1];
std::strcpy(buf, v[i].value.c_str());
strncpy(buf, v[i].value.c_str(), v[i].value.size() + 1);
}
break;
case x_longstring:
@@ -241,7 +241,7 @@ void postgresql_vector_use_type_backend::pre_use(indicator const * ind)
std::vector<long_string> & v = *pv;
buf = new char[v[i].value.size() + 1];
std::strcpy(buf, v[i].value.c_str());
strncpy(buf, v[i].value.c_str(), v[i].value.size() + 1);
}
break;

View File

@@ -221,7 +221,7 @@ std::vector<std::string>& get_default_search_paths()
if (!paths.empty())
return paths;
char const* const penv = std::getenv("SOCI_BACKENDS_PATH");
char const* const penv = soci::getenv("SOCI_BACKENDS_PATH");
std::string const env(penv ? penv : "");
if (env.empty())
{

View File

@@ -897,7 +897,7 @@ bool string_to_date(char const * val, std::tm & /* out */ dt,
int hour;
int minute;
int second;
int const converted = std::sscanf(val, "%d %d %d %d %d %d",
int const converted = soci::sscanf(val, "%d %d %d %d %d %d",
&year, &month, &day, &hour, &minute, &second);
if (converted != 6)
{

View File

@@ -154,7 +154,7 @@ TEST_CASE_METHOD(common_tests, "Failover", "[keep-alive][.]")
// SOCI_TEST_SQL environment variable and examine the resulting error.
TEST_CASE_METHOD(common_tests, "Execute", "[.]")
{
auto const text = std::getenv("SOCI_TEST_SQL");
auto const text = soci::getenv("SOCI_TEST_SQL");
if (!text)
{
FAIL( "SOCI_TEST_SQL environment variable must be set." );

View File

@@ -144,7 +144,7 @@ TEST_CASE("Firebird char types", "[firebird][string]")
char buf[20];
std::string buf_str;
sql << "select p1 from test2", into(buf_str);
std::strcpy(buf, buf_str.c_str());
soci::strncpy(buf, buf_str.c_str(), sizeof(buf));
CHECK(std::strncmp(buf, msg, 5) == 0);
// This test works only for charset none

View File

@@ -38,8 +38,9 @@ public:
bool init_from_string(char const* s)
{
initialized_ = std::sscanf(s, "%u.%u.%u",
&major_, &minor_, &release_) == 3;
initialized_ = soci::sscanf(s, "%u.%u.%u",
&major_, &minor_, &release_) == 3;
return initialized_;
}

View File

@@ -1544,7 +1544,7 @@ public:
bool start_testing() override
{
if (!std::getenv("ORACLE_HOME"))
if (!soci::getenv("ORACLE_HOME"))
{
std::cerr << "ORACLE_HOME environment variable must be defined for Oracle tests.\n";
return false;

View File

@@ -632,7 +632,7 @@ server_version get_postgresql_version(soci::session& sql)
std::string version;
std::pair<int,int> result;
sql << "select version()",into(version);
if (sscanf(version.c_str(),"PostgreSQL %i.%i", &result.first, &result.second) < 2)
if (soci::sscanf(version.c_str(),"PostgreSQL %i.%i", &result.first, &result.second) < 2)
{
throw std::runtime_error("Failed to retrieve PostgreSQL version number");
}

View File

@@ -50,12 +50,12 @@ TEST_CASE("SQLite connection string", "[sqlite][connstring]")
// Finally allow testing arbitrary connection strings by specifying them in
// the environment variables.
if (auto const connstr = std::getenv("SOCI_TEST_CONNSTR_GOOD"))
if (auto const connstr = soci::getenv("SOCI_TEST_CONNSTR_GOOD"))
{
CHECK_NOTHROW(soci::session(backEnd, connstr));
}
if (auto const connstr = std::getenv("SOCI_TEST_CONNSTR_BAD"))
if (auto const connstr = soci::getenv("SOCI_TEST_CONNSTR_BAD"))
{
CHECK_THROWS_AS(soci::session(backEnd, connstr), soci_error);
}
@@ -877,7 +877,14 @@ TEST_CASE("SQLite std::tm bind", "[sqlite][std-tm-bind]")
std::time_t datetimeEpoch = 1491307200; // 2017-04-04 12:00:00
std::tm datetime = *std::gmtime(&datetimeEpoch);
std::tm datetime;
#ifdef _MSC_VER
gmtime_s(&datetime, &datetimeEpoch);
#else
datetime = *std::gmtime(&datetimeEpoch);
#endif
soci::rowset<std::tm> rs = (sql.prepare << "select date from soci_test where date=:dt", soci::use(datetime));
std::vector<std::tm> result;

View File

@@ -10,6 +10,8 @@
#include "soci/soci.h"
#include "soci-compiler.h"
#include <cassert>
#include <clocale>
#include <cstdlib>
@@ -127,7 +129,7 @@ public:
// environment variable can be set and then the current default locale
// (which can itself be changed by setting LC_ALL environment variable)
// will then be used.
if (std::getenv("SOCI_TEST_USE_LC_ALL"))
if (soci::getenv("SOCI_TEST_USE_LC_ALL"))
std::setlocale(LC_ALL, "");
return true;