From: Ken Steele Date: Fri, 6 Sep 2013 17:56:18 +0000 (-0400) Subject: Minor optimization in time caching code. X-Git-Tag: suricata-2.0beta2~387 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F535%2Fhead;p=thirdparty%2Fsuricata.git Minor optimization in time caching code. Reduced the size of the cached string buffer from 128 to 32, which is still larger than the largest possible time string, which is 26 characters. Added a check for the user passing in an output buffer that is smaller than the cached string. Previously, the code would have copied past the end of the users buffer. --- diff --git a/src/util-time.c b/src/util-time.c index fb0e682eec..f040dfaca6 100644 --- a/src/util-time.c +++ b/src/util-time.c @@ -148,7 +148,11 @@ void CreateTimeString (const struct timeval *ts, char *str, size_t size) /* On systems supporting __thread, use Per-thread values for caching * in CreateTimeString */ -#define MAX_LOCAL_TIME_STRING 128 +/* The maximum possible length of the time string. + * "%02d/%02d/%02d-%02d:%02d:%02d.%06u" + * Or "01/01/2013-15:42:21.123456", which is 26, so round up to 32. */ +#define MAX_LOCAL_TIME_STRING 32 + static __thread int mru_time_slot; /* Most recently used cached value */ static __thread time_t last_local_time[2]; static __thread short int cached_local_time_len[2]; @@ -267,6 +271,8 @@ void CreateTimeString (const struct timeval *ts, char *str, size_t size) into the return string buffer. */ char *cached_str = cached_local_time[mru_time_slot]; int cached_len = cached_local_time_len[mru_time_slot]; + if (cached_len >= size) + cached_len = size; memcpy(str, cached_str, cached_len); snprintf(str + cached_len, size - cached_len, "%02d.%06u",