handle strftime() more defensively
[libi2ncommon] / src / timefunc.cpp
index 69a50fb..88e6c33 100644 (file)
@@ -892,10 +892,13 @@ std::string format_iso8601 (const struct tm &tm, const bool date,
     /*
      * 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);
 }