Bug 6888 – std.getopt.getopt: one-letter hash option causes range violation
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2011-11-03T08:48:00Z
Last change time
2011-11-14T04:52:25Z
Assigned to
nobody
Creator
maximzms
Comments
Comment #0 by maximzms — 2011-11-03T08:48:03Z
The following code causes the error:
--------------------
import std.getopt;
void main()
{
int[string] foo;
auto args = ["", "-t", "a=1"];
getopt(args, "t", &foo);
}
--------------------
[email protected](519): Range violation
--------------------
There is no error if bundling is turned on:
getopt(args, config.bundling, "t", &foo);
There is no error if the option contains more than one letter
e.g. "--tune"
Comment #1 by maximzms — 2011-11-03T09:58:06Z
The bug is found!
In std/getopt.d:
If the option is not long and bundling is forbidden lines 587-588 are executed.
If arg looks like "-t" value is empty but !value is false, i.e. in the following code all asserts are true:
test.d:
--------------------
void main()
{
string s = "t";
string s1 = s[1..$];
string s2 = null;
assert(s1 == null);
assert(s2 == null);
assert(!s1 == false); // Why?
assert(!s2 == true);
}
--------------------
Hence in std/getopt.d in the line 464:
!val is false and val is empty when we come to line 519 and try to access its contents.
I'm going to fix it right now.
But the behavior of empty slices looks like a bug of the compiler.