MB-21777: max_cas_str not formatting on Linux 98/70298/2
authorJim Walker <jim@couchbase.com>
Wed, 23 Nov 2016 15:28:05 +0000 (15:28 +0000)
committerDave Rigby <daver@couchbase.com>
Thu, 24 Nov 2016 15:04:27 +0000 (15:04 +0000)
Seems an issue with snprintf where we use timeString as input and
output yields only the nanosecond fractions being printed.

Fix also reduces buffers down, they're still bigger than we need, but
100 is way too much.

Change-Id: Ief6755d15d4d77a10a6c14fb76321893a7dd0d9c
Reviewed-on: http://review.couchbase.org/70298
Reviewed-by: David Haikney <david.haikney@couchbase.com>
Tested-by: buildbot <build@couchbase.com>
Reviewed-by: Dave Rigby <daver@couchbase.com>
src/hlc.h

index 2235abd..e7a3d7f 100644 (file)
--- a/src/hlc.h
+++ b/src/hlc.h
@@ -165,19 +165,23 @@ public:
         time_t maxCasSeconds = seconds.count();
 
         std::tm tm;
-        char timeString[100];
+        char timeString[30]; // Need to store 1970-12-31T23:23:59
         // Print as an ISO-8601 date format with nanosecond fractional part
         if (cb_gmtime_r(&maxCasSeconds, &tm) == 0 &&
             strftime(timeString, sizeof(timeString), "%Y-%m-%dT%H:%M:%S", &tm)) {
             // Get the fractional nanosecond part
             nanoseconds -= seconds;
+            char finalString[40];// Needs to store 1970-12-31T23:23:59.999999999
+            const char* maxCasStr = finalString;
             try {
-                checked_snprintf(timeString, sizeof(timeString), "%s.%lld",
+                checked_snprintf(finalString, sizeof(finalString), "%s.%lld",
                                  timeString, nanoseconds.count());
-            } catch (...) { /* genuinely do nothing, we will just see that the
-                        fraction is missing and max_cas is already printed */
+            } catch (...) {
+                // snprint fail, point at timeString which at least has the
+                // majority of the time data.
+                maxCasStr = timeString;
             }
-            add_prefixed_stat(prefix.data(), "max_cas_str", timeString, add_stat, c);
+            add_prefixed_stat(prefix.data(), "max_cas_str", maxCasStr, add_stat, c);
         } else {
             add_prefixed_stat(prefix.data(), "max_cas_str", "could not get string", add_stat, c);
         }