diff --git a/gnucash/gnome/gnc-plugin-page-report.cpp b/gnucash/gnome/gnc-plugin-page-report.cpp index 184a7971ac..7c1ba12b8b 100644 --- a/gnucash/gnome/gnc-plugin-page-report.cpp +++ b/gnucash/gnome/gnc-plugin-page-report.cpp @@ -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) diff --git a/gnucash/html/gnc-html.c b/gnucash/html/gnc-html.c index 0d269f6fa6..0c13875ce7 100644 --- a/gnucash/html/gnc-html.c +++ b/gnucash/html/gnc-html.c @@ -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++; diff --git a/libgnucash/app-utils/calculation/fin.c b/libgnucash/app-utils/calculation/fin.c index 3c04118fb8..2966a2fb83 100644 --- a/libgnucash/app-utils/calculation/fin.c +++ b/libgnucash/app-utils/calculation/fin.c @@ -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; diff --git a/libgnucash/app-utils/gnc-ui-util.c b/libgnucash/app-utils/gnc-ui-util.c index 534f16d349..43b6c427a7 100644 --- a/libgnucash/app-utils/gnc-ui-util.c +++ b/libgnucash/app-utils/gnc-ui-util.c @@ -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) diff --git a/libgnucash/backend/xml/test/test-xml2-is-file.cpp b/libgnucash/backend/xml/test/test-xml2-is-file.cpp index 6303313f0c..717bb5e5b4 100644 --- a/libgnucash/backend/xml/test/test-xml2-is-file.cpp +++ b/libgnucash/backend/xml/test/test-xml2-is-file.cpp @@ -38,9 +38,9 @@ main (int argc, char** argv) directory = "test-files/xml2"; } - char* filename = static_cast (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 (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 (); diff --git a/libgnucash/engine/gnc-int128.cpp b/libgnucash/engine/gnc-int128.cpp index d670232aba..5e3201f1cd 100644 --- a/libgnucash/engine/gnc-int128.cpp +++ b/libgnucash/engine/gnc-int128.cpp @@ -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; } diff --git a/libgnucash/engine/gnc-int128.hpp b/libgnucash/engine/gnc-int128.hpp index a352f73eb2..4dc76ab2c0 100644 --- a/libgnucash/engine/gnc-int128.hpp +++ b/libgnucash/engine/gnc-int128.hpp @@ -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; diff --git a/libgnucash/engine/gnc-numeric.cpp b/libgnucash/engine/gnc-numeric.cpp index a33519855e..2568fd7487 100644 --- a/libgnucash/engine/gnc-numeric.cpp +++ b/libgnucash/engine/gnc-numeric.cpp @@ -34,6 +34,7 @@ #include #include +#include #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; } diff --git a/libgnucash/engine/test/gtest-gnc-int128.cpp b/libgnucash/engine/test/gtest-gnc-int128.cpp index 82ed98ba03..7c215409d4 100644 --- a/libgnucash/engine/test/gtest-gnc-int128.cpp +++ b/libgnucash/engine/test/gtest-gnc-int128.cpp @@ -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)