diff options
author | Guy Harris <gharris@sonic.net> | 2023-01-30 23:03:16 -0800 |
---|---|---|
committer | Francois-Xavier Le Bail <devel.fx.lebail@orange.fr> | 2023-02-02 15:43:24 +0100 |
commit | 7578e1c04ee280dda50c4c2813e7d55f539c6501 (patch) | |
tree | d4b27ce6c93663119fd7bc36ca21db68668d9923 /ntp.c | |
parent | 4d0dd4fbc300188657ad437a1ce794606a87cda8 (diff) | |
download | tcpdump-7578e1c04ee280dda50c4c2813e7d55f539c6501.tar.gz |
Have a common routine for converting dates and times to strings.
Have a routine that takes a buffer, a strftime format, and a struct tm *
as arguments, and:
* checks whether the struct tm * is null and, if so, returns a string
indicating that the date and time couldn't be converted;
* otherwise, passes it to strftime(), along with the buffer and the
format argument and, if strftime() returns 0, meaning the string didn't
fit into the buffer and thus that the buffer's contents are undefined,
returns a string indicating that the date and time didn't fit into the
buffer;
* otherwise, returns a pointer to the buffer.
Call that routine instead of directly calling strftime() in printers;
that prevents printing a buffer with undefined data if the buffer isn't
big enough for the string.
Also, when generating file names using an strftime format, check the
return value of strftime() to make sure the buffer didn't overflow.
Diffstat (limited to 'ntp.c')
-rw-r--r-- | ntp.c | 21 |
1 files changed, 6 insertions, 15 deletions
@@ -54,8 +54,8 @@ p_ntp_time(netdissect_options *ndo, if (i) { int64_t seconds_64bit = (int64_t)i - JAN_1970; time_t seconds; - struct tm *tm; char time_buf[128]; + const char *time_string; seconds = (time_t)seconds_64bit; if (seconds != seconds_64bit) { @@ -63,21 +63,12 @@ p_ntp_time(netdissect_options *ndo, * It doesn't fit into a time_t, so we can't hand it * to gmtime. */ - ND_PRINT(" (unrepresentable)"); + time_string = "[Time is too large to fit into a time_t]"; } else { - tm = gmtime(&seconds); - if (tm == NULL) { - /* - * gmtime() can't handle it. - * (Yes, that might happen with some version of - * Microsoft's C library.) - */ - ND_PRINT(" (unrepresentable)"); - } else { - /* use ISO 8601 (RFC3339) format */ - strftime(time_buf, sizeof (time_buf), "%Y-%m-%dT%H:%M:%SZ", tm); - ND_PRINT(" (%s)", time_buf); - } + /* use ISO 8601 (RFC3339) format */ + time_string = nd_format_time(time_buf, sizeof (time_buf), + "%Y-%m-%dT%H:%M:%SZ", gmtime(&seconds)); } + ND_PRINT(" (%s)", time_string); } } |