[Git][NTPsec/ntpsec][master] 2 commits: Added guards to command line args that take ints. Added guard function.

Eric S. Raymond gitlab at mg.gitlab.com
Thu May 11 20:57:00 UTC 2017


Eric S. Raymond pushed to branch master at NTPsec / ntpsec


Commits:
e75a7115 by Ian Bruene at 2017-05-11T15:24:42-05:00
Added guards to command line args that take ints. Added guard function.

- - - - -
f2e05695 by Ian Bruene at 2017-05-11T15:31:38-05:00
Missed one!

- - - - -


6 changed files:

- ntpclients/ntpdig
- ntpclients/ntpq
- ntpclients/ntpsweep
- ntpclients/ntptrace
- ntpclients/ntpwait
- pylib/util.py


Changes:

=====================================
ntpclients/ntpdig
=====================================
--- a/ntpclients/ntpdig
+++ b/ntpclients/ntpdig
@@ -299,13 +299,15 @@ if __name__ == '__main__':
             elif switch in ("-6", "--ipv6"):
                 af = socket.AF_INET6
             elif switch in ("-a", "--authentication"):
-                authkey = int(val)
+                errmsg = "Error: -a parameter '%s' not a number\n"
+                authkey = ntp.util.safeargcast(val, int, errmsg, usage)
             elif switch in ("-c", "--concurrent"):
                 concurrent_hosts.append(val)
             elif switch in ("-d", "--debug"):
                 debug += 1
             elif switch in ("-D", "--set-debug-level"):
-                debug = int(val)
+                errmsg = "Error: -D parameter '%s' not a number\n"
+                debug = ntp.util.safeargcast(val, int, errmsg, usage)
             elif switch in ("-j", "--json"):
                 json = True
             elif switch in ("-k", "--keyfile"):
@@ -317,9 +319,11 @@ if __name__ == '__main__':
                     sys.stderr.write("logfile open of %s failed.\n" % val)
                     raise SystemExit(1)
             elif switch in ("-M", "--steplimit"):
-                steplimit = int(val)
+                errmsg = "Error: -M parameter '%s' not a number\n"
+                steplimit = ntp.util.safeargcast(val, int, errmsg, usage)
             elif switch in ("-p", "--samples"):
-                samples = int(val)
+                errmsg = "Error: -p parameter '%s' not a number\n"
+                samples = ntp.util.safeargcast(val, int, errmsg, usage)
             elif switch in ('-r', "--replay"):
                 replay = val
             elif switch in ("-S", "--step"):
@@ -327,7 +331,8 @@ if __name__ == '__main__':
             elif switch in ("-s", "--slew"):
                 slew = True
             elif switch in ("-t", "--timeout"):
-                timeout = int(val)
+                errmsg = "Error: -t parameter '%s' not a number\n"
+                timeout = ntp.util.safeargcast(val, int, errmsg, usage)
             elif switch in ("-h", "--help"):
                 print(usage)
                 raise SystemExit(0)


=====================================
ntpclients/ntpq
=====================================
--- a/ntpclients/ntpq
+++ b/ntpclients/ntpq
@@ -1623,12 +1623,9 @@ if __name__ == '__main__':
             session.debug += 1
             session.logfp = open("ntpq.log", "a", 1)    # 1 => line buffered
         elif switch in ("-D", "--set-debug-level"):
-            try:
-                session.debug = interpreter.debug = int(val)
-            except ValueError as e:
-                sys.stderr.write("'%s' is not a recognizable number\n" % val)
-                sys.stderr.write(usage)
-                raise SystemExit(1)
+            errmsg = "Error: -D parameter '%s' not a number\n"
+            cast = ntp.util.safeargcast(val, int, errmsg, usage)
+            session.debug = interpreter.debug = cast
         elif switch in ("-h", "--help"):
             print(usage)
             raise SystemExit(0)
@@ -1644,13 +1641,9 @@ if __name__ == '__main__':
         elif switch in ("-w", "--wide"):
             interpreter.wideremote = True
         elif switch in ("-W", "--width"):
