From 2d226bd3e09417d8f2cf60d92c92b0dd437e4230 Mon Sep 17 00:00:00 2001 From: Ben Longbons Date: Sat, 23 Feb 2013 16:55:53 -0800 Subject: Fix buffer overflow due to off-by-one error --- src/common/utils.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/common/utils.cpp b/src/common/utils.cpp index bd82180..33a96b4 100644 --- a/src/common/utils.cpp +++ b/src/common/utils.cpp @@ -122,17 +122,20 @@ bool split_key_value(const std::string& line, std::string *w1, std::string *w2) return true; } +static_assert(sizeof(timestamp_seconds_buffer) == 20, "seconds buffer"); +static_assert(sizeof(timestamp_milliseconds_buffer) == 24, "millis buffer"); + void stamp_time(timestamp_seconds_buffer& out, time_t *t) { time_t when = t ? *t : time(NULL); - strftime(out, sizeof(out), "%Y-%m-%d %H:%M:%S", gmtime(&when)); + strftime(out, 20, "%Y-%m-%d %H:%M:%S", gmtime(&when)); } void stamp_time(timestamp_milliseconds_buffer& out) { struct timeval tv; gettimeofday(&tv, NULL); - strftime(out, sizeof(out), "%Y-%m-%d %H:%M:%S", gmtime(&tv.tv_sec)); - sprintf(out + 20, ".%03d", int(tv.tv_usec / 1000)); + strftime(out, 20, "%Y-%m-%d %H:%M:%S", gmtime(&tv.tv_sec)); + sprintf(out + 19, ".%03d", int(tv.tv_usec / 1000)); } void log_with_timestamp(FILE *out, const_string line) -- cgit v1.2.3-60-g2f50