Check for localtime() failing.
authorGuy Harris <guy@alum.mit.edu>
Wed, 29 Mar 2017 01:41:43 +0000 (18:41 -0700)
committerGuy Harris <guy@alum.mit.edu>
Wed, 29 Mar 2017 01:42:14 +0000 (01:42 +0000)
It "shouldn't happen", but at least this squelches a Coverity complaint,
CID 1394503.

Change-Id: I40af10d47c1d1b026f6b40ef68b139e6bf246109
Reviewed-on: https://code.wireshark.org/review/20774
Reviewed-by: Guy Harris <guy@alum.mit.edu>
text2pcap.c

index 848c213c79920143795edfcdac138c26a04d2a9a..87a3e90680e1c3fc844a6d39be1c8b50069e1130 100644 (file)
@@ -1469,6 +1469,7 @@ parse_options (int argc, char *argv[])
         {"version", no_argument, NULL, 'v'},
         {0, 0, 0, 0 }
     };
+    struct tm *now_tm;
 
 #ifdef _WIN32
     arg_list_utf_16to8(argc, argv);
@@ -1833,8 +1834,17 @@ parse_options (int argc, char *argv[])
     }
 
     ts_sec = time(0);               /* initialize to current time */
-    /* We trust the OS to return a time after the Epoch. */
-    timecode_default = *localtime(&ts_sec);
+    now_tm = localtime(&ts_sec);
+    if (now_tm == NULL) {
+        /*
+         * This shouldn't happen - on UN*X, this should Just Work, and
+         * on Windows, it won't work if ts_sec is before the Epoch,
+         * but it's long after 1970, so....
+         */
+        fprintf(stderr, "localtime(right now) failed\n");
+        return EXIT_FAILURE;
+    }
+    timecode_default = *now_tm;
     timecode_default.tm_isdst = -1; /* Unknown for now, depends on time given to the strptime() function */
 
     /* Display summary of our state */