X-Git-Url: https://git.exim.org/exim.git/blobdiff_plain/8768d5483a5894400ae1f70cda1beb44ed9b087c..1d717e1c110562fd6bf28478c79f180cafeba776:/src/src/tod.c diff --git a/src/src/tod.c b/src/src/tod.c index b0d3ac9b0..e7123f9eb 100644 --- a/src/src/tod.c +++ b/src/src/tod.c @@ -62,19 +62,19 @@ gettimeofday(&now, NULL); switch(type) { case tod_epoch: - (void) sprintf(CS timebuf, TIME_T_FMT, now.tv_sec); /* Unix epoch format */ + (void) snprintf(CS timebuf, sizeof(timebuf), TIME_T_FMT, now.tv_sec); /* Unix epoch format */ return timebuf; /* NB the above will be wrong if time_t is FP */ case tod_epoch_l: /* Unix epoch/usec format */ - (void) sprintf(CS timebuf, TIME_T_FMT "%06ld", now.tv_sec, (long) now.tv_usec ); + (void) snprintf(CS timebuf, sizeof(timebuf), TIME_T_FMT "%06ld", now.tv_sec, (long) now.tv_usec ); return timebuf; case tod_zulu: t = gmtime(&now.tv_sec); - (void) sprintf(CS timebuf, "%04d%02d%02d%02d%02d%02dZ", - 1900 + t->tm_year, 1 + t->tm_mon, t->tm_mday, t->tm_hour, t->tm_min, - t->tm_sec); + (void) snprintf(CS timebuf, sizeof(timebuf), "%04u%02u%02u%02u%02u%02uZ", + 1900 + (uint)t->tm_year, 1 + (uint)t->tm_mon, (uint)t->tm_mday, (uint)t->tm_hour, (uint)t->tm_min, + (uint)t->tm_sec); return timebuf; } @@ -91,14 +91,15 @@ switch(type) case tod_log_bare: /* Format used in logging without timezone */ #ifndef COMPILE_UTILITY if (LOGGING(millisec)) - sprintf(CS timebuf, "%04d-%02d-%02d %02d:%02d:%02d.%03d", - 1900 + t->tm_year, 1 + t->tm_mon, t->tm_mday, - t->tm_hour, t->tm_min, t->tm_sec, (int)(now.tv_usec/1000)); + snprintf(CS timebuf, sizeof(timebuf), "%04u-%02u-%02u %02u:%02u:%02u.%03u", + 1900 + (uint)t->tm_year, 1 + (uint)t->tm_mon, (uint)t->tm_mday, + (uint)t->tm_hour, (uint)t->tm_min, (uint)t->tm_sec, + (uint)(now.tv_usec/1000)); else #endif - sprintf(CS timebuf, "%04d-%02d-%02d %02d:%02d:%02d", - 1900 + t->tm_year, 1 + t->tm_mon, t->tm_mday, - t->tm_hour, t->tm_min, t->tm_sec); + snprintf(CS timebuf, sizeof(timebuf), "%04u-%02u-%02u %02u:%02u:%02u", + 1900 + (uint)t->tm_year, 1 + (uint)t->tm_mon, (uint)t->tm_mday, + (uint)t->tm_hour, (uint)t->tm_min, (uint)t->tm_sec); break; @@ -108,20 +109,21 @@ switch(type) #ifdef TESTING_LOG_DATESTAMP case tod_log_datestamp_daily: case tod_log_datestamp_monthly: - sprintf(CS timebuf, "%04d%02d%02d%02d%02d", - 1900 + t->tm_year, 1 + t->tm_mon, t->tm_mday, t->tm_hour, t->tm_min); + snprintf(CS timebuf, sizeof(timebuf), "%04u%02u%02u%02u%02u", + 1900 + (uint)t->tm_year, 1 + (uint)t->tm_mon, (uint)t->tm_mday, + (uint)t->tm_hour, (uint)t->tm_min); break; #else case tod_log_datestamp_daily: - sprintf(CS timebuf, "%04d%02d%02d", - 1900 + t->tm_year, 1 + t->tm_mon, t->tm_mday); + snprintf(CS timebuf, sizeof(timebuf), "%04u%02u%02u", + 1900 + (uint)t->tm_year, 1 + (uint)t->tm_mon, (uint)t->tm_mday); break; case tod_log_datestamp_monthly: #ifndef COMPILE_UTILITY - sprintf(CS timebuf, "%04d%02d", - 1900 + t->tm_year, 1 + t->tm_mon); + snprintf(CS timebuf, sizeof(timebuf), "%04u%02u", + 1900 + (uint)t->tm_year, 1 + (uint)t->tm_mon); #endif break; #endif @@ -168,22 +170,22 @@ switch(type) case tod_log_zone: /* Format used in logging with timezone */ #ifndef COMPILE_UTILITY if (LOGGING(millisec)) - (void) sprintf(CS timebuf, - "%04d-%02d-%02d %02d:%02d:%02d.%03d %+03d%02d", - 1900 + local.tm_year, 1 + local.tm_mon, local.tm_mday, - local.tm_hour, local.tm_min, local.tm_sec, (int)(now.tv_usec/1000), + (void) snprintf(CS timebuf, sizeof(timebuf), + "%04u-%02u-%02u %02u:%02u:%02u.%03u %+03d%02d", + 1900 + (uint)local.tm_year, 1 + (uint)local.tm_mon, (uint)local.tm_mday, + (uint)local.tm_hour, (uint)local.tm_min, (uint)local.tm_sec, (uint)(now.tv_usec/1000), diff_hour, diff_min); else #endif - (void) sprintf(CS timebuf, - "%04d-%02d-%02d %02d:%02d:%02d %+03d%02d", - 1900 + local.tm_year, 1 + local.tm_mon, local.tm_mday, - local.tm_hour, local.tm_min, local.tm_sec, + (void) snprintf(CS timebuf, sizeof(timebuf), + "%04u-%02u-%02u %02u:%02u:%02u %+03d%02d", + 1900 + (uint)local.tm_year, 1 + (uint)local.tm_mon, (uint)local.tm_mday, + (uint)local.tm_hour, (uint)local.tm_min, (uint)local.tm_sec, diff_hour, diff_min); break; case tod_zone: /* Just the timezone offset */ - (void) sprintf(CS timebuf, "%+03d%02d", diff_hour, diff_min); + (void) snprintf(CS timebuf, sizeof(timebuf), "%+03d%02d", diff_hour, diff_min); break; /* tod_mbx: format used in MBX mailboxes - subtly different to tod_full */ @@ -192,11 +194,11 @@ switch(type) case tod_mbx: { int len; - (void) sprintf(CS timebuf, "%02d-", local.tm_mday); + (void) snprintf(CS timebuf, sizeof(timebuf), "%02u-", (uint)local.tm_mday); len = Ustrlen(timebuf); len += Ustrftime(timebuf + len, sizeof(timebuf) - len, "%b-%Y %H:%M:%S", &local); - (void) sprintf(CS timebuf + len, " %+03d%02d", diff_hour, diff_min); + (void) snprintf(CS timebuf + len, sizeof(timebuf)-len, " %+03d%02d", diff_hour, diff_min); } break; #endif @@ -207,11 +209,11 @@ switch(type) default: { int len = Ustrftime(timebuf, sizeof(timebuf), "%a, ", &local); - (void) sprintf(CS timebuf + len, "%02d ", local.tm_mday); + (void) snprintf(CS timebuf + len, sizeof(timebuf)-len, "%02u ", (uint)local.tm_mday); len += Ustrlen(timebuf + len); len += Ustrftime(timebuf + len, sizeof(timebuf) - len, "%b %Y %H:%M:%S", &local); - (void) sprintf(CS timebuf + len, " %+03d%02d", diff_hour, diff_min); + (void) snprintf(CS timebuf + len, sizeof(timebuf)-len, " %+03d%02d", diff_hour, diff_min); } break; }