[Git][NTPsec/ntpsec][display-fix] 6 commits: libisc: add some attribute((pure))

Ian Bruene gitlab at mg.gitlab.com
Fri Apr 14 22:01:18 UTC 2017


Ian Bruene pushed to branch display-fix at NTPsec / ntpsec


Commits:
c1fa98a9 by Gary E. Miller at 2017-04-14T14:45:57-07:00
libisc: add some attribute((pure))

- - - - -
5dc21621 by Gary E. Miller at 2017-04-14T14:48:49-07:00
Strok(): add attribute((pure))

- - - - -
0a82cdf5 by Gary E. Miller at 2017-04-14T14:50:52-07:00
auth_findkey(): remove unused function.

- - - - -
f0284a30 by Gary E. Miller at 2017-04-14T14:52:31-07:00
auth_havekey(): remove unused functions and its tests.

- - - - -
20b5738c by Ian Bruene at 2017-04-14T22:00:56+00:00
Fixed 0.000, and filt* display errors. Added relevant tests.

- - - - -
c95e460f by Ian Bruene at 2017-04-14T22:00:57+00:00
Fixed fake digit problem. Scaling functions refuse to scale if inapplicable.

- - - - -


7 changed files:

- include/ntp_stdlib.h
- include/parse.h
- libisc/include/isc/netaddr.h
- libntp/authkeys.c
- pylib/util.py
- tests/libntp/authkeys.c
- tests/pylib/test_util.py


Changes:

=====================================
include/ntp_stdlib.h
=====================================
--- a/include/ntp_stdlib.h
+++ b/include/ntp_stdlib.h
@@ -44,7 +44,6 @@ extern	void	errno_to_str(int, char *, size_t);
 
 /* authkeys.c */
 extern	void	auth_delkeys	(void);
-extern	int	auth_havekey	(keyid_t);
 extern	int	authdecrypt	(keyid_t, uint32_t *, int, int);
 extern	int	authencrypt	(keyid_t, uint32_t *, int);
 extern	int	authhavekey	(keyid_t);
@@ -58,7 +57,6 @@ extern	int	clocktime	(int, int, int, int, int, uint32_t, uint32_t *, uint32_t *)
 extern	void	init_auth	(void);
 extern	void	init_lib	(void);
 extern	void	init_network	(void);
-extern	struct savekey *auth_findkey (keyid_t);
 extern	void	auth_moremem	(int);
 extern	void	auth_prealloc_symkeys(int);
 extern	int	ymd2yd		(int, int, int);


=====================================
include/parse.h
=====================================
--- a/include/parse.h
+++ b/include/parse.h
@@ -292,7 +292,8 @@ extern unsigned int parse_restart (parse_t *, char);
 extern unsigned int parse_addchar (parse_t *, char);
 extern unsigned int parse_end (parse_t *);
 
-extern int Strok (const unsigned char *, const unsigned char *);
+extern int Strok (const unsigned char *, const unsigned char *)
+		__attribute__((pure));
 extern int Stoi (const unsigned char *, long *, int);
 
 extern time_t parse_to_unixtime (clocktime_t *, unsigned long *);


=====================================
libisc/include/isc/netaddr.h
=====================================
--- a/libisc/include/isc/netaddr.h
+++ b/libisc/include/isc/netaddr.h
@@ -27,8 +27,8 @@ struct isc_netaddr {
 };
 
 bool
-isc_netaddr_eqprefix(const isc_netaddr_t *a, const isc_netaddr_t *b,
-		     unsigned int prefixlen);
+isc_netaddr_eqprefix(const isc_netaddr_t *, const isc_netaddr_t *,
+		     unsigned int) __attribute__((pure));
 /*%<
  * Compare the 'prefixlen' most significant bits of the network
  * addresses 'a' and 'b'.  If 'b''s scope is zero then 'a''s scope is
@@ -92,7 +92,7 @@ void
 isc_netaddr_setzone(isc_netaddr_t *netaddr, uint32_t zone);
 
 uint32_t
-isc_netaddr_getzone(const isc_netaddr_t *netaddr);
+isc_netaddr_getzone(const isc_netaddr_t *netaddr) __attribute__((pure));
 
 void
 isc_netaddr_any(isc_netaddr_t *netaddr);
@@ -119,7 +119,7 @@ isc_netaddr_isexperimental(isc_netaddr_t *na);
  */
 
 bool
-isc_netaddr_islinklocal(isc_netaddr_t *na);
+isc_netaddr_islinklocal(isc_netaddr_t *na) __attribute__((pure));
 /*%<
  * Returns #true if the address is a link local address.
  */


