Index: head/contrib/netbsd-tests/usr.bin/grep/t_grep.sh =================================================================== --- head/contrib/netbsd-tests/usr.bin/grep/t_grep.sh +++ head/contrib/netbsd-tests/usr.bin/grep/t_grep.sh @@ -517,6 +517,28 @@ atf_check -o empty grep -q -A 1 -e "B" test1 atf_check -o empty grep -q -C 1 -e "B" test1 } + +atf_test_case badcontext +badcontext_head() +{ + atf_set "descr" "Check for handling of invalid context arguments" +} +badcontext_body() +{ + printf "A\nB\nC\n" > test1 + + atf_check -s not-exit:0 -e ignore grep -A "-1" "B" test1 + + atf_check -s not-exit:0 -e ignore grep -B "-1" "B" test1 + + atf_check -s not-exit:0 -e ignore grep -C "-1" "B" test1 + + atf_check -s not-exit:0 -e ignore grep -A "B" "B" test1 + + atf_check -s not-exit:0 -e ignore grep -B "B" "B" test1 + + atf_check -s not-exit:0 -e ignore grep -C "B" "B" test1 +} # End FreeBSD atf_init_test_cases() @@ -551,5 +573,6 @@ atf_add_test_case egrep_sanity atf_add_test_case grep_sanity atf_add_test_case grep_nomatch_flags + atf_add_test_case badcontext # End FreeBSD } Index: head/usr.bin/grep/grep.h =================================================================== --- head/usr.bin/grep/grep.h +++ head/usr.bin/grep/grep.h @@ -115,7 +115,7 @@ bflag, cflag, hflag, iflag, lflag, mflag, nflag, oflag, qflag, sflag, vflag, wflag, xflag; extern bool dexclude, dinclude, fexclude, finclude, lbflag, nullflag; -extern unsigned long long Aflag, Bflag; +extern long long Aflag, Bflag; extern long long mcount; extern long long mlimit; extern char fileeol; Index: head/usr.bin/grep/grep.c =================================================================== --- head/usr.bin/grep/grep.c +++ head/usr.bin/grep/grep.c @@ -108,8 +108,8 @@ char re_error[RE_ERROR_BUF + 1]; /* Command-line flags */ -unsigned long long Aflag; /* -A x: print x lines trailing each match */ -unsigned long long Bflag; /* -B x: print x lines leading each match */ +long long Aflag; /* -A x: print x lines trailing each match */ +long long Bflag; /* -B x: print x lines leading each match */ bool Hflag; /* -H: always print file name */ bool Lflag; /* -L: only show names of files with no matches */ bool bflag; /* -b: show block numbers for each match */ @@ -351,7 +351,7 @@ char **aargv, **eargv, *eopts; char *ep; const char *pn; - unsigned long long l; + long long l; unsigned int aargc, eargc, i; int c, lastc, needpattern, newarg, prevoptind; @@ -438,10 +438,11 @@ case '5': case '6': case '7': case '8': case '9': if (newarg || !isdigit(lastc)) Aflag = 0; - else if (Aflag > LLONG_MAX / 10) { + else if (Aflag > LLONG_MAX / 10 - 1) { errno = ERANGE; err(2, NULL); } + Aflag = Bflag = (Aflag * 10) + (c - '0'); break; case 'C': @@ -454,14 +455,17 @@ /* FALLTHROUGH */ case 'B': errno = 0; - l = strtoull(optarg, &ep, 10); - if (((errno == ERANGE) && (l == ULLONG_MAX)) || - ((errno == EINVAL) && (l == 0))) + l = strtoll(optarg, &ep, 10); + if (errno == ERANGE || errno == EINVAL) err(2, NULL); else if (ep[0] != '\0') { errno = EINVAL; err(2, NULL); + } else if (l < 0) { + errno = EINVAL; + err(2, "context argument must be non-negative"); } + if (c == 'A') Aflag = l; else if (c == 'B') Index: head/usr.bin/grep/queue.c =================================================================== --- head/usr.bin/grep/queue.c +++ head/usr.bin/grep/queue.c @@ -49,7 +49,7 @@ }; static STAILQ_HEAD(, qentry) queue = STAILQ_HEAD_INITIALIZER(queue); -static unsigned long long count; +static long long count; static struct qentry *dequeue(void); Index: head/usr.bin/grep/util.c =================================================================== --- head/usr.bin/grep/util.c +++ head/usr.bin/grep/util.c @@ -196,11 +196,12 @@ procfile(const char *fn) { struct parsec pc; + long long tail; struct file *f; struct stat sb; struct str *ln; mode_t s; - int c, last_outed, t, tail; + int c, last_outed, t; bool doctx, printmatch, same_file; if (strcmp(fn, "-") == 0) {