D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 6888 - std.getopt.getopt: one-letter hash option causes range violation
Summary: std.getopt.getopt: one-letter hash option causes range violation
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: x86_64 Linux
: P2 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-11-03 08:48 UTC by Maksim Zholudev
Modified: 2011-11-14 04:52 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Maksim Zholudev 2011-11-03 08:48:03 UTC
The following code causes the error:
--------------------
import std.getopt;

void main()
{
    int[string] foo;
    auto args = ["", "-t", "a=1"];
    getopt(args, "t", &foo);
}
--------------------
core.exception.RangeError@std.getopt(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 Maksim Zholudev 2011-11-03 09:58:06 UTC
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.