From 296184f5419e1a7f8748688606950e747338f8f1 Mon Sep 17 00:00:00 2001 From: Jon Degenhardt Date: Sat, 15 Jul 2017 13:08:53 -0700 Subject: [PATCH] Fix issue 17650: std.getopt range violation when option value is a hyphen. --- std/getopt.d | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/std/getopt.d b/std/getopt.d index f54c6c2dc8c..5beddcc23b4 100644 --- a/std/getopt.d +++ b/std/getopt.d @@ -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; @@ -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 == "-"); +}