Silence Clang deprecation warnings about sprintf.

This commit is contained in:
John Ralls 2023-01-23 12:34:17 -08:00
parent dd2497416c
commit 046e4a156e
9 changed files with 41 additions and 35 deletions

View File

@ -1927,7 +1927,7 @@ static gchar *report_create_jobname(GncPluginPageReportPrivate *priv)
if (report_name && job_date)
{
// Look up the sprintf format of the output name from the preferences database
// Look up the printf format of the output name from the preferences database
char* format = gnc_prefs_get_string(GNC_PREFS_GROUP_REPORT_PDFEXPORT, GNC_PREF_FILENAME_FMT);
if (format && *format)

View File

@ -736,7 +736,8 @@ gnc_html_encode_string(const char * str)
static gchar *safe = "$-._!*(),"; /* RFC 1738 */
unsigned pos = 0;
GString *encoded = g_string_new ("");
gchar buffer[5], *ptr;
static const size_t buf_size = 5;
gchar buffer[buf_size], *ptr;
guchar c;
if (!str) return NULL;
@ -762,7 +763,7 @@ gnc_html_encode_string(const char * str)
}
else if ( c != '\r' )
{
sprintf( buffer, "%%%02X", (int)c );
snprintf( buffer, buf_size, "%%%02X", (int)c );
encoded = g_string_append (encoded, buffer);
}
pos++;

View File

