handle strftime() more defensively
[libi2ncommon] / src / timefunc.cpp
index 0d2ee5d..88e6c33 100644 (file)
@@ -868,7 +868,7 @@ namespace {
  * @param tm      Time to format as broken-down \c struct tm.
  * @param date    Include the day part ([-]YYYY-MM-DD).
  * @param time    Include the time part (hh:mm:ss).
- * @param tz      Include the timezone ([±]ZZZZ); only heeded if
+ * @param tz      Include the timezone ([±]ZZZZ); only needed if
  *                \c time is requested as well.
  *
  * @return        The formatted timestamp.
@@ -890,12 +890,15 @@ std::string format_iso8601 (const struct tm &tm, const bool date,
     }
 
     /*
-     * The sign is *always* handled above so the formatted string her
+     * The sign is *always* handled above so the formatted string here
      * is always one character shorter.
-     * */
-    const size_t n = strftime (start, iso8601::bufsize-1, format, &tmp);
+     */
+    if (strftime (start, iso8601::bufsize-1, format, &tmp) == 0)
+    {
+        return std::string ();
+    }
 
-    buf [n+1] = '\0';
+    buf [iso8601::bufsize-1] = '\0'; /* Just in case. */
 
     return std::string (buf);
 }
@@ -997,7 +1000,7 @@ format_min_sec_msec (const struct timespec &ts)
 {
     char ms [4] = { '\0', '\0', '\0', '\0' };
 
-    if (snprintf (ms, 4, "%0.3ld", ts.tv_nsec / 1000000) < 0) {
+    if (snprintf (ms, 4, "%.3ld", ts.tv_nsec / 1000000) < 0) {
         return boost::none;
     }