=====================================
libntp/authkeys.c
=====================================
--- a/libntp/authkeys.c
+++ b/libntp/authkeys.c
@@ -307,46 +307,6 @@ freesymkey(
 
 
 /*
- * auth_findkey - find a key in the hash table
- */
-struct savekey *
-auth_findkey(
-	keyid_t		id
-	)
-{
-	for (symkey * sk = key_hash[KEYHASH(id)]; sk != NULL; sk = sk->hlink) {
-		if (id == sk->keyid) {
-			return sk;
-		}
-	}
-
-	return NULL;
-}
-
-
-/*
- * auth_havekey - return true if the key id is zero or known
- */
-int
-auth_havekey(
-	keyid_t		id
-	)
-{
-	if (0 == id || cache_keyid == id) {
-		return true;
-	}
-
-	for (symkey * sk = key_hash[KEYHASH(id)]; sk != NULL; sk = sk->hlink) {
-		if (id == sk->keyid) {
-			return true;
-		}
-	}
-
-	return false;
-}
-
-
-/*
  * authhavekey - return true and cache the key, if zero or both known
  *		 and trusted.
  */


=====================================
pylib/util.py
=====================================
--- a/pylib/util.py
+++ b/pylib/util.py
@@ -94,14 +94,19 @@ def stringfiltcooker(data):
     "Cooks a filt* string of space seperated numbers, expects milliseconds"
     parts = data.split()
     oomcount = {}
+    minscale = -100000  # Keep track of the maxdownscale for each value
     # Find out what the 'natural' unit of each value is
     for part in parts:
         value, oom = scalestring(part)
+        ds = maxdownscale(part)
+        minscale = max(ds, minscale)
         oomcount[oom] = oomcount.get(oom, 0) + 1
     # Find the most common unit
-    mostcommon = None
+    mostcommon = 0
     highestcount = 0
     for key in oomcount.keys():
+        if key < minscale:
+            continue  # skip any scale that would result in making up data
         count = oomcount[key]
         if count > highestcount:
             mostcommon = key
@@ -162,6 +167,15 @@ def gluenumberstring(above, below, isnegative):
     return newvalue
 
 
+def maxdownscale(value):
+    "Maximum units a value can be scaled down without inventing data"
+    if "." in value:
+        digitcount = len(value.split(".")[1])
+        return -(digitcount // 3)
+    else:
+        return 0
+
+
 def rescalestring(value, unitsscaled):
     "Rescale a number string by a given number of units"
     whole, dec, negative = breaknumberstring(value)
@@ -173,8 +187,10 @@ def rescalestring(value, unitsscaled):
     hilen = len(whole)
     lolen = len(dec)
     digitsmoved = abs(unitsscaled * 3)
-    if unitsscaled > 0:  # Scale to a larger unit
-        if hilen < digitsmoved:  # Scaling beyond the digits, pad it out
+    if unitsscaled > 0:  # Scale to a larger unit, move decimal left
+        if hilen < digitsmoved:
+            # Scaling beyond the digits, pad it out. We can pad here
+            # without making up digits that don't exist
             padcount = digitsmoved - hilen
             newwhole = "0"
             newdec = ("0" * padcount) + whole + dec
@@ -184,14 +200,16 @@ def rescalestring(value, unitsscaled):
             newwhole = whole[:choppoint]
             if newwhole == "":
                 newwhole = "0"
-    elif unitsscaled < 0:  # scale to a smaller unit
-        if lolen < digitsmoved:  # Scaling beyone the digits, pad it out
-            padcount = digitsmoved - lolen
-            newwhole = whole + dec + ("0" * padcount)
-            newdec = ""
+    elif unitsscaled < 0:  # scale to a smaller unit, move decimal right
+        if lolen < digitsmoved:
+            # Scaling beyond the digits would force us to make up data
+            # that doesn't exist. So fail.
+            # The caller should have already caught this with maxdownscale()
+            return None
         else:
             newwhole = whole + dec[:digitsmoved]
             newdec = dec[digitsmoved:]
+    newwhole = newwhole.lstrip("0")
     newvalue = gluenumberstring(newwhole, newdec, negative)
     return newvalue
 
@@ -215,7 +233,12 @@ def scalestring(value):
         else:
             lounits = (i // 3) + 1  # always need to shift one more unit
             movechars = lounits * 3
-            newwhole = dec[:movechars].lstrip('0')
+            if lolen < movechars:
+                # Not enough digits to scale all the way down. Inventing
+                # digits is unacceptable, so scale down as much as we can.
+                lounits = (i // 3)  # "always", unless out of digits
+                movechars = lounits * 3
+            newwhole = dec[:movechars].lstrip("0")
             newdec = dec[movechars:]
             unitsmoved = -lounits
     else:  # Shift to larger units
@@ -316,8 +339,9 @@ def unitify(value, startingunit, baseunit=None, strip=False, width=8):
         baseunit = getunitgroup(startingunit)[0]
     if isstringzero(value) is True:  # display highest precision zero
         if strip is False:
-            value = fitinfield("0", width - len(baseunit))
-        value = value + baseunit
+            value = fitinfield("0", width - len(baseunit)) + baseunit
+        else:
+            value = "0" + baseunit
         return value
     ooms = oomsbetweenunits(startingunit, baseunit)
     newvalue = cropprecision(value, ooms)


=====================================
tests/libntp/authkeys.c
=====================================
--- a/tests/libntp/authkeys.c
+++ b/tests/libntp/authkeys.c
@@ -79,14 +79,12 @@ TEST(authkeys, HaveKeyCorrect) {
 
 	AddTrustedKey(KEYNO);
 
-	TEST_ASSERT_TRUE(auth_havekey(KEYNO));
 	TEST_ASSERT_TRUE(authhavekey(KEYNO));
 }
 
 TEST(authkeys, HaveKeyIncorrect) {
 	const keyid_t KEYNO = 2;
 
-	TEST_ASSERT_FALSE(auth_havekey(KEYNO));
 	TEST_ASSERT_FALSE(authhavekey(KEYNO));
 }
 


=====================================
tests/pylib/test_util.py
=====================================
--- a/tests/pylib/test_util.py
+++ b/tests/pylib/test_util.py
@@ -37,6 +37,8 @@ class TestPylibUtilMethods(unittest.TestCase):
 
         # Scale all decimals
         self.assertEqual(f("0.042"), ("42", -1))
+        # Unscalable
+        self.assertEqual(f(".23"), ("0.23", 0))
         # Typical length, positive value, no scaling
         self.assertEqual(f("1.23450"), ("1.23450", 0))
         # Ditto, negative
@@ -89,9 +91,11 @@ class TestPylibUtilMethods(unittest.TestCase):
         # ditto, negative
         self.assertEqual(f("-1.23456", -1), "-1234.56")
         # Scale to lower unit, beyond available digits
-        self.assertEqual(f("1.23456", -2), "1234560")
+        self.assertEqual(f("1.23456", -2), None)
         # ditto, negative
-        self.assertEqual(f("-1.23456", -2), "-1234560")
+        self.assertEqual(f("-1.23456", -2), None)
+        # Scale from below the decimal
+        self.assertEqual(f("0.420", -1), "420")
 
     def test_breaknumberstring(self):
         f = ntp.util.breaknumberstring
@@ -204,12 +208,23 @@ class TestPylibUtilMethods(unittest.TestCase):
             "1.02 34.5 0.67835 -23.0 9 6.7 1.00 .1"),
             "   1.02    34.5 0.67835   -23.0       9     6.7    1.00     0.1 ms"
         )
-        # Scale
+        # Scale to larger unit
         self.assertEqual(ntp.util.stringfiltcooker(
             "1000.02 3400.5 0.67835 -23.0 9001 6.7 1.00 1234"),
             "1.00002  3.4005 0.00068 -0.0230   9.001  0.0067 0.00100   1.234 s"
         )
-
+        # Scale to smaller unit
+        self.assertEqual(ntp.util.stringfiltcooker(
+            "0.470 0.420 0.430 0.500 0.460 0.4200 0.490 0.480"),
+            u"    470     420     430     500     460   420.0     490     480 \u03bcs")
+        # Can't scale
+        self.assertEqual(ntp.util.stringfiltcooker(
+            "0.47 0.42 0.43 0.50 0.46 0.42 0.49 0.48"),
+            "   0.47    0.42    0.43    0.50    0.46    0.42    0.49    0.48 ms")
+        # Can't scale, only one value blocking
+        self.assertEqual(ntp.util.stringfiltcooker(
+            "0.47 0.4200 0.4300 0.5000 0.4600 0.4200 0.4900 0.4800"),
+            "   0.47  0.4200  0.4300  0.5000  0.4600  0.4200  0.4900  0.4800 ms")
     def test_unitrelativeto(self):
         f = ntp.util.unitrelativeto
 



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/264d03abe4dd47a0e253b5aac5e0d953a815f8fc...c95e460fc74f67dc8fbcd5464c155b38b34cf12f

---
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/264d03abe4dd47a0e253b5aac5e0d953a815f8fc...c95e460fc74f67dc8fbcd5464c155b38b34cf12f
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/20170414/7c5af7ea/attachment.html>


More information about the vc mailing list