GH #3876: Replace sprintf with snprintf in Environment and NumberFormatter to avoid deprecation warnings

This commit is contained in:
Günter Obiltschnig 2022-11-17 11:41:41 +01:00
parent 5a252fddcb
commit 4e8837db9e
4 changed files with 9 additions and 13 deletions

View File

@ -18,8 +18,8 @@
#include "Poco/Data/MySQL/MySQLException.h"
#include <mysql.h>
#include <stdio.h>
#include "Poco/NumberFormatter.h"
#include <mysql/mysql.h>
namespace Poco {
@ -71,9 +71,7 @@ std::string ConnectionException::compose(const std::string& text, MYSQL* h)
str += mysql_error(h);
str += "\t[mysql_errno]: ";
char buff[30];
sprintf(buff, "%d", mysql_errno(h));
str += buff;
Poco::NumberFormatter::append(str, mysql_errno(h));
str += "\t[mysql_sqlstate]: ";
str += mysql_sqlstate(h);
@ -125,9 +123,7 @@ std::string StatementException::compose(const std::string& text, MYSQL_STMT* h,
str += mysql_stmt_error(h);
str += "\t[mysql_stmt_errno]: ";
char buff[30];
sprintf(buff, "%d", mysql_stmt_errno(h));
str += buff;
Poco::NumberFormatter::append(str, mysql_stmt_errno(h));
str += "\t[mysql_stmt_sqlstate]: ";
str += mysql_stmt_sqlstate(h);

View File

@ -20,7 +20,7 @@
#include "Poco/Environment.h"
#include "Poco/Version.h"
#include <cstdlib>
#include <cstdio> // sprintf()
#include <cstdio> // snprintf()
#if defined(POCO_VXWORKS)
@ -101,7 +101,7 @@ std::string Environment::nodeId()
NodeId id;
nodeId(id);
char result[18];
std::sprintf(result, "%02x:%02x:%02x:%02x:%02x:%02x",
std::snprintf(result, sizeof(result), "%02x:%02x:%02x:%02x:%02x:%02x",
id[0],
id[1],
id[2],

View File

@ -477,9 +477,9 @@ void NumberFormatter::append(std::string& str, const void* ptr)
{
char buffer[24];
#if defined(POCO_PTR_IS_64_BIT)
std::sprintf(buffer, "%016" PRIXPTR, (UIntPtr) ptr);
std::snprintf(buffer, sizeof(buffer), "%016" PRIXPTR, (UIntPtr) ptr);
#else
std::sprintf(buffer, "%08" PRIXPTR, (UIntPtr) ptr);
std::snprintf(buffer, sizeof(buffer), "%08" PRIXPTR, (UIntPtr) ptr);
#endif
str.append(buffer);
}

View File

@ -1238,7 +1238,7 @@ void formatStream(double value, std::string& str)
void formatSprintf(double value, std::string& str)
{
char buffer[128];
std::sprintf(buffer, "%.*g", 16, value);
std::snprintf(buffer, sizeof(buffer), "%.*g", 16, value);
str = buffer;
}