[chrony-dev] Avoid large times in chronyc sources / sourcestats overflowing

[ Thread Index | Date Index | More chrony.tuxfamily.org/chrony-dev Archives ]



The attached patch changes the formatting of (nano)seconds such that very large time offsets does not cause the lines of 'sources' or 'sourcestats' to overflow. Root cause was the conversion from double to int that gave misleading values, fooing the following logics. %f formatting in printf rounds by itself.

Cheers,
Håkan
From 707803a7f8e70a53e56e97834d457a66a1e225d8 Mon Sep 17 00:00:00 2001
From: H J <a@b.c>
Date: Sun, 28 Mar 2010 17:40:17 +0200
Subject: [PATCH] Avoid large times in chronyc sources / sourcestats overflowing lines.
 Main trouble was double values too large to be represented as ints being
 converted to -INT_MAX and then passing the < 9999 cut.


diff --git a/client.c b/client.c
index 8ad8ba6..3673f96 100644
--- a/client.c
+++ b/client.c
@@ -1483,21 +1483,19 @@ print_seconds(unsigned long s)
 static void
 print_nanoseconds(double s)
 {
-  unsigned long ms, ns;
-
-  ns = s * 1e9 + 0.5;
-  ms = s * 1e3 + 0.5;
-
-  if (ns <= 9999) {
-    printf("%4ldns", ns);
-  } else if (ns <= 9999499) {
-    printf("%4ldus", (ns + 500) / 1000);
-  } else if (ms <= 9999) {
-    printf("%4ldms", ms);
-  } else if (ms <= 999949) {
-    printf("%3ld.%01lds", (ms + 50) / 1000, ((ms + 50) / 100) % 10);
+  if (fabs(s) <= 9999e-9) {
+    printf("%4.0fns",s * 1e9);
+  } else if (fabs(s) <= 9999e-6) {
+    printf("%4.0fus",s * 1e6);
+  } else if (fabs(s) <= 9999e-3) {
+    printf("%4.0fms",s * 1e3);
+  } else if (fabs(s) <= 999) {
+    printf("%5.1fs",s);
+  } else if (fabs(s) <= 1025) {
+    printf("%5.0fs",s);
   } else {
-    printf("%5lds", (ms + 500) / 1000);
+    printf(" ");
+    print_seconds((unsigned long) fabs(s));
   }
 }
 
@@ -1506,26 +1504,29 @@ print_nanoseconds(double s)
 static void
 print_signed_nanoseconds(double s)
 {
-  long ms, ns, sign;
-
-  if (s >= 0.0) {
-    ns = s * 1e9 + 0.5;
-    ms = s * 1e3 + 0.5;
-    sign = 1;
+  if (fabs(s) <= 9999e-9) {
+    printf("%+5.0fns",s * 1e9);
+  } else if (fabs(s) <= 9999e-6) {
+    printf("%+5.0fus",s * 1e6);
+  } else if (fabs(s) <= 9999e-3) {
+    printf("%+5.0fms",s * 1e3);
+  } else if (fabs(s) <= 1025) {
+    printf("%+6.0fs",s);
   } else {
-    ns = -s * 1e9 + 0.5;
-    ms = -s * 1e3 + 0.5;
-    sign = -1;
+    printf("  %c",s < 0 ? '-' : '+');
+    print_seconds((unsigned long) fabs(s));
   }
+}
+
+/* ================================================== */
 
-  if (ns <= 9999) {
-    printf("%+5ldns", ns * sign);
-  } else if (ns <= 9999499) {
-    printf("%+5ldus", (ns + 500) / 1000 * sign);
-  } else if (ms <= 9999) {
-    printf("%+5ldms", ms * sign);
+static void
+print_freq_ppm(double f)
+{
+  if (fabs(f) <= 99999) {
+    printf("%10.3f",f);
   } else {
-    printf("%+6lds", (ms + 500) / 1000 * sign);
+    printf("%10.0f",f);
   }
 }
 
@@ -1713,7 +1714,11 @@ process_cmd_sourcestats(char *line)
 
           printf("%-25s  %2lu  %2lu  ", hostname_buf, n_samples, n_runs);
           print_seconds(span_seconds);
-          printf(" %10.3f %10.3f  ", resid_freq_ppm, skew_ppm);
+          printf(" ");
+          print_freq_ppm(resid_freq_ppm);
+          printf(" ");
+          print_freq_ppm(skew_ppm);
+          printf("  ");
           print_signed_nanoseconds(est_offset);
           printf("  ");
           print_nanoseconds(sd);
-- 
1.5.6.5



Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/