Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 3 Oct 2021 05:20:54 GMT
From:      Kyle Evans <kevans@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org
Subject:   git: 00d0f1811b71 - stable/12 - man: reset OPTIND before parsing args
Message-ID:  <202110030520.1935KsDP020872@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch stable/12 has been updated by kevans:

URL: https://cgit.FreeBSD.org/src/commit/?id=00d0f1811b719c66ee91b9bf4680cfc0515e65aa

commit 00d0f1811b719c66ee91b9bf4680cfc0515e65aa
Author:     Kyle Evans <kevans@FreeBSD.org>
AuthorDate: 2021-09-22 19:58:19 +0000
Commit:     Kyle Evans <kevans@FreeBSD.org>
CommitDate: 2021-10-03 05:19:56 +0000

    man: reset OPTIND before parsing args
    
    From jilles: POSIX requires that a script set `OPTIND=1` before using
    different sets of parameters with `getopts`, or the results will be
    unspecified.
    
    The specific problem observed here is that we would execute `man -f` or
    `man -k` without cleaning up state from man_parse_args()' `getopts`
    loop.  FreeBSD's /bin/sh seems to reset OPTIND to 1 after we hit the
    second getopts loop, rendering the following shift harmless; other
    /bin/sh implementations will leave it at what we came into the loop at
    (e.g., bash as /bin/sh), shifting off any keywords that we had.
    
    Input from:     jilles
    Reviewed by:    allanjude, bapt, imp
    Sponsored by:   Klara, Inc.
    
    (cherry picked from commit f555b39e6bb7cbfbe1905e90f64c4dfc4456fabb)
---
 usr.bin/man/man.sh | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/usr.bin/man/man.sh b/usr.bin/man/man.sh
index 12a370cd8efb..68b9bb3e695a 100755
--- a/usr.bin/man/man.sh
+++ b/usr.bin/man/man.sh
@@ -243,6 +243,7 @@ is_newer() {
 manpath_parse_args() {
 	local cmd_arg
 
+	OPTIND=1
 	while getopts 'Ldq' cmd_arg; do
 		case "${cmd_arg}" in
 		L)	Lflag=Lflag ;;
@@ -426,6 +427,7 @@ man_display_page_groff() {
 
 	if [ -n "$MANROFFSEQ" ]; then
 		set -- -$MANROFFSEQ
+		OPTIND=1
 		while getopts 'egprtv' preproc_arg; do
 			case "${preproc_arg}" in
 			e)	pipeline="$pipeline | $EQN" ;;
@@ -545,6 +547,7 @@ man_find_and_display() {
 man_parse_args() {
 	local IFS cmd_arg
 
+	OPTIND=1
 	while getopts 'M:P:S:adfhkm:op:tw' cmd_arg; do
 		case "${cmd_arg}" in
 		M)	MANPATH=$OPTARG ;;
@@ -933,6 +936,7 @@ trim() {
 # Parse commandline args for whatis and apropos.
 whatis_parse_args() {
 	local cmd_arg
+	OPTIND=1
 	while getopts 'd' cmd_arg; do
 		case "${cmd_arg}" in
 		d)	debug=$(( $debug + 1 )) ;;



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202110030520.1935KsDP020872>