From cb9b5491ad5eb76b92ae999b74e38a3de00bf96c Mon Sep 17 00:00:00 2001 From: millert Date: Sun, 7 Sep 2003 22:05:30 +0000 Subject: Make -number be an error (similar to my change in grep.c); OK tedu@ --- usr.bin/diff/diff.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) (limited to 'usr.bin/diff/diff.c') diff --git a/usr.bin/diff/diff.c b/usr.bin/diff/diff.c index 412a5c9b9bd..aef4b781234 100644 --- a/usr.bin/diff/diff.c +++ b/usr.bin/diff/diff.c @@ -1,4 +1,4 @@ -/* $OpenBSD: diff.c,v 1.41 2003/09/07 18:50:58 jmc Exp $ */ +/* $OpenBSD: diff.c,v 1.42 2003/09/07 22:05:30 millert Exp $ */ /* * Copyright (c) 2003 Todd C. Miller @@ -21,12 +21,13 @@ */ #ifndef lint -static const char rcsid[] = "$OpenBSD: diff.c,v 1.41 2003/09/07 18:50:58 jmc Exp $"; +static const char rcsid[] = "$OpenBSD: diff.c,v 1.42 2003/09/07 22:05:30 millert Exp $"; #endif /* not lint */ #include #include +#include #include #include #include @@ -84,22 +85,25 @@ main(int argc, char **argv) { char *ep, **oargv; long l; - int ch, lastch, gotstdin; + int ch, lastch, gotstdin, prevoptind, newarg; oargv = argv; gotstdin = 0; - lastch = 0; + lastch = '\0'; + prevoptind = 1; + newarg = 1; while ((ch = getopt_long(argc, argv, OPTIONS, longopts, NULL)) != -1) { switch (ch) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': - if (!(lastch == 'c' || lastch == 'u' || - (lastch >= '0' && lastch <= '9'))) - usage(); - if (lastch == 'c' || lastch == 'u') + if (newarg) + usage(); /* disallow -[0-9]+ */ + else if (lastch == 'c' || lastch == 'u') context = 0; - context = context * 10 + ch - '0'; + else if (!isdigit(lastch) || context > INT_MAX / 10) + usage(); + context = (context * 10) + (ch - '0'); break; case 'a': aflag = 1; @@ -196,6 +200,8 @@ main(int argc, char **argv) break; } lastch = ch; + newarg = optind != prevoptind; + prevoptind = optind; } argc -= optind; argv += optind; -- cgit v1.2.3-59-g8ed1b