if i pass "1filename" as an argument to -A, -B, or -C, it's not out
of range, it's not a number.

this converts the arg processing to use strtonum so the errors are
better.

however, maybe the errors are standardised and this breaks compat
with posix or something?

Index: grep.c
===================================================================
RCS file: /cvs/src/usr.bin/grep/grep.c,v
retrieving revision 1.51
diff -u -p -r1.51 grep.c
--- grep.c      30 Apr 2015 13:49:04 -0000      1.51
+++ grep.c      26 Aug 2015 00:29:30 -0000
@@ -235,7 +235,8 @@ main(int argc, char *argv[])
        int c, lastc, prevoptind, newarg, i, needpattern, exprs, expr_sz;
        struct patfile *patfile, *pf_next;
        long l;
-       char *ep, **expr;
+       char **expr;
+       const char *errstr;
 
        SLIST_INIT(&patfilelh);
        switch (__progname[0]) {
@@ -279,10 +280,9 @@ main(int argc, char *argv[])
                        break;
                case 'A':
                case 'B':
-                       l = strtol(optarg, &ep, 10);
-                       if (ep == optarg || *ep != '\0' ||
-                           l <= 0 || l >= INT_MAX)
-                               errx(2, "context out of range");
+                       l = strtonum(optarg, 1, INT_MAX, &errstr);
+                       if (errstr != NULL)
+                               errx(2, "context %s", errstr);
                        if (c == 'A')
                                Aflag = (int)l;
                        else
@@ -292,10 +292,9 @@ main(int argc, char *argv[])
                        if (optarg == NULL)
                                Aflag = Bflag = 2;
                        else {
-                               l = strtol(optarg, &ep, 10);
-                               if (ep == optarg || *ep != '\0' ||
-                                   l <= 0 || l >= INT_MAX)
-                                       errx(2, "context out of range");
+                               l = strtonum(optarg, 1, INT_MAX, &errstr);
+                               if (errstr != NULL)
+                                       errx(2, "context %s", errstr);
                                Aflag = Bflag = (int)l;
                        }
                        break;

Reply via email to