emersion / emersion/mrsh

using shift in getopts triggers an assert

Open
#177 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C
Stars
531
Forks
40
PR merge metrics
No merged PRs in 30d

Description

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:

```sh
#!/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.
```

Contributor guide

Open the contributing guide

Research direction

Reproduce the assertion with the provided shell script and command, then start in getopt.c at _mrsh_getopt and the argv[argc] assertion. Trace how getopts handles the positional-parameter changes after shift 2. Done means the script completes without the segmentation fault or assertion failure and produces the expected printf output.

Written by the indexing model from the issue text.

Assessment

Tech stack
c
Domain
cli
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
50/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.