coap_debug.c: Use length returned by print_timestamp()

In theory, print_timestamp() could omit the terminating null byte when
the buffer passed as argument is too small or if snprintf() returns -1.
To make coap_log_impl() more robust against unforeseen out-of-bounds
access, the length returned by print_timestamp() now is passed to
fprintf() explicitly.
This commit is contained in:
Olaf Bergmann committed 2020-02-24 11:17:35 +01:00
1 parent a9d5f07661
commit 620e6fe2d4
1 file changed
+4 -2
+4 -2
View File
@@ -800,12 +800,14 @@ coap_log_impl(coap_log_t level, const char *format, ...) {
coap_tick_t now;
va_list ap;
FILE *log_fd;
size_t len;
log_fd = level <= LOG_CRIT ? COAP_ERR_FD : COAP_DEBUG_FD;
coap_ticks(&now);
if (print_timestamp(timebuf,sizeof(timebuf), now))
fprintf(log_fd, "%s ", timebuf);
len = print_timestamp(timebuf,sizeof(timebuf), now);
if (len)
fprintf(log_fd, "%.*s ", (int)len, timebuf);
if (level <= COAP_LOG_CIPHERS)
fprintf(log_fd, "%s ", loglevels[level]);