On 29/01/2021 20:36, Jilles Tjoelker wrote:
Unfortunately, dash and FreeBSD sh reset the getopts state when the positional parameters are modified via set or shift. They probably do this to avoid use after free and out of bounds memory access when a script violates POSIX's rule.
In dash, the getopts command guards against this. getoptscmd() checks that shellparam.optind is in bounds before calling getopts(), and getopts() checks that shellparam.optoff is in bounds, so as far as correctness goes, it should be enough to just remove the resetting of shellparam.optind and shellparam.optoff in those places that are not supposed to reset the state.
Cheers, Harald van Dijk