emersion / mrsh

A minimal POSIX shell
MIT License
489 stars 35 forks source link

using shift in getopts triggers an assert #177

Open Earnestly opened 3 years ago

Earnestly commented 3 years ago

I have encountered a few issues when using getopts while testing a potential bug in dash and discovered a segmentation fault condition in mrsh doing the following:

#!/bin/sh --

while getopts :a: arg; do
    case $arg in
        a)
            set -- "$@" attr="$OPTARG"
            shift 2
    esac
done

printf '[%s]' "$@"
mrsh -x example.sh -a foo -a bar
+ getopts :a: arg
+ set -- -a foo -a bar attr=foo
+ shift 2
+ getopts :a: arg
mrsh: ../getopt.c:13: _mrsh_getopt: Assertion `argv[argc] == NULL' failed.