[systemd-devel] [PATCH 2/3] bootchart: check return of strftime

Thomas H.P. Andersen phomes at gmail.com
Fri Sep 26 13:01:31 PDT 2014


From: Thomas Hindoe Paaboel Andersen <phomes at gmail.com>

Found by coverity. Fixes: CID#996314 and #996312
---
 src/bootchart/bootchart.c | 14 ++++++++++++--
 src/bootchart/svg.c       |  6 ++++--
 2 files changed, 16 insertions(+), 4 deletions(-)

diff --git a/src/bootchart/bootchart.c b/src/bootchart/bootchart.c
index 8ef5ad1..e127ad3 100644
--- a/src/bootchart/bootchart.c
+++ b/src/bootchart/bootchart.c
@@ -389,7 +389,12 @@ int main(int argc, char *argv[]) {
 
                 if (!of && (access(arg_output_path, R_OK|W_OK|X_OK) == 0)) {
                         t = time(NULL);
-                        strftime(datestr, sizeof(datestr), "%Y%m%d-%H%M", localtime(&t));
+                        r = strftime(datestr, sizeof(datestr), "%Y%m%d-%H%M", localtime(&t));
+                        if (r <= 0) {
+                                log_error("Failed to format time.");
+                                return EXIT_FAILURE;
+                        }
+
                         snprintf(output_file, PATH_MAX, "%s/bootchart-%s.svg", arg_output_path, datestr);
                         of = fopen(output_file, "we");
                 }
@@ -457,7 +462,12 @@ int main(int argc, char *argv[]) {
 
         if (!of) {
                 t = time(NULL);
-                strftime(datestr, sizeof(datestr), "%Y%m%d-%H%M", localtime(&t));
+                r = strftime(datestr, sizeof(datestr), "%Y%m%d-%H%M", localtime(&t));
+                if (r <= 0) {
+                        log_error("Failed to format time.");
+                        return EXIT_FAILURE;
+                }
+
                 snprintf(output_file, PATH_MAX, "%s/bootchart-%s.svg", arg_output_path, datestr);
                 of = fopen(output_file, "we");
         }
diff --git a/src/bootchart/svg.c b/src/bootchart/svg.c
index 135883f..cb58246 100644
--- a/src/bootchart/svg.c
+++ b/src/bootchart/svg.c
@@ -162,7 +162,7 @@ static void svg_title(const char *build) {
         char *c;
         FILE *f;
         time_t t;
-        int fd;
+        int fd, r;
         struct utsname uts;
 
         /* grab /proc/cmdline */
@@ -196,7 +196,9 @@ static void svg_title(const char *build) {
 
         /* date */
         t = time(NULL);
-        strftime(date, sizeof(date), "%a, %d %b %Y %H:%M:%S %z", localtime(&t));
+        r = strftime(date, sizeof(date), "%a, %d %b %Y %H:%M:%S %z", localtime(&t));
+        if (r <= 0)
+                fprintf(stderr, "Failed to format time\n");
 
         /* CPU type */
         fd = openat(procfd, "cpuinfo", O_RDONLY);
-- 
2.1.0



More information about the systemd-devel mailing list