diff --git a/CHANGES b/CHANGES index 322f65d400..f232e80c0f 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,6 @@ +4855. [bug] isc_time_formatshorttimestamp produced incorrect + output. [RT #46938] + 4854. [bug] query_synthcnamewildcard should stop generating the response if query_synthwildcard fails. [RT #46939] diff --git a/lib/isc/tests/time_test.c b/lib/isc/tests/time_test.c index 12a8743d20..d20ed0fabe 100644 --- a/lib/isc/tests/time_test.c +++ b/lib/isc/tests/time_test.c @@ -182,6 +182,37 @@ ATF_TC_BODY(isc_time_formatISO8601Lms, tc) { ATF_CHECK_STREQ(buf, "2015-12-13T01:46:40.123"); } +ATF_TC(isc_time_formatshorttimestamp); +ATF_TC_HEAD(isc_time_formatshorttimestamp, tc) { + atf_tc_set_md_var(tc, "descr", + "print UTC time as yyyymmddhhmmsssss"); +} +ATF_TC_BODY(isc_time_formatshorttimestamp, tc) { + isc_result_t result; + isc_time_t t; + char buf[64]; + + setenv("TZ", "PST8PDT", 1); + result = isc_time_now(&t); + ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); + + /* check formatting: yyyymmddhhmmsssss */ + memset(buf, 'X', sizeof(buf)); + isc_time_formatshorttimestamp(&t, buf, sizeof(buf)); + ATF_CHECK_EQ(strlen(buf), 17); + + /* check time conversion correctness */ + memset(buf, 'X', sizeof(buf)); + isc_time_settoepoch(&t); + isc_time_formatshorttimestamp(&t, buf, sizeof(buf)); + ATF_CHECK_STREQ(buf, "19700101000000000"); + + memset(buf, 'X', sizeof(buf)); + isc_time_set(&t, 1450000000, 123000000); + isc_time_formatshorttimestamp(&t, buf, sizeof(buf)); + ATF_CHECK_STREQ(buf, "20151213094640123"); +} + /* * Main */ @@ -191,6 +222,6 @@ ATF_TP_ADD_TCS(tp) { ATF_TP_ADD_TC(tp, isc_time_formatISO8601ms); ATF_TP_ADD_TC(tp, isc_time_formatISO8601L); ATF_TP_ADD_TC(tp, isc_time_formatISO8601Lms); + ATF_TP_ADD_TC(tp, isc_time_formatshorttimestamp); return (atf_no_error()); } - diff --git a/lib/isc/unix/time.c b/lib/isc/unix/time.c index 589225cac2..eccae669f4 100644 --- a/lib/isc/unix/time.c +++ b/lib/isc/unix/time.c @@ -552,6 +552,9 @@ isc_time_formatshorttimestamp(const isc_time_t *t, char *buf, unsigned int len) struct tm tm; #endif + REQUIRE(t != NULL); + INSIST(t->nanoseconds < NS_PER_S); + REQUIRE(buf != NULL); REQUIRE(len > 0); now = (time_t)t->seconds; @@ -562,7 +565,6 @@ isc_time_formatshorttimestamp(const isc_time_t *t, char *buf, unsigned int len) #endif INSIST(flen < len); if (flen > 0U && len - flen >= 5) { - flen -= 1; /* rewind one character (Z) */ snprintf(buf + flen, len - flen, "%03u", t->nanoseconds / NS_PER_MS); } diff --git a/lib/isc/win32/time.c b/lib/isc/win32/time.c index a4b6e92354..fa00293984 100644 --- a/lib/isc/win32/time.c +++ b/lib/isc/win32/time.c @@ -442,7 +442,10 @@ isc_time_formatshorttimestamp(const isc_time_t *t, char *buf, unsigned int len) /* strtime() format: "%Y%m%d%H%M%SSSS" */ + REQUIRE(t != NULL); + REQUIRE(buf != NULL); REQUIRE(len > 0); + if (FileTimeToSystemTime(&t->absolute, &st)) { GetDateFormat(LOCALE_NEUTRAL, 0, &st, "yyyyMMdd", DateBuf, 50);