Cleanup: Converted all sprintf to snprintf

This commit is contained in:
Paul Beckingham 2016-02-25 00:19:38 -05:00
parent 88f6872190
commit 2d20f4d57b
8 changed files with 24 additions and 32 deletions

View file

@ -180,7 +180,7 @@ const std::string Task::identifier (bool shortened /* = false */) const
void Task::setAsNow (const std::string& att) void Task::setAsNow (const std::string& att)
{ {
char now[16]; char now[16];
sprintf (now, "%u", (unsigned int) time (NULL)); snprintf (now, 16, "%u", (unsigned int) time (NULL));
set (att, now); set (att, now);
recalc_urgency = true; recalc_urgency = true;

View file

@ -1782,7 +1782,7 @@ void Variant::cast (const enum type new_type)
case type_string: case type_string:
{ {
char temp[24]; char temp[24];
sprintf (temp, "%d", _integer); snprintf (temp, 24, "%d", _integer);
_string = temp; _string = temp;
} }
break; break;
@ -1800,7 +1800,7 @@ void Variant::cast (const enum type new_type)
case type_string: case type_string:
{ {
char temp[24]; char temp[24];
sprintf (temp, "%g", _real); snprintf (temp, 24, "%g", _real);
_string = temp; _string = temp;
} }
break; break;

View file

@ -467,9 +467,9 @@ std::string Chart::render ()
// Draw y-axis labels. // Draw y-axis labels.
char label [12]; 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); _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 (1 + (_graph_height / 2), _max_label - strlen (label)), strlen (label), label);
_grid.replace (LOC (_graph_height + 1, _max_label - 1), 1, "0"); _grid.replace (LOC (_graph_height + 1, _max_label - 1), 1, "0");
@ -527,7 +527,7 @@ std::string Chart::render ()
calculateRates (); calculateRates ();
char rate[12]; char rate[12];
if (_net_fix_rate != 0.0) if (_net_fix_rate != 0.0)
sprintf (rate, "%.1f/d", _net_fix_rate); snprintf (rate, 12, "%.1f/d", _net_fix_rate);
else else
strcpy (rate, "-"); strcpy (rate, "-");
@ -729,24 +729,24 @@ void Chart::generateBars ()
std::string month = ISO8601d::monthName (cursor.month ()); std::string month = ISO8601d::monthName (cursor.month ());
bar._major_label = month.substr (0, 3); bar._major_label = month.substr (0, 3);
sprintf (str, "%02d", cursor.day ()); snprintf (str, 12, "%02d", cursor.day ());
bar._minor_label = str; bar._minor_label = str;
} }
break; break;
case 'W': // year/week case 'W': // year/week
sprintf (str, "%d", cursor.year ()); snprintf (str, 12, "%d", cursor.year ());
bar._major_label = str; bar._major_label = str;
sprintf (str, "%02d", cursor.weekOfYear (0)); snprintf (str, 12, "%02d", cursor.weekOfYear (0));
bar._minor_label = str; bar._minor_label = str;
break; break;
case 'M': // year/month case 'M': // year/month
sprintf (str, "%d", cursor.year ()); snprintf (str, 12, "%d", cursor.year ());
bar._major_label = str; bar._major_label = str;
sprintf (str, "%02d", cursor.month ()); snprintf (str, 12, "%02d", cursor.month ());
bar._minor_label = str; bar._minor_label = str;
break; break;
} }

View file

@ -219,7 +219,7 @@ int CmdColor::execute (std::string& output)
char label [12]; char label [12];
for (int g = 0; g < 6; ++g) for (int g = 0; g < 6; ++g)
{ {
sprintf (label, " %d", g); snprintf (label, 12, " %d", g);
out << Color::colorize (label, "bold green"); out << Color::colorize (label, "bold green");
for (int r = 0; r < 6; ++r) for (int r = 0; r < 6; ++r)
{ {

View file

@ -108,7 +108,7 @@ int CmdLogo::execute (std::string& output)
{ {
value += 167; value += 167;
char block [24]; char block [24];
sprintf (block, "\033[48;5;%dm \033[0m", value); snprintf (block, 24, "\033[48;5;%dm \033[0m", value);
output += block; output += block;
} }
} }
@ -122,7 +122,7 @@ int CmdLogo::execute (std::string& output)
{ {
value += 167; value += 167;
char block [24]; char block [24];
sprintf (block, "\033[48;5;%dm \033[0m", value); snprintf (block, 24, "\033[48;5;%dm \033[0m", value);
output += block; output += block;
} }
} }