-            try:
-                interpreter.termwidth = int(val)
-            except ValueError as e:
-                sys.stderr.write("Error: -W parameter '%s' not a number\n"
-                                 % val)
-                sys.stderr.write(usage)
-                raise SystemExit(1)
+            errmsg = "Error: -W parameter '%s' not a number\n"
+            interpreter.termwidth = ntp.util.safeargcast(val, int,
+                                                         errmsg, usage)
         elif switch in ("-u", "--units"):
             interpreter.showunits = True
 


=====================================
ntpclients/ntpsweep
=====================================
--- a/ntpclients/ntpsweep
+++ b/ntpclients/ntpsweep
@@ -156,7 +156,8 @@ if __name__ == '__main__':
         elif switch == "-l" or switch == "--host-list":
             hostlist = val.split(",")
         elif switch == "-m" or switch == "--maxlevel":
-            maxlevel = int(val)
+            errmsg = "Error: -m parameter '%s' not a number\n"
+            maxlevel = ntp.util.safeargcast(val, int, errmsg, __doc__)
         elif switch == "-p" or switch == "--peers":
             recurse = True
         elif switch == "-s" or switch == "--strip":


=====================================
ntpclients/ntptrace
=====================================
--- a/ntpclients/ntptrace
+++ b/ntpclients/ntptrace
@@ -105,7 +105,7 @@ USAGE: ntptrace [-<flag> [<val>] | --<name>[{=| }<val>]]... [host]
         --more-help              Pass the extended usage text through a pager
 
 Options are specified by doubled hyphens and their name or by a single
-hyphen and the flag character."""
+hyphen and the flag character.""" + "\n"
 
 try:
     (options, arguments) = getopt.getopt(
@@ -121,7 +121,8 @@ host = '127.0.0.1'
 
 for (switch, val) in options:
     if switch == "-m" or switch == "--max-hosts":
-        maxhosts = int(val)
+        errmsg = "Error: -m parameter '%s' not a number\n"
+        maxhosts = ntp.util.safeargcast(val, int, errmsg, usage)
     elif switch == "-n" or switch == "--numeric":
         numeric = True
     elif switch == "-r" or switch == "--host":


=====================================
ntpclients/ntpwait
=====================================
--- a/ntpclients/ntpwait
+++ b/ntpclients/ntpwait
@@ -138,9 +138,11 @@ if __name__ == "__main__":
     verbose = 0
     for (switch, val) in options:
         if switch in ("-n", "--tries"):
-            tries = int(val)
+            errmsg = "Error: -n parameter '%s' not a number\n"
+            tries = ntp.util.safeargcast(val, int, errmsg, __doc__)
         elif switch in ("-s", "--sleep"):
-            sleep = int(val)
+            errmsg = "Error: -s parameter '%s' not a number\n"
+            sleep = ntp.util.safeargcast(val, int, errmsg, __doc__)
         elif switch in ("-v", "--verbose"):
             verbose += 1
         elif switch in ("-h", "--help"):


=====================================
pylib/util.py
=====================================
--- a/pylib/util.py
+++ b/pylib/util.py
@@ -57,6 +57,19 @@ MS_VARS = ("rootdelay", "rootdisp", "offset", "sys_jitter", "clk_jitter",
 PPM_VARS = ("frequency", "clk_wander", "clk_wander_threshold")
 
 
+def safeargcast(arg, castfunc, errtext, usage):
+    """Attempts to typecast an argument, prints and dies on failure.
+    errtext must contain a %s for splicing in the argument, and be
+    newline terminated."""
+    try:
+        casted = castfunc(arg)
+    except ValueError:
+        sys.stderr.write(errtext % arg)
+        sys.stderr.write(usage)
+        raise SystemExit(1)
+    return casted
+
+
 def stdversion():
     return "ntpsec-%s+%s %s" % (ntp.version.VERSION,
                                 ntp.version.VCS_TICK,



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/fdd07a68baf2ea75f86316f52908bee3e829368e...f2e056955773f579fc68b14ec7d04b968159ba31

---
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/fdd07a68baf2ea75f86316f52908bee3e829368e...f2e056955773f579fc68b14ec7d04b968159ba31
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/20170511/719054a7/attachment.html>


More information about the vc mailing list