Skip to content

Commit

Permalink
Replace remaining occurrences of sprintf() with snprintf()
Browse files Browse the repository at this point in the history
This (unsafe) function is deprecated in macOS 12 SDK and using it
results in the warnings that are not worth suppressing -- instead, just
stop using it and always use snprintf(), even when we know that we can't
overflow the buffer.
  • Loading branch information
vadz committed Aug 19, 2024
1 parent 1c792dc commit b2172e4
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/backends/postgresql/session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ void postgresql_session_backend::clean_up()
std::string postgresql_session_backend::get_next_statement_name()
{
char nameBuf[20] = { 0 }; // arbitrary length
sprintf(nameBuf, "st_%d", ++statementCount_);
snprintf(nameBuf, sizeof(nameBuf), "st_%d", ++statementCount_);
return nameBuf;
}

Expand Down
6 changes: 4 additions & 2 deletions src/core/soci-simple.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -861,7 +861,8 @@ void resize_in_map(std::map<std::string, std::vector<T> > & m, int new_size)
// helper for formatting date values
char const * format_date(statement_wrapper & wrapper, std::tm const & d)
{
std::sprintf(wrapper.date_formatted, "%d %d %d %d %d %d",
snprintf(wrapper.date_formatted, sizeof(wrapper.date_formatted),
"%d %d %d %d %d %d",
d.tm_year + 1900, d.tm_mon + 1, d.tm_mday,
d.tm_hour, d.tm_min, d.tm_sec);

Expand Down Expand Up @@ -2999,7 +3000,8 @@ SOCI_DECL char const * soci_get_use_date(statement_handle st, char const * name)

// format is: "YYYY MM DD hh mm ss"
std::tm const & d = wrapper->use_dates[name];
std::sprintf(wrapper->date_formatted, "%d %d %d %d %d %d",
snprintf(wrapper->date_formatted, sizeof(wrapper->date_formatted),
"%d %d %d %d %d %d",
d.tm_year + 1900, d.tm_mon + 1, d.tm_mday,
d.tm_hour, d.tm_min, d.tm_sec);

Expand Down
2 changes: 1 addition & 1 deletion tests/odbc/test-odbc-postgresql.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class odbc_version
char buf[128];
// This uses the ODBC convention of padding the minor and release
// versions with 0 and might be not appropriate in general.
std::sprintf(buf, "%u.%02u.%04u", major_, minor_, release_);
snprintf(buf, sizeof(buf), "%u.%02u.%04u", major_, minor_, release_);
return buf;
}
else
Expand Down
2 changes: 1 addition & 1 deletion tests/oracle/test-oracle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ TEST_CASE("Oracle datetime", "[oracle][datetime]")
for(int i = 100; i <= 2201; i = i + 50)
{
char t[10];
sprintf(t, "%04d", i);
snprintf(t, sizeof(t), "%04d", i);

std::string date = std::string(t) + "-03-28 14:06:13";
std::tm t1 {}, t2 {}, t4 {};
Expand Down

0 comments on commit b2172e4

Please sign in to comment.