@ -1214,10 +1214,11 @@
static double
rnd (double x, unsigned places)
{
static const size_t buflen = 50; /* make buffer large enough */
double r;
char buf[50]; /* make buffer large enough */
char buf[buflen];
sprintf (buf, "%.*f", (int) places, x);
snprintf (buf, buflen, "%.*f", (int) places, x);
r = strtod(buf, NULL);
return r;

View File

@ -1105,7 +1105,8 @@ PrintAmountInternal(char *buf, gnc_numeric val, const GNCPrintAmountInfo *info)
{
struct lconv *lc = gnc_localeconv();
int num_whole_digits;
char temp_buf[128];
static const size_t buf_size = 128;
char temp_buf[buf_size];
gnc_numeric whole, rounding;
int min_dp, max_dp;
gboolean value_is_negative, value_is_decimal;
@ -1180,7 +1181,7 @@ PrintAmountInternal(char *buf, gnc_numeric val, const GNCPrintAmountInfo *info)
// Value may now be decimal, for example if the factional part is zero
value_is_decimal = gnc_numeric_to_decimal(&val, NULL);
/* print the integer part without separators */
sprintf(temp_buf, "%" G_GINT64_FORMAT, whole.num);
snprintf(temp_buf, buf_size, "%" G_GINT64_FORMAT, whole.num);
num_whole_digits = strlen (temp_buf);
if (!info->use_separators)
@ -1257,10 +1258,10 @@ PrintAmountInternal(char *buf, gnc_numeric val, const GNCPrintAmountInfo *info)
val = gnc_numeric_reduce (val);
if (val.denom > 0)
sprintf (temp_buf, "%" G_GINT64_FORMAT "/%" G_GINT64_FORMAT,
snprintf (temp_buf, buf_size, "%" G_GINT64_FORMAT "/%" G_GINT64_FORMAT,
val.num, val.denom);
else
sprintf (temp_buf, "%" G_GINT64_FORMAT " * %" G_GINT64_FORMAT,
snprintf (temp_buf, buf_size, "%" G_GINT64_FORMAT " * %" G_GINT64_FORMAT,
val.num, -val.denom);
if (whole.num == 0)

View File

@ -38,9 +38,9 @@ main (int argc, char** argv)
directory = "test-files/xml2";
}
char* filename = static_cast<decltype (filename)> (malloc (strlen (
directory) + 1 + strlen (FILENAME) + 1));
sprintf (filename, "%s/%s", directory, FILENAME);
auto size{strlen (directory) + 1 + strlen (FILENAME) + 1};
char* filename = static_cast<decltype (filename)> (malloc (size));
snprintf (filename, size, "%s/%s", directory, FILENAME);
do_test (gnc_is_xml_data_file_v2 (filename, NULL), "gnc_is_xml_data_file_v2");
print_test_results ();

View File

@ -916,21 +916,21 @@ decimal_from_binary (uint64_t d[dec_array_size], uint64_t hi, uint64_t lo)
static const uint8_t char_buf_size {41}; //39 digits plus sign and trailing null
char*
GncInt128::asCharBufR(char* buf) const noexcept
GncInt128::asCharBufR(char* buf, uint32_t size) const noexcept
{
if (isOverflow())
{
sprintf (buf, "%s", "Overflow");
snprintf (buf, size, "%s", "Overflow");
return buf;
}
if (isNan())
{
sprintf (buf, "%s", "NaN");
snprintf (buf, size, "%s", "NaN");
return buf;
}
if (isZero())
{
sprintf (buf, "%d", 0);
snprintf (buf, size, "%d", 0);
return buf;
}
uint64_t d[dec_array_size] {};
@ -943,10 +943,11 @@ GncInt128::asCharBufR(char* buf) const noexcept
for (unsigned int i {dec_array_size}; i; --i)
if (d[i - 1] || trailing)
{
uint32_t new_size = size - (next - buf);
if (trailing)
next += sprintf (next, "%8.8" PRIu64, d[i - 1]);
next += snprintf (next, new_size, "%8.8" PRIu64, d[i - 1]);
else
next += sprintf (next, "%" PRIu64, d[i - 1]);
next += snprintf (next, new_size, "%" PRIu64, d[i - 1]);
trailing = true;
}
@ -958,7 +959,7 @@ std::ostream&
operator<< (std::ostream& stream, const GncInt128& a) noexcept
{
char buf[char_buf_size] {};
stream << a.asCharBufR (buf);
stream << a.asCharBufR (buf, char_buf_size - 1);
return stream;
}

View File

@ -215,7 +215,7 @@ enum // Values for m_flags
* @param buf char[41], 39 digits plus sign and trailing 0.
* @return pointer to the buffer for convenience
*/
char* asCharBufR(char* buf) const noexcept;
char* asCharBufR(char* buf, uint32_t size) const noexcept;
GncInt128 abs() const noexcept;

View File

@ -34,6 +34,7 @@
#include <boost/locale/encoding_utf.hpp>
#include <config.h>
#include <stdint.h>
#include "qof.h"
#include "gnc-numeric.hpp"
@ -1197,11 +1198,11 @@ gnc_numeric_error(GNCNumericErrorCode error_code)
gchar *
gnc_numeric_to_string(gnc_numeric n)
{
gchar *result;
gint64 tmpnum = n.num;
gint64 tmpdenom = n.denom;
char *result;
int64_t tmpnum = n.num;
int64_t tmpdenom = n.denom;
result = g_strdup_printf("%" G_GINT64_FORMAT "/%" G_GINT64_FORMAT, tmpnum, tmpdenom);
result = g_strdup_printf("%" PRId64 "/%" PRId64, tmpnum, tmpdenom);
return result;
}
@ -1211,13 +1212,14 @@ gnc_num_dbg_to_string(gnc_numeric n)
{
static char buff[1000];
static char *p = buff;
gint64 tmpnum = n.num;
gint64 tmpdenom = n.denom;
static const uint64_t size = 50;
int64_t tmpnum = n.num;
int64_t tmpdenom = n.denom;
p += 100;
p += size;
if (p - buff >= 1000) p = buff;
sprintf(p, "%" G_GINT64_FORMAT "/%" G_GINT64_FORMAT, tmpnum, tmpdenom);
snprintf(p, size, "%" PRId64 "/%" PRId64, tmpnum, tmpdenom);
return p;
}

View File

@ -318,13 +318,13 @@ TEST(GncInt128_functions, stream_output)
static const uint8_t char_buf_size {41};
char buf[char_buf_size] {};
EXPECT_STREQ("567894392130486207", small.asCharBufR (buf));
EXPECT_STREQ("-567894392130486207", neg_small.asCharBufR (buf));
EXPECT_STREQ("5237901256262967342410779070006542271", really_big.asCharBufR (buf));
EXPECT_STREQ("-5237901256262967342410779070006542271", neg_really_big.asCharBufR (buf));
EXPECT_STREQ("36893488147419103231", boundary_value.asCharBufR (buf));
EXPECT_STREQ("Overflow", overflowed.asCharBufR (buf));
EXPECT_STREQ("NaN", not_a_number.asCharBufR (buf));
EXPECT_STREQ("567894392130486207", small.asCharBufR (buf, char_buf_size));
EXPECT_STREQ("-567894392130486207", neg_small.asCharBufR (buf, char_buf_size));
EXPECT_STREQ("5237901256262967342410779070006542271", really_big.asCharBufR (buf, char_buf_size));
EXPECT_STREQ("-5237901256262967342410779070006542271", neg_really_big.asCharBufR (buf, char_buf_size));
EXPECT_STREQ("36893488147419103231", boundary_value.asCharBufR (buf, char_buf_size));
EXPECT_STREQ("Overflow", overflowed.asCharBufR (buf, char_buf_size));
EXPECT_STREQ("NaN", not_a_number.asCharBufR (buf, char_buf_size));
}
TEST(GncInt128_functions, add_and_subtract)