Skip to content

Commit

Permalink
Fix issue 17650: std.getopt range violation when option value is a hy…
Browse files Browse the repository at this point in the history
…phen.
  • Loading branch information
jondegenhardt committed Jul 15, 2017
1 parent 5948f1c commit 296184f
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion std/getopt.d
Expand Up @@ -1105,7 +1105,7 @@ private bool optMatch(string arg, string optPattern, ref string value,
import std.uni : toUpper;
//writeln("optMatch:\n ", arg, "\n ", optPattern, "\n ", value);
//scope(success) writeln("optMatch result: ", value);
if (!arg.length || arg[0] != optionChar) return false;
if (arg.length < 2 || arg[0] != optionChar) return false;
// yank the leading '-'
arg = arg[1 .. $];
immutable isLong = arg.length > 1 && arg[0] == optionChar;
Expand Down Expand Up @@ -1834,3 +1834,24 @@ void defaultGetoptFormatter(Output)(Output output, string text, Option[] opt)
assert(x == 17);
assert(y == 50);
}

@system unittest // Hyphens at the start of option values; Issue 17650
{
auto args = ["program", "-m", "-5", "-n", "-50", "-c", "-", "-f", "-"];

int m;
int n;
char c;
string f;

getopt(args,
"m|mm", "integer", &m,
"n|nn", "integer", &n,
"c|cc", "character", &c,
"f|file", "filename or hyphen for stdin", &f);

assert(m == -5);
assert(n == -50);
assert(c == '-');
assert(f == "-");
}

0 comments on commit 296184f

Please sign in to comment.