Issue 17650: std.getopt range violation when option value is a hyphen #5612
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This fixes an issue in std.getopt private function optMatch. This function is used to see if a command line argument represents an option (e.g. "--file", "-a", etc.). It looks at all the arguments on the command line, including option values. (In
$ command -f 1 -g 2, "1" and "2" are option values.)The range violation occurs if an option value is a single hyphen. The
optMatchfunction sees the hyphen and assumes it might be an option argument (like "-f"). It then tests the next character in the string. When the value is a single hyphen the next character does not exist.A single hyphen (or a single
optionChar) only introduces an option, it is never an option by itself. So the easy fix is to also check that the length is at least 2.Note: Looking through this code it appears there may be other problems like this. I've elected to fix only this case in this PR, as it is the most pressing issue.