View file

@ -199,7 +199,7 @@ int CmdSummary::execute (std::string& output)
char percent[12] = "0%"; char percent[12] = "0%";
if (c + p) if (c + p)
sprintf (percent, "%d%%", 100 * c / (c + p)); snprintf (percent, 12, "%d%%", 100 * c / (c + p));
view.set (row, 3, percent); view.set (row, 3, percent);
processed.push_back (i.first); processed.push_back (i.first);
} }

View file

@ -37,7 +37,6 @@
#include <stdlib.h> #include <stdlib.h>
#include <pwd.h> #include <pwd.h>
#include <time.h> #include <time.h>
#include <Context.h> #include <Context.h>
#include <Lexer.h> #include <Lexer.h>
#include <ISO8601.h> #include <ISO8601.h>
@ -80,10 +79,10 @@ void handleRecurrence ()
} }
// Get the mask from the parent task. // 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. // Iterate over the due dates, and check each against the mask.
bool changed = false; auto changed = false;
unsigned int i = 0; unsigned int i = 0;
for (auto& d : due) for (auto& d : due)
{ {
@ -97,18 +96,14 @@ void handleRecurrence ()
rec.set ("uuid", uuid ()); // New UUID. rec.set ("uuid", uuid ()); // New UUID.
rec.set ("parent", t.get ("uuid")); // Remember mom. rec.set ("parent", t.get ("uuid")); // Remember mom.
rec.setAsNow ("entry"); // New entry date. rec.setAsNow ("entry"); // New entry date.
rec.set ("due", format (d.toEpoch ()));
char dueDate[16];
sprintf (dueDate, "%u", (unsigned int) d.toEpoch ());
rec.set ("due", dueDate); // Store generated due date.
if (t.has ("wait")) if (t.has ("wait"))
{ {
ISO8601d old_wait (t.get_date ("wait")); ISO8601d old_wait (t.get_date ("wait"));
ISO8601d old_due (t.get_date ("due")); ISO8601d old_due (t.get_date ("due"));
ISO8601d due (d); ISO8601d due (d);
sprintf (dueDate, "%u", (unsigned int) (due + (old_wait - old_due)).toEpoch ()); rec.set ("wait", format ((due + (old_wait - old_due)).toEpoch ()));
rec.set ("wait", dueDate);
rec.setStatus (Task::waiting); rec.setStatus (Task::waiting);
mask += 'W'; mask += 'W';
} }
@ -118,10 +113,7 @@ void handleRecurrence ()
rec.setStatus (Task::pending); rec.setStatus (Task::pending);
} }
char indexMask[12]; rec.set ("imask", i);
sprintf (indexMask, "%u", (unsigned int) i);
rec.set ("imask", indexMask); // Store index into mask.
rec.remove ("mask"); // Remove the mask of the parent. rec.remove ("mask"); // Remove the mask of the parent.
// Add the new task to the DB. // Add the new task to the DB.

View file

@ -155,10 +155,10 @@ std::string formatBytes (size_t bytes)
{ {
char formatted[24]; char formatted[24];
if (bytes >= 995000000) sprintf (formatted, "%.1f %s", (bytes / 1000000000.0), STRING_UTIL_GIBIBYTES); if (bytes >= 995000000) snprintf (formatted, 24, "%.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 >= 995000) snprintf (formatted, 24, "%.1f %s", (bytes / 1000000.0), STRING_UTIL_MEBIBYTES);
else if (bytes >= 995) sprintf (formatted, "%.1f %s", (bytes / 1000.0), STRING_UTIL_KIBIBYTES); else if (bytes >= 995) snprintf (formatted, 24, "%.1f %s", (bytes / 1000.0), STRING_UTIL_KIBIBYTES);
else sprintf (formatted, "%d %s", (int)bytes, STRING_UTIL_BYTES); else snprintf (formatted, 24, "%d %s", (int)bytes, STRING_UTIL_BYTES);
return Lexer::commify (formatted); return Lexer::commify (formatted);
} }