From 10ca4c7527122efde0300205deaa6c0143f07219 Mon Sep 17 00:00:00 2001 From: Guy Harris Date: Fri, 21 Oct 2016 19:18:15 -0700 Subject: [PATCH] More checks for localtime() and gmtime() returning NULL. And some comments in the case where we're converting the result of time() - if your machine's idea of time predates January 1, 1970, 00:00:00 UTC, it'll crash on Windows, but that's not a case where a *file* can cause the problem due either to a bad file time stamp or bad time stamps in the file. Change-Id: I837a438e4b875dd8c4f3ec2137df7a16ee4e9498 Reviewed-on: https://code.wireshark.org/review/18369 Reviewed-by: Guy Harris --- dumpcap.c | 21 ++++--- epan/print.c | 21 +++++-- epan/tvbuff.c | 13 ++++- extcap.c | 4 ++ ringbuffer.c | 6 ++ text2pcap.c | 1 + ui/cli/tap-iostat.c | 86 +++++++++++++++++------------ ui/cli/tap-iousers.c | 100 +++++++++++++++++++--------------- ui/console.c | 15 +++-- ui/gtk/fileset_dlg.c | 20 +++++-- ui/gtk/iax2_analysis.c | 23 +++++--- ui/gtk/io_stat.c | 21 ++++--- ui/gtk/main_welcome.c | 2 +- ui/gtk/memory_dlg.c | 21 ++++--- ui/gtk/rtp_analysis.c | 22 +++++--- ui/gtk/rtp_player.c | 10 +++- ui/text_import.c | 1 + wiretap/commview.c | 27 ++++++--- wiretap/logcat_text.c | 43 +++++++++++---- wiretap/network_instruments.c | 1 + wsutil/tempfile.c | 1 + 21 files changed, 299 insertions(+), 160 deletions(-) diff --git a/dumpcap.c b/dumpcap.c index be1910fd24..be96c173a7 100644 --- a/dumpcap.c +++ b/dumpcap.c @@ -4509,10 +4509,6 @@ console_log_handler(const char *log_domain, GLogLevelFlags log_level, #endif } - /* create a "timestamp" */ - time(&curr); - today = localtime(&curr); - switch(log_level & G_LOG_LEVEL_MASK) { case G_LOG_LEVEL_ERROR: level = "Err "; @@ -4543,11 +4539,20 @@ console_log_handler(const char *log_domain, GLogLevelFlags log_level, /* normal user messages without additional infos */ msg = g_strdup_printf("%s\n", message); } else { + /* create a "timestamp" */ + time(&curr); + today = localtime(&curr); + /* info/debug messages with additional infos */ - msg = g_strdup_printf("%02u:%02u:%02u %8s %s %s\n", - today->tm_hour, today->tm_min, today->tm_sec, - log_domain != NULL ? log_domain : "", - level, message); + if (today != NULL) + msg = g_strdup_printf("%02u:%02u:%02u %8s %s %s\n", + today->tm_hour, today->tm_min, today->tm_sec, + log_domain != NULL ? log_domain : "", + level, message); + else + msg = g_strdup_printf("Time not representable %8s %s %s\n", + log_domain != NULL ? log_domain : "", + level, message); } /* DEBUG & INFO msgs (if we're debugging today) */ diff --git a/epan/print.c b/epan/print.c index 3ae96f5a0c..a54d84873a 100644 --- a/epan/print.c +++ b/epan/print.c @@ -246,9 +246,16 @@ void write_pdml_preamble(FILE *fh, const gchar *filename) { time_t t = time(NULL); - char *ts = asctime(localtime(&t)); + struct tm * timeinfo; + char *ts; - ts[strlen(ts)-1] = 0; /* overwrite \n */ + /* Create the output */ + timeinfo = localtime(&t); + if (timeinfo != NULL) { + ts = asctime(timeinfo); + ts[strlen(ts)-1] = 0; /* overwrite \n */ + } else + ts = "Not representable"; fprintf(fh, "\n"); fprintf(fh, "\n"); @@ -333,7 +340,10 @@ write_json_proto_tree(output_fields_t* fields, print_args_t *print_args, gchar * /* Create the output */ timeinfo = localtime(&t); - strftime(ts, 30, "%Y-%m-%d", timeinfo); + if (timeinfo != NULL) + strftime(ts, sizeof ts, "%Y-%m-%d", timeinfo); + else + g_strlcpy(ts, "XXXX-XX-XX", sizeof ts); /* XXX - better way of saying "Not representable"? */ if (!is_first) fputs(" ,\n", fh); @@ -381,7 +391,10 @@ write_ek_proto_tree(output_fields_t* fields, print_args_t *print_args, gchar **p /* Create the output */ timeinfo = localtime(&t); - strftime(ts, 30, "%Y-%m-%d", timeinfo); + if (timeinfo != NULL) + strftime(ts, sizeof ts, "%Y-%m-%d", timeinfo); + else + g_strlcpy(ts, "XXXX-XX-XX", sizeof ts); /* XXX - better way of saying "Not representable"? */ fprintf(fh, "{\"index\" : {\"_index\": \"packets-%s\", \"_type\": \"pcap_file\", \"_score\": null}}\n", ts); /* Timestamp added for time indexing in Elasticsearch */ diff --git a/epan/tvbuff.c b/epan/tvbuff.c index 8919131344..3358b7ae1f 100644 --- a/epan/tvbuff.c +++ b/epan/tvbuff.c @@ -1580,9 +1580,16 @@ tvb_get_string_time(tvbuff_t *tvb, const gint offset, const gint length, /* setting it to "now" for now */ time_t time_now = time(NULL); struct tm *tm_now = gmtime(&time_now); - tm.tm_year = tm_now->tm_year; - tm.tm_mon = tm_now->tm_mon; - tm.tm_mday = tm_now->tm_mday; + if (tm_now != NULL) { + tm.tm_year = tm_now->tm_year; + tm.tm_mon = tm_now->tm_mon; + tm.tm_mday = tm_now->tm_mday; + } else { + /* The second before the Epoch */ + tm.tm_year = 69; + tm.tm_mon = 12; + tm.tm_mday = 31; + } end = ptr + num_chars; errno = 0; diff --git a/extcap.c b/extcap.c index 3672902cdd..b900d084f5 100644 --- a/extcap.c +++ b/extcap.c @@ -1127,6 +1127,10 @@ gboolean extcap_create_pipe(char ** fifo) SECURITY_ATTRIBUTES security; /* create pipename */ current_time = time(NULL); + /* + * XXX - we trust Windows not to return a time before the Epoch here, + * so we won't get a null pointer back from localtime(). + */ strftime(timestr, sizeof(timestr), "%Y%m%d%H%M%S", localtime(¤t_time)); pipename = g_strconcat ( "\\\\.\\pipe\\", EXTCAP_PIPE_PREFIX, "_", timestr, NULL ); diff --git a/ringbuffer.c b/ringbuffer.c index 7f0f67d5e7..099e8c4783 100644 --- a/ringbuffer.c +++ b/ringbuffer.c @@ -102,6 +102,12 @@ static int ringbuf_open_file(rb_file *rfile, int *err) current_time = time(NULL); g_snprintf(filenum, sizeof(filenum), "%05u", (rb_data.curr_file_num + 1) % RINGBUFFER_MAX_NUM_FILES); + /* + * XXX - We trust Windows not to return a time before the Epoch, so + * localtime() doesn't return a null pointer. localtime() can probably + * handle pre-Epoch times on most UN*X systems, and we trust them not + * to return a time before the Epoch in any case. + */ strftime(timestr, sizeof(timestr), "%Y%m%d%H%M%S", localtime(¤t_time)); rfile->name = g_strconcat(rb_data.fprefix, "_", filenum, "_", timestr, rb_data.fsuffix, NULL); diff --git a/text2pcap.c b/text2pcap.c index 855df5e54c..65d6547b45 100644 --- a/text2pcap.c +++ b/text2pcap.c @@ -1801,6 +1801,7 @@ 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); timecode_default.tm_isdst = -1; /* Unknown for now, depends on time given to the strptime() function */ diff --git a/ui/cli/tap-iostat.c b/ui/cli/tap-iostat.c index c23eb63bcb..1ff9b38b00 100644 --- a/ui/cli/tap-iostat.c +++ b/ui/cli/tap-iostat.c @@ -1013,60 +1013,78 @@ iostat_draw(void *arg) switch (timestamp_get_type()) { case TS_ABSOLUTE: tm_time = localtime(&the_time); - printf("| %02d:%02d:%02d |", - tm_time->tm_hour, - tm_time->tm_min, - tm_time->tm_sec); + if (tm_time != NULL) { + printf("| %02d:%02d:%02d |", + tm_time->tm_hour, + tm_time->tm_min, + tm_time->tm_sec); + } else + printf("| XX:XX:XX |"); break; case TS_ABSOLUTE_WITH_YMD: tm_time = localtime(&the_time); - printf("| %04d-%02d-%02d %02d:%02d:%02d |", - tm_time->tm_year + 1900, - tm_time->tm_mon + 1, - tm_time->tm_mday, - tm_time->tm_hour, - tm_time->tm_min, - tm_time->tm_sec); + if (tm_time != NULL) { + printf("| %04d-%02d-%02d %02d:%02d:%02d |", + tm_time->tm_year + 1900, + tm_time->tm_mon + 1, + tm_time->tm_mday, + tm_time->tm_hour, + tm_time->tm_min, + tm_time->tm_sec); + } else + printf("| XXXX-XX-XX XX:XX:XX |"); break; case TS_ABSOLUTE_WITH_YDOY: tm_time = localtime(&the_time); - printf("| %04d/%03d %02d:%02d:%02d |", - tm_time->tm_year + 1900, - tm_time->tm_yday + 1, - tm_time->tm_hour, - tm_time->tm_min, - tm_time->tm_sec); + if (tm_time != NULL) { + printf("| %04d/%03d %02d:%02d:%02d |", + tm_time->tm_year + 1900, + tm_time->tm_yday + 1, + tm_time->tm_hour, + tm_time->tm_min, + tm_time->tm_sec); + } else + printf("| XXXX/XXX XX:XX:XX |"); break; case TS_UTC: tm_time = gmtime(&the_time); - printf("| %02d:%02d:%02d |", - tm_time->tm_hour, - tm_time->tm_min, - tm_time->tm_sec); + if (tm_time != NULL) { + printf("| %02d:%02d:%02d |", + tm_time->tm_hour, + tm_time->tm_min, + tm_time->tm_sec); + } else + printf("| XX:XX:XX |"); break; case TS_UTC_WITH_YMD: tm_time = gmtime(&the_time); - printf("| %04d-%02d-%02d %02d:%02d:%02d |", - tm_time->tm_year + 1900, - tm_time->tm_mon + 1, - tm_time->tm_mday, - tm_time->tm_hour, - tm_time->tm_min, - tm_time->tm_sec); + if (tm_time != NULL) { + printf("| %04d-%02d-%02d %02d:%02d:%02d |", + tm_time->tm_year + 1900, + tm_time->tm_mon + 1, + tm_time->tm_mday, + tm_time->tm_hour, + tm_time->tm_min, + tm_time->tm_sec); + } else + printf("| XXXX-XX-XX XX:XX:XX |"); break; case TS_UTC_WITH_YDOY: tm_time = gmtime(&the_time); - printf("| %04d/%03d %02d:%02d:%02d |", - tm_time->tm_year + 1900, - tm_time->tm_yday + 1, - tm_time->tm_hour, - tm_time->tm_min, - tm_time->tm_sec); + if (tm_time != NULL) { + printf("| %04d/%03d %02d:%02d:%02d |", + tm_time->tm_year + 1900, + tm_time->tm_yday + 1, + tm_time->tm_hour, + tm_time->tm_min, + tm_time->tm_sec); + } else + printf("| XXXX/XXX XX:XX:XX |"); break; case TS_RELATIVE: diff --git a/ui/cli/tap-iousers.c b/ui/cli/tap-iousers.c index 06f02697ac..129b56e580 100644 --- a/ui/cli/tap-iousers.c +++ b/ui/cli/tap-iousers.c @@ -140,71 +140,83 @@ iousers_draw(void *arg) switch (timestamp_get_type()) { case TS_ABSOLUTE: tm_time = localtime(&iui->start_abs_time.secs); - printf("%02d:%02d:%02d %12.4f\n", - tm_time->tm_hour, - tm_time->tm_min, - tm_time->tm_sec, - nstime_to_sec(&iui->stop_time) - nstime_to_sec(&iui->start_time)); + if (tm_time != NULL) { + printf("%02d:%02d:%02d", + tm_time->tm_hour, + tm_time->tm_min, + tm_time->tm_sec); + } else + printf("XX:XX:XX"); break; case TS_ABSOLUTE_WITH_YMD: tm_time = localtime(&iui->start_abs_time.secs); - printf("%04d-%02d-%02d %02d:%02d:%02d %12.4f\n", - tm_time->tm_year + 1900, - tm_time->tm_mon + 1, - tm_time->tm_mday, - tm_time->tm_hour, - tm_time->tm_min, - tm_time->tm_sec, - nstime_to_sec(&iui->stop_time) - nstime_to_sec(&iui->start_time)); + if (tm_time != NULL) { + printf("%04d-%02d-%02d %02d:%02d:%02d", + tm_time->tm_year + 1900, + tm_time->tm_mon + 1, + tm_time->tm_mday, + tm_time->tm_hour, + tm_time->tm_min, + tm_time->tm_sec); + } else + printf("XXXX-XX-XX XX:XX:XX"); break; case TS_ABSOLUTE_WITH_YDOY: tm_time = localtime(&iui->start_abs_time.secs); - printf("%04d/%03d %02d:%02d:%02d %12.4f\n", - tm_time->tm_year + 1900, - tm_time->tm_yday + 1, - tm_time->tm_hour, - tm_time->tm_min, - tm_time->tm_sec, - nstime_to_sec(&iui->stop_time) - nstime_to_sec(&iui->start_time)); + if (tm_time != NULL) { + printf("%04d/%03d %02d:%02d:%02d", + tm_time->tm_year + 1900, + tm_time->tm_yday + 1, + tm_time->tm_hour, + tm_time->tm_min, + tm_time->tm_sec); + } else + printf("XXXX/XXX XX:XX:XX"); break; case TS_UTC: tm_time = gmtime(&iui->start_abs_time.secs); - printf("%02d:%02d:%02d %12.4f\n", - tm_time->tm_hour, - tm_time->tm_min, - tm_time->tm_sec, - nstime_to_sec(&iui->stop_time) - nstime_to_sec(&iui->start_time)); + if (tm_time != NULL) { + printf("%02d:%02d:%02d", + tm_time->tm_hour, + tm_time->tm_min, + tm_time->tm_sec); + } else + printf("XX:XX:XX"); break; case TS_UTC_WITH_YMD: tm_time = gmtime(&iui->start_abs_time.secs); - printf("%04d-%02d-%02d %02d:%02d:%02d %12.4f\n", - tm_time->tm_year + 1900, - tm_time->tm_mon + 1, - tm_time->tm_mday, - tm_time->tm_hour, - tm_time->tm_min, - tm_time->tm_sec, - nstime_to_sec(&iui->stop_time) - nstime_to_sec(&iui->start_time)); + if (tm_time != NULL) { + printf("%04d-%02d-%02d %02d:%02d:%02d", + tm_time->tm_year + 1900, + tm_time->tm_mon + 1, + tm_time->tm_mday, + tm_time->tm_hour, + tm_time->tm_min, + tm_time->tm_sec); + } else + printf("XXXX-XX-XX XX:XX:XX"); break; case TS_UTC_WITH_YDOY: tm_time = gmtime(&iui->start_abs_time.secs); - printf("%04d/%03d %02d:%02d:%02d %12.4f\n", - tm_time->tm_year + 1900, - tm_time->tm_yday + 1, - tm_time->tm_hour, - tm_time->tm_min, - tm_time->tm_sec, - nstime_to_sec(&iui->stop_time) - nstime_to_sec(&iui->start_time)); + if (tm_time != NULL) { + printf("%04d/%03d %02d:%02d:%02d", + tm_time->tm_year + 1900, + tm_time->tm_yday + 1, + tm_time->tm_hour, + tm_time->tm_min, + tm_time->tm_sec); + } else + printf("XXXX/XXX XX:XX:XX"); break; case TS_RELATIVE: case TS_NOT_SET: default: - printf("%14.9f %12.4f\n", - nstime_to_sec(&iui->start_time), - nstime_to_sec(&iui->stop_time) - nstime_to_sec(&iui->start_time) - ); + printf("%14.9f", + nstime_to_sec(&iui->start_time)); break; } + printf(" %12.4f\n", + nstime_to_sec(&iui->stop_time) - nstime_to_sec(&iui->start_time)); } } max_frames = last_frames; diff --git a/ui/console.c b/ui/console.c index 94c6e5ac3f..2f3064b290 100644 --- a/ui/console.c +++ b/ui/console.c @@ -91,11 +91,16 @@ console_log_handler(const char *log_domain, GLogLevelFlags log_level, /* create a "timestamp" */ time(&curr); today = localtime(&curr); - - fprintf(stderr, "%02d:%02d:%02d %8s %s %s\n", - today->tm_hour, today->tm_min, today->tm_sec, - log_domain != NULL ? log_domain : "", - level, message); + if (today != NULL) { + fprintf(stderr, "%02d:%02d:%02d %8s %s %s\n", + today->tm_hour, today->tm_min, today->tm_sec, + log_domain != NULL ? log_domain : "", + level, message); + } else { + fprintf(stderr, "Time not representable %8s %s %s\n", + log_domain != NULL ? log_domain : "", + level, message); + } #ifdef _WIN32 if(log_level & G_LOG_LEVEL_ERROR) { /* wait for a key press before the following error handler will terminate the program diff --git a/ui/gtk/fileset_dlg.c b/ui/gtk/fileset_dlg.c index bb0e04d7fe..cc28f2b152 100644 --- a/ui/gtk/fileset_dlg.c +++ b/ui/gtk/fileset_dlg.c @@ -160,15 +160,23 @@ fileset_dlg_add_file(fileset_entry *entry, void *window _U_) { /* if this file doesn't follow the file set pattern, */ /* use the creation time of that file */ local = localtime(&entry->ctime); - created = g_strdup_printf("%04u-%02u-%02u %02u:%02u:%02u", - local->tm_year+1900, local->tm_mon+1, local->tm_mday, - local->tm_hour, local->tm_min, local->tm_sec); + if (local != NULL) { + created = g_strdup_printf("%04u-%02u-%02u %02u:%02u:%02u", + local->tm_year+1900, local->tm_mon+1, local->tm_mday, + local->tm_hour, local->tm_min, local->tm_sec); + } else { + created = g_strdup("Time not representable"); + } } local = localtime(&entry->mtime); - modified = g_strdup_printf("%04u-%02u-%02u %02u:%02u:%02u", - local->tm_year+1900, local->tm_mon+1, local->tm_mday, - local->tm_hour, local->tm_min, local->tm_sec); + if (local != NULL) { + modified = g_strdup_printf("%04u-%02u-%02u %02u:%02u:%02u", + local->tm_year+1900, local->tm_mon+1, local->tm_mday, + local->tm_hour, local->tm_min, local->tm_sec); + } else { + modified = g_strdup("Time not representable"); + } size = g_strdup_printf("%" G_GINT64_MODIFIER "d Bytes", entry->size); fs_rb = gtk_radio_button_new_with_label_from_widget( diff --git a/ui/gtk/iax2_analysis.c b/ui/gtk/iax2_analysis.c index f8b45d1c75..d4ade50f23 100644 --- a/ui/gtk/iax2_analysis.c +++ b/ui/gtk/iax2_analysis.c @@ -516,14 +516,21 @@ static int iax2_packet_add_info(GtkWidget *list, user_data_t * user_data, then = pinfo->abs_ts.secs; msecs = (guint16)(pinfo->abs_ts.nsecs/1000000); tm_tmp = localtime(&then); - g_snprintf(timeStr,sizeof(timeStr),"%02d/%02d/%04d %02d:%02d:%02d.%03d", - tm_tmp->tm_mon + 1, - tm_tmp->tm_mday, - tm_tmp->tm_year + 1900, - tm_tmp->tm_hour, - tm_tmp->tm_min, - tm_tmp->tm_sec, - msecs); + if (tm_tmp != NULL) { + /* + * XXX - somewhat US-centric here. + */ + g_snprintf(timeStr,sizeof(timeStr),"%02d/%02d/%04d %02d:%02d:%02d.%03d", + tm_tmp->tm_mon + 1, + tm_tmp->tm_mday, + tm_tmp->tm_year + 1900, + tm_tmp->tm_hour, + tm_tmp->tm_min, + tm_tmp->tm_sec, + msecs); + } else { + g_snprintf(timeStr,sizeof(timeStr),"XX/XX/XXXX XX:XX:XX.XXX", + } /* Default to using black on white text if nothing below overrides it */ g_snprintf(color_str,sizeof(color_str),"#ffffffffffff"); diff --git a/ui/gtk/io_stat.c b/ui/gtk/io_stat.c index 7f3f4eee7c..2f16332506 100644 --- a/ui/gtk/io_stat.c +++ b/ui/gtk/io_stat.c @@ -488,15 +488,18 @@ print_interval_string(char *buf, int buf_len, guint32 interval, io_stat_t *io, nsec_val -= 1000; } tmp = localtime (&sec_val); - if (io->interval >= 1000) { - g_snprintf(buf, buf_len, "%02d:%02d:%02d", tmp->tm_hour, tmp->tm_min, tmp->tm_sec); - } else if (io->interval >= 100) { - g_snprintf(buf, buf_len, "%02d:%02d:%02d.%1d", tmp->tm_hour, tmp->tm_min, tmp->tm_sec, nsec_val/100); - } else if (io->interval >= 10) { - g_snprintf(buf, buf_len, "%02d:%02d:%02d.%02d", tmp->tm_hour, tmp->tm_min, tmp->tm_sec, nsec_val/10); - } else { - g_snprintf(buf, buf_len, "%02d:%02d:%02d.%03d", tmp->tm_hour, tmp->tm_min, tmp->tm_sec, nsec_val); - } + if (tmp != NULL) { + if (io->interval >= 1000) { + g_snprintf(buf, buf_len, "%02d:%02d:%02d", tmp->tm_hour, tmp->tm_min, tmp->tm_sec); + } else if (io->interval >= 100) { + g_snprintf(buf, buf_len, "%02d:%02d:%02d.%1d", tmp->tm_hour, tmp->tm_min, tmp->tm_sec, nsec_val/100); + } else if (io->interval >= 10) { + g_snprintf(buf, buf_len, "%02d:%02d:%02d.%02d", tmp->tm_hour, tmp->tm_min, tmp->tm_sec, nsec_val/10); + } else { + g_snprintf(buf, buf_len, "%02d:%02d:%02d.%03d", tmp->tm_hour, tmp->tm_min, tmp->tm_sec, nsec_val); + } + } else + g_snprintf(buf, buf_len, "Time not representable"); } else { if (!ext) { g_snprintf(buf, buf_len, "%d.%03d", interval/1000,interval%1000); diff --git a/ui/gtk/main_welcome.c b/ui/gtk/main_welcome.c index 696446f7b4..5debeb0fa0 100644 --- a/ui/gtk/main_welcome.c +++ b/ui/gtk/main_welcome.c @@ -330,7 +330,7 @@ welcome_header_set_message(gchar *msg) { if (msg) { g_string_append(message, msg); } else { /* Use our default header */ - if ((now->tm_mon == 3 && now->tm_mday == 1) || (now->tm_mon == 6 && now->tm_mday == 14)) { + if (now != NULL && ((now->tm_mon == 3 && now->tm_mday == 1) || (now->tm_mon == 6 && now->tm_mday == 14))) { g_string_append(message, "Sniffing the glue that holds the Internet together"); } else { g_string_append(message, prefs.gui_start_title); diff --git a/ui/gtk/memory_dlg.c b/ui/gtk/memory_dlg.c index af06667076..d11122c2ae 100644 --- a/ui/gtk/memory_dlg.c +++ b/ui/gtk/memory_dlg.c @@ -130,15 +130,18 @@ print_interval_string(char *buf, int buf_len, guint32 interval, io_stat_t *io) nsec_val -= 1000; } tmp = localtime (&sec_val); - if (INTERVAL >= 1000) { - g_snprintf(buf, buf_len, "%02d:%02d:%02d", tmp->tm_hour, tmp->tm_min, tmp->tm_sec); - } else if (INTERVAL >= 100) { - g_snprintf(buf, buf_len, "%02d:%02d:%02d.%1d", tmp->tm_hour, tmp->tm_min, tmp->tm_sec, nsec_val/100); - } else if (INTERVAL >= 10) { - g_snprintf(buf, buf_len, "%02d:%02d:%02d.%02d", tmp->tm_hour, tmp->tm_min, tmp->tm_sec, nsec_val/10); - } else { - g_snprintf(buf, buf_len, "%02d:%02d:%02d.%03d", tmp->tm_hour, tmp->tm_min, tmp->tm_sec, nsec_val); - } + if (tmp != NULL) { + if (INTERVAL >= 1000) { + g_snprintf(buf, buf_len, "%02d:%02d:%02d", tmp->tm_hour, tmp->tm_min, tmp->tm_sec); + } else if (INTERVAL >= 100) { + g_snprintf(buf, buf_len, "%02d:%02d:%02d.%1d", tmp->tm_hour, tmp->tm_min, tmp->tm_sec, nsec_val/100); + } else if (INTERVAL >= 10) { + g_snprintf(buf, buf_len, "%02d:%02d:%02d.%02d", tmp->tm_hour, tmp->tm_min, tmp->tm_sec, nsec_val/10); + } else { + g_snprintf(buf, buf_len, "%02d:%02d:%02d.%03d", tmp->tm_hour, tmp->tm_min, tmp->tm_sec, nsec_val); + } + } else + g_snprintf(buf, buf_len, "XX:XX:XX"); } static void diff --git a/ui/gtk/rtp_analysis.c b/ui/gtk/rtp_analysis.c index 239f8569da..0089624909 100644 --- a/ui/gtk/rtp_analysis.c +++ b/ui/gtk/rtp_analysis.c @@ -570,14 +570,20 @@ rtp_packet_add_info(GtkWidget *list, user_data_t * user_data, then = pinfo->abs_ts.secs; msecs = (guint16)(pinfo->abs_ts.nsecs/1000000); tm_tmp = localtime(&then); - g_snprintf(timeStr, sizeof(timeStr), "%02d/%02d/%04d %02d:%02d:%02d.%03d", - tm_tmp->tm_mon + 1, - tm_tmp->tm_mday, - tm_tmp->tm_year + 1900, - tm_tmp->tm_hour, - tm_tmp->tm_min, - tm_tmp->tm_sec, - msecs); + if (tm_tmp != NULL) { + /* + * XXX - somewhat US-centric. + */ + g_snprintf(timeStr, sizeof(timeStr), "%02d/%02d/%04d %02d:%02d:%02d.%03d", + tm_tmp->tm_mon + 1, + tm_tmp->tm_mday, + tm_tmp->tm_year + 1900, + tm_tmp->tm_hour, + tm_tmp->tm_min, + tm_tmp->tm_sec, + msecs); + } else + g_snprintf(timeStr, sizeof(timeStr), "XX/XX/XXXX XX:XX:XX.XXX"); /* Default to using black on white text if nothing below overrides it */ g_snprintf(color_str, sizeof(color_str), "#ffffffffffff"); diff --git a/ui/gtk/rtp_player.c b/ui/gtk/rtp_player.c index 7d051093ad..aaa029a51e 100644 --- a/ui/gtk/rtp_player.c +++ b/ui/gtk/rtp_player.c @@ -1303,7 +1303,10 @@ channel_draw(rtp_channel_info_t *rci) if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cb_view_as_time_of_day))) { seconds = rci->start_time_abs.secs + i * MULT / sample_rate; timestamp = localtime(&seconds); - g_snprintf(label_string, MAX_TIME_LABEL, "%02d:%02d:%02d", timestamp->tm_hour, timestamp->tm_min, timestamp->tm_sec); + if (timestamp != NULL + g_snprintf(label_string, MAX_TIME_LABEL, "%02d:%02d:%02d", timestamp->tm_hour, timestamp->tm_min, timestamp->tm_sec); + else + g_snprintf(label_string, MAX_TIME_LABEL, "XX:XX:XX"); } else { g_snprintf(label_string, MAX_TIME_LABEL, "%.0f s", floor(nstime_to_sec(&rci->start_time_abs)) + i*MULT/sample_rate); } @@ -1451,7 +1454,10 @@ channel_draw(rtp_channel_info_t *rci) if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cb_view_as_time_of_day))) { seconds = rci->start_time_abs.secs + i * MULT / sample_rate; timestamp = localtime(&seconds); - g_snprintf(label_string, MAX_TIME_LABEL, "%02d:%02d:%02d", timestamp->tm_hour, timestamp->tm_min, timestamp->tm_sec); + if (timestamp != NULL) + g_snprintf(label_string, MAX_TIME_LABEL, "%02d:%02d:%02d", timestamp->tm_hour, timestamp->tm_min, timestamp->tm_sec); + else + g_snprintf(label_string, MAX_TIME_LABEL, "XX:XX:XX"); } else { g_snprintf(label_string, MAX_TIME_LABEL, "%.0f s", floor(nstime_to_sec(&rci->start_time_abs)) + i*MULT/sample_rate); } diff --git a/ui/text_import.c b/ui/text_import.c index 93db8a2f93..0536028a5b 100644 --- a/ui/text_import.c +++ b/ui/text_import.c @@ -938,6 +938,7 @@ text_import(text_import_info_t *info) packet_start = 0; packet_preamble_len = 0; ts_sec = time(0); /* initialize to current time */ + /* We trust the OS not to provide a time before the Epoch. */ timecode_default = *localtime(&ts_sec); timecode_default.tm_isdst = -1; /* Unknown for now, depends on time given to the strptime() function */ ts_usec = 0; diff --git a/wiretap/commview.c b/wiretap/commview.c index f44dbe5578..e6dbd8101f 100644 --- a/wiretap/commview.c +++ b/wiretap/commview.c @@ -422,13 +422,26 @@ static gboolean commview_dump(wtap_dumper *wdh, cv_hdr.version = 0; tm = localtime(&phdr->ts.secs); - cv_hdr.year = tm->tm_year + 1900; - cv_hdr.month = tm->tm_mon + 1; - cv_hdr.day = tm->tm_mday; - cv_hdr.hours = tm->tm_hour; - cv_hdr.minutes = tm->tm_min; - cv_hdr.seconds = tm->tm_sec; - cv_hdr.usecs = GUINT32_TO_LE(phdr->ts.nsecs / 1000); + if (tm != NULL) { + cv_hdr.year = tm->tm_year + 1900; + cv_hdr.month = tm->tm_mon + 1; + cv_hdr.day = tm->tm_mday; + cv_hdr.hours = tm->tm_hour; + cv_hdr.minutes = tm->tm_min; + cv_hdr.seconds = tm->tm_sec; + cv_hdr.usecs = GUINT32_TO_LE(phdr->ts.nsecs / 1000); + } else { + /* + * Second before the Epoch. + */ + cv_hdr.year = 1969; + cv_hdr.month = 12; + cv_hdr.day = 31; + cv_hdr.hours = 23; + cv_hdr.minutes = 59; + cv_hdr.seconds = 59; + cv_hdr.usecs = 0; + } switch(phdr->pkt_encap) { diff --git a/wiretap/logcat_text.c b/wiretap/logcat_text.c index 551358170f..2f7e5bc294 100644 --- a/wiretap/logcat_text.c +++ b/wiretap/logcat_text.c @@ -104,6 +104,7 @@ static gchar *logcat_log(const struct dumper_t *dumper, guint32 seconds, { gchar time_buffer[15]; time_t datetime; + struct tm *tm; datetime = (time_t) seconds; @@ -123,20 +124,38 @@ static gchar *logcat_log(const struct dumper_t *dumper, guint32 seconds, return g_strdup_printf("%c(%5i:%5i) %s\n", priority, pid, tid, log); case WTAP_ENCAP_LOGCAT_TIME: - strftime(time_buffer, sizeof(time_buffer), "%m-%d %H:%M:%S", - gmtime(&datetime)); - return g_strdup_printf("%s.%03i %c/%-8s(%5i): %s\n", - time_buffer, milliseconds, priority, tag, pid, log); + tm = gmtime(&datetime); + if (tm != NULL) { + strftime(time_buffer, sizeof(time_buffer), "%m-%d %H:%M:%S", + tm); + return g_strdup_printf("%s.%03i %c/%-8s(%5i): %s\n", + time_buffer, milliseconds, priority, tag, pid, log); + } else { + return g_strdup_printf("Not representable %c/%-8s(%5i): %s\n", + priority, tag, pid, log); + } case WTAP_ENCAP_LOGCAT_THREADTIME: - strftime(time_buffer, sizeof(time_buffer), "%m-%d %H:%M:%S", - gmtime(&datetime)); - return g_strdup_printf("%s.%03i %5i %5i %c %-8s: %s\n", - time_buffer, milliseconds, pid, tid, priority, tag, log); + tm = gmtime(&datetime); + if (tm != NULL) { + strftime(time_buffer, sizeof(time_buffer), "%m-%d %H:%M:%S", + tm); + return g_strdup_printf("%s.%03i %5i %5i %c %-8s: %s\n", + time_buffer, milliseconds, pid, tid, priority, tag, log); + } else { + return g_strdup_printf("Not representable %5i %5i %c %-8s: %s\n", + pid, tid, priority, tag, log); + } case WTAP_ENCAP_LOGCAT_LONG: - strftime(time_buffer, sizeof(time_buffer), "%m-%d %H:%M:%S", - gmtime(&datetime)); - return g_strdup_printf("[ %s.%03i %5i:%5i %c/%-8s ]\n%s\n\n", - time_buffer, milliseconds, pid, tid, priority, tag, log); + tm = gmtime(&datetime); + if (tm != NULL) { + strftime(time_buffer, sizeof(time_buffer), "%m-%d %H:%M:%S", + tm); + return g_strdup_printf("[ %s.%03i %5i:%5i %c/%-8s ]\n%s\n\n", + time_buffer, milliseconds, pid, tid, priority, tag, log); + } else { + return g_strdup_printf("[ Not representable %5i:%5i %c/%-8s ]\n%s\n\n", + pid, tid, priority, tag, log); + } default: return NULL; } diff --git a/wiretap/network_instruments.c b/wiretap/network_instruments.c index 19ff6fbba8..fb335f445c 100644 --- a/wiretap/network_instruments.c +++ b/wiretap/network_instruments.c @@ -614,6 +614,7 @@ gboolean network_instruments_dump_open(wtap_dumper *wdh, int *err) /* create the file comment TLV */ { time(&system_time); + /* We trusst the OS not to return a time before the Epoch */ current_time = localtime(&system_time); memset(&comment, 0x00, sizeof(comment)); g_snprintf(comment, 64, "This capture was saved from Wireshark on %s", asctime(current_time)); diff --git a/wsutil/tempfile.c b/wsutil/tempfile.c index 7391fbfc76..5900b2bb8a 100644 --- a/wsutil/tempfile.c +++ b/wsutil/tempfile.c @@ -204,6 +204,7 @@ create_tempfile(char **namebuf, const char *pfx, const char *sfx) _tzset(); #endif current_time = time(NULL); + /* We trust the OS not to return a time before the Epoch. */ strftime(timestr, sizeof(timestr), "%Y%m%d%H%M%S", localtime(¤t_time)); sep[0] = G_DIR_SEPARATOR; tmp_file = g_strconcat(tmp_dir, sep, safe_pfx, "_", timestr, "_", TMP_FILE_SUFFIX, sfx, NULL);