[Git][NTPsec/ntpsec][master] Make f8dot3() not crash on bad input.

Gary E. Miller gitlab at mg.gitlab.com
Tue Aug 8 22:29:24 UTC 2017


Gary E. Miller pushed to branch master at NTPsec / ntpsec


Commits:
75c50a7c by Gary E. Miller at 2017-08-08T15:28:20-07:00
Make f8dot3() not crash on bad input.

f8dot3() expect a float, but sometimes gets other things...

- - - - -


1 changed file:

- pylib/util.py


Changes:

=====================================
pylib/util.py
=====================================
--- a/pylib/util.py
+++ b/pylib/util.py
@@ -389,6 +389,7 @@ def f8dot4(f):
 
     if str(float(f)).lower() == 'nan':
         # yes, this is a better test than math.isnan()
+        # it also catches None, strings, etc.
         return "     nan"
 
     fmt = "%8d"          # xxxxxxxx
@@ -402,7 +403,7 @@ def f8dot4(f):
         elif f < 1000000.0:
             fmt = "%8.1f"    # xxxxxx.x
     else:
-        # f < 0
+        # negative number, account for minus sign
         if f > -100.0:
             fmt = "%8.4f"      # -xx.xxxx  normal case
         elif f > -1000.0:
@@ -417,22 +418,29 @@ def f8dot4(f):
 
 def f8dot3(f):
     "Scaled floating point formatting to fit in 8 characters"
+    if str(float(f)).lower() == 'nan':
+        # yes, this is a better test than math.isnan()
+        # it also catches None, strings, etc.
+        return "     nan"
+
+    fmt = "%8d" % f          # xxxxxxxx or -xxxxxxx
     if f >= 0:
         if f < 10000.0:
-            return "%8.3f" % f    # xxxx.xxx  normal case
+            fmt = "%8.3f"    # xxxx.xxx  normal case
         elif f < 100000.0:
-            return "%8.2f" % f    # xxxxx.xx
+            fmt = "%8.2f"    # xxxxx.xx
         elif f < 1000000.0:
-            return "%8.1f" % f    # xxxxxx.x
-        return "%8d" % f          # xxxxxxxx
-
-    if f > -1000.0:
-        return "%8.3f" % f      # -xxx.xxx  normal case
-    elif f > -10000.0:
-        return "%8.2f" % f      # -xxxx.xx
-    elif f > -100000.0:
-        return "%8.1f" % f      # -xxxxx.x
-    return "%8d" % f            # -xxxxxxx
+            fmt = "%8.1f"    # xxxxxx.x
+    else:
+        # negative number, account for minus sign
+        if f > -1000.0:
+            fmt = "%8.3f"    # -xxx.xxx  normal case
+        elif f > -10000.0:
+            fmt = "%8.2f"    # -xxxx.xx
+        elif f > -100000.0:
+            fmt = "%8.1f"    # -xxxxx.x
+
+    return fmt % f 
 
 
 # A hack to avoid repeatedly hammering on DNS when ntpmon runs.



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/75c50a7c0e81332aa81d9e4c614a990cf49d923b

---
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/75c50a7c0e81332aa81d9e4c614a990cf49d923b
You're receiving this email because of your account on gitlab.com.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ntpsec.org/pipermail/vc/attachments/20170808/68050db4/attachment.html>


More information about the vc mailing list