From 7820158f937533b53f8ca01356de015a3da49e11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20M=C3=A4kel=C3=A4?= Date: Mon, 23 Apr 2018 18:21:58 +0300 Subject: [PATCH] Properly fix string truncation in snprint_timestamp The length of the timestamp string had a practical meaning. The correct fix was to always use the length passed as the parameter. --- server/core/internal/skygw_utils.h | 5 ++--- server/core/skygw_utils.cc | 4 ++-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/server/core/internal/skygw_utils.h b/server/core/internal/skygw_utils.h index 6fca7cc1c..d7b306545 100644 --- a/server/core/internal/skygw_utils.h +++ b/server/core/internal/skygw_utils.h @@ -46,12 +46,11 @@ typedef enum { MES_RC_FAIL, MES_RC_SUCCESS, MES_RC_TIMEOUT } skygw_mes_rc_t; static const char* timestamp_formatstr = "%04d-%02d-%02d %02d:%02d:%02d "; /** One for terminating '\0' */ -static const size_t timestamp_len = (4 + 1 + 2 + 1 + 2 + 1 + 2 + 1 + 2 + 1 + 2 + 3 + 1 + 16) * sizeof(char); +static const size_t timestamp_len = (4 + 1 + 2 + 1 + 2 + 1 + 2 + 1 + 2 + 1 + 2 + 3 + 1); static const char* timestamp_formatstr_hp = "%04d-%02d-%02d %02d:%02d:%02d.%03d "; /** One for terminating '\0' */ -static const size_t timestamp_len_hp = (4 + 1 + 2 + 1 + 2 + 1 + 2 + 1 + 2 + 1 + 2 + 1 + 3 + 3 + 1 + 16) * - sizeof(char); +static const size_t timestamp_len_hp = (4 + 1 + 2 + 1 + 2 + 1 + 2 + 1 + 2 + 1 + 2 + 1 + 3 + 3 + 1); struct skygw_thread_st { diff --git a/server/core/skygw_utils.cc b/server/core/skygw_utils.cc index d5aef14f8..48c0c6d9e 100644 --- a/server/core/skygw_utils.cc +++ b/server/core/skygw_utils.cc @@ -84,7 +84,7 @@ size_t snprint_timestamp(char* p_ts, size_t tslen) t = time(NULL); localtime_r(&t, &tm); - snprintf(p_ts, MXS_MIN(tslen, timestamp_len), timestamp_formatstr, + snprintf(p_ts, tslen, timestamp_formatstr, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec); rval = strlen(p_ts) * sizeof (char); @@ -125,7 +125,7 @@ size_t snprint_timestamp_hp(char* p_ts, size_t tslen) gettimeofday(&tv, NULL); localtime_r(&tv.tv_sec, &tm); usec = tv.tv_usec / 1000; - snprintf(p_ts, MXS_MIN(tslen, timestamp_len_hp), timestamp_formatstr_hp, + snprintf(p_ts, tslen, timestamp_formatstr_hp, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, usec); rval = strlen(p_ts) * sizeof (char);