Don't allow negative numbers for font width, because it could lead to a
floating point exception (and wouldn't make sense anyway).

# wsfontload -w -7 /dev/null
Floating point exception (core dumped)
# _

While at it, use strtonum for proper error messages; also check font height.

Current error message includes previous value of font width/height
and therefore doesn't help spotting the error at all.

Index: wsfontload.c
===================================================================
RCS file: /cvs/src/usr.sbin/wsfontload/wsfontload.c,v
retrieving revision 1.14
diff -u -p -u -p -r1.14 wsfontload.c
--- wsfontload.c        9 Feb 2015 23:00:15 -0000       1.14
+++ wsfontload.c        6 Sep 2015 19:21:35 -0000
@@ -33,17 +33,18 @@
  *
  */
 
-#include <sys/types.h>
-#include <sys/time.h>
 #include <sys/ioctl.h>
 #include <sys/stat.h>
+#include <sys/time.h>
+#include <sys/types.h>
 
+#include <err.h>
+#include <fcntl.h>
+#include <limits.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <fcntl.h>
-#include <unistd.h>
 #include <string.h>
-#include <err.h>
+#include <unistd.h>
 
 #include <dev/wscons/wsconsio.h>
 
@@ -90,6 +91,7 @@ main(int argc, char *argv[])
        struct stat stat;
        size_t len;
        void *buf;
+       const char *errstr;
 
        wsdev = DEFDEV;
        memset(&f, 0, sizeof f);
@@ -103,14 +105,14 @@ main(int argc, char *argv[])
                        wsdev = optarg;
                        break;
                case 'w':
-                       if (sscanf(optarg, "%d", &f.fontwidth) != 1)
-                               errx(1, "invalid font width of %d",
-                                   f.fontwidth);
+                       f.fontwidth = strtonum(optarg, 1, UINT_MAX, &errstr);
+                       if (errstr)
+                               errx(1, "font width is %s: %s", errstr, optarg);
                        break;
                case 'h':
-                       if (sscanf(optarg, "%d", &f.fontheight) != 1)
-                               errx(1, "invalid font height of %d",
-                                   f.fontheight);
+                       f.fontheight = strtonum(optarg, 1, UINT_MAX, &errstr);
+                       if (errstr)
+                               errx(1, "font height is %s: %s", errstr, 
optarg);
                        break;
                case 'e':
                        f.encoding = getencoding(optarg);

Reply via email to