From 2d20f4d57b04bcf219a8d25d8343879777c83bdd Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Thu, 25 Feb 2016 00:19:38 -0500 Subject: [PATCH] Cleanup: Converted all sprintf to snprintf --- src/Task.cpp | 2 +- src/Variant.cpp | 4 ++-- src/commands/CmdBurndown.cpp | 16 ++++++++-------- src/commands/CmdColor.cpp | 2 +- src/commands/CmdLogo.cpp | 4 ++-- src/commands/CmdSummary.cpp | 2 +- src/recur.cpp | 18 +++++------------- src/util.cpp | 8 ++++---- 8 files changed, 24 insertions(+), 32 deletions(-) diff --git a/src/Task.cpp b/src/Task.cpp index 35191c9b6..5dee6f25e 100644 --- a/src/Task.cpp +++ b/src/Task.cpp @@ -180,7 +180,7 @@ const std::string Task::identifier (bool shortened /* = false */) const void Task::setAsNow (const std::string& att) { char now[16]; - sprintf (now, "%u", (unsigned int) time (NULL)); + snprintf (now, 16, "%u", (unsigned int) time (NULL)); set (att, now); recalc_urgency = true; diff --git a/src/Variant.cpp b/src/Variant.cpp index c99b31525..1de474e85 100644 --- a/src/Variant.cpp +++ b/src/Variant.cpp @@ -1782,7 +1782,7 @@ void Variant::cast (const enum type new_type) case type_string: { char temp[24]; - sprintf (temp, "%d", _integer); + snprintf (temp, 24, "%d", _integer); _string = temp; } break; @@ -1800,7 +1800,7 @@ void Variant::cast (const enum type new_type) case type_string: { char temp[24]; - sprintf (temp, "%g", _real); + snprintf (temp, 24, "%g", _real); _string = temp; } break; diff --git a/src/commands/CmdBurndown.cpp b/src/commands/CmdBurndown.cpp index 3cf04e2fc..5151b3712 100644 --- a/src/commands/CmdBurndown.cpp +++ b/src/commands/CmdBurndown.cpp @@ -467,9 +467,9 @@ std::string Chart::render () // Draw y-axis labels. char label [12]; - sprintf (label, "%*d", _max_label, _labels[2]); + snprintf (label, 12, "%*d", _max_label, _labels[2]); _grid.replace (LOC (1, _max_label - strlen (label)), strlen (label), label); - sprintf (label, "%*d", _max_label, _labels[1]); + snprintf (label, 12, "%*d", _max_label, _labels[1]); _grid.replace (LOC (1 + (_graph_height / 2), _max_label - strlen (label)), strlen (label), label); _grid.replace (LOC (_graph_height + 1, _max_label - 1), 1, "0"); @@ -527,7 +527,7 @@ std::string Chart::render () calculateRates (); char rate[12]; if (_net_fix_rate != 0.0) - sprintf (rate, "%.1f/d", _net_fix_rate); + snprintf (rate, 12, "%.1f/d", _net_fix_rate); else strcpy (rate, "-"); @@ -729,24 +729,24 @@ void Chart::generateBars () std::string month = ISO8601d::monthName (cursor.month ()); bar._major_label = month.substr (0, 3); - sprintf (str, "%02d", cursor.day ()); + snprintf (str, 12, "%02d", cursor.day ()); bar._minor_label = str; } break; case 'W': // year/week - sprintf (str, "%d", cursor.year ()); + snprintf (str, 12, "%d", cursor.year ()); bar._major_label = str; - sprintf (str, "%02d", cursor.weekOfYear (0)); + snprintf (str, 12, "%02d", cursor.weekOfYear (0)); bar._minor_label = str; break; case 'M': // year/month - sprintf (str, "%d", cursor.year ()); + snprintf (str, 12, "%d", cursor.year ()); bar._major_label = str; - sprintf (str, "%02d", cursor.month ()); + snprintf (str, 12, "%02d", cursor.month ()); bar._minor_label = str; break; } diff --git a/src/commands/CmdColor.cpp b/src/commands/CmdColor.cpp index 64672d271..6fe0e3b2e 100644 --- a/src/commands/CmdColor.cpp +++ b/src/commands/CmdColor.cpp @@ -219,7 +219,7 @@ int CmdColor::execute (std::string& output) char label [12]; for (int g = 0; g < 6; ++g) { - sprintf (label, " %d", g); + snprintf (label, 12, " %d", g); out << Color::colorize (label, "bold green"); for (int r = 0; r < 6; ++r) { diff --git a/src/commands/CmdLogo.cpp b/src/commands/CmdLogo.cpp index 8a1c2c408..744c7e641 100644 --- a/src/commands/CmdLogo.cpp +++ b/src/commands/CmdLogo.cpp @@ -108,7 +108,7 @@ int CmdLogo::execute (std::string& output) { value += 167; char block [24]; - sprintf (block, "\033[48;5;%dm \033[0m", value); + snprintf (block, 24, "\033[48;5;%dm \033[0m", value); output += block; } } @@ -122,7 +122,7 @@ int CmdLogo::execute (std::string& output) { value += 167; char block [24]; - sprintf (block, "\033[48;5;%dm \033[0m", value); + snprintf (block, 24, "\033[48;5;%dm \033[0m", value); output += block; } } diff --git a/src/commands/CmdSummary.cpp b/src/commands/CmdSummary.cpp index 90da8328d..04e06ce4b 100644 --- a/src/commands/CmdSummary.cpp +++ b/src/commands/CmdSummary.cpp @@ -199,7 +199,7 @@ int CmdSummary::execute (std::string& output) char percent[12] = "0%"; if (c + p) - sprintf (percent, "%d%%", 100 * c / (c + p)); + snprintf (percent, 12, "%d%%", 100 * c / (c + p)); view.set (row, 3, percent); processed.push_back (i.first); } diff --git a/src/recur.cpp b/src/recur.cpp index 2c8b3f670..6ee2179fc 100644 --- a/src/recur.cpp +++ b/src/recur.cpp @@ -37,7 +37,6 @@ #include #include #include - #include #include #include @@ -80,10 +79,10 @@ void handleRecurrence () } // Get the mask from the parent task. - std::string mask = t.get ("mask"); + auto mask = t.get ("mask"); // Iterate over the due dates, and check each against the mask. - bool changed = false; + auto changed = false; unsigned int i = 0; for (auto& d : due) { @@ -97,18 +96,14 @@ void handleRecurrence () rec.set ("uuid", uuid ()); // New UUID. rec.set ("parent", t.get ("uuid")); // Remember mom. rec.setAsNow ("entry"); // New entry date. - - char dueDate[16]; - sprintf (dueDate, "%u", (unsigned int) d.toEpoch ()); - rec.set ("due", dueDate); // Store generated due date. + rec.set ("due", format (d.toEpoch ())); if (t.has ("wait")) { ISO8601d old_wait (t.get_date ("wait")); ISO8601d old_due (t.get_date ("due")); ISO8601d due (d); - sprintf (dueDate, "%u", (unsigned int) (due + (old_wait - old_due)).toEpoch ()); - rec.set ("wait", dueDate); + rec.set ("wait", format ((due + (old_wait - old_due)).toEpoch ())); rec.setStatus (Task::waiting); mask += 'W'; } @@ -118,10 +113,7 @@ void handleRecurrence () rec.setStatus (Task::pending); } - char indexMask[12]; - sprintf (indexMask, "%u", (unsigned int) i); - rec.set ("imask", indexMask); // Store index into mask. - + rec.set ("imask", i); rec.remove ("mask"); // Remove the mask of the parent. // Add the new task to the DB. diff --git a/src/util.cpp b/src/util.cpp index 0d7c6e3a8..2db86148d 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -155,10 +155,10 @@ std::string formatBytes (size_t bytes) { char formatted[24]; - if (bytes >= 995000000) sprintf (formatted, "%.1f %s", (bytes / 1000000000.0), STRING_UTIL_GIBIBYTES); - else if (bytes >= 995000) sprintf (formatted, "%.1f %s", (bytes / 1000000.0), STRING_UTIL_MEBIBYTES); - else if (bytes >= 995) sprintf (formatted, "%.1f %s", (bytes / 1000.0), STRING_UTIL_KIBIBYTES); - else sprintf (formatted, "%d %s", (int)bytes, STRING_UTIL_BYTES); + if (bytes >= 995000000) snprintf (formatted, 24, "%.1f %s", (bytes / 1000000000.0), STRING_UTIL_GIBIBYTES); + else if (bytes >= 995000) snprintf (formatted, 24, "%.1f %s", (bytes / 1000000.0), STRING_UTIL_MEBIBYTES); + else if (bytes >= 995) snprintf (formatted, 24, "%.1f %s", (bytes / 1000.0), STRING_UTIL_KIBIBYTES); + else snprintf (formatted, 24, "%d %s", (int)bytes, STRING_UTIL_BYTES); return Lexer::commify (formatted); }