Bug 9612 – std.range.Cycle.opSlice tests on the bounds are missing
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-02-27T15:20:00Z
Last change time
2013-03-11T03:17:27Z
Keywords
accepts-invalid
Assigned to
nobody
Creator
trikkuz
Comments
Comment #0 by trikkuz — 2013-02-27T15:20:41Z
This line:
writeln(iota(10).cycle()[5..2].take(4));
prints:
[5, 6, 7, 8]
It seems some preconditions/tests are missing from cycle()
Comment #1 by bearophile_hugs — 2013-02-27T15:26:58Z
I was about to file this bug myself, this is the text I was going to use:
This code:
void main() {
auto a = [0,1,2,3,4,5,6,7,8,9][5 .. 2];
}
Gives the correct compile-time error:
temp3.d(2): Error: array slice [5 .. 2] is out of bounds
temp3.d(2): Error: array slice [5 .. 2] is out of bounds
While this gives no compile-time nor run-time errors:
import std.stdio: writeln;
import std.range: iota, cycle, take;
void main() {
iota(10).cycle()[5 .. 2].take(4).writeln();
}
And prints (DMD 2.063alpha):
[5, 6, 7, 8]
The opSlice of cycle() lacks pre-conditions or tests, and there are not enough unittests to catch this bug:
auto opSlice(size_t i, size_t j)
{
auto retval = this.save;
retval._index += i;
return takeExactly(retval, j - i);
}
j - i is positive because those numbers are unsigned, and because D lacks run-time errors for integral overflows:
import std.stdio;
struct Foo {
auto opSlice(size_t i, size_t j) {
writeln(j - i);
}
}
void main() {
Foo f;
f[5 .. 2];
}
Output:
4294967293
Maybe there is a need to run something similar to QuickCheck on Phobos:
http://en.wikipedia.org/wiki/QuickCheck
Comment #3 by bearophile_hugs — 2013-02-27T16:29:41Z
(In reply to comment #2)
> https://github.com/D-Programming-Language/phobos/pull/1183
>
> should take care of this.
Maybe that RangeError() should have a message that tells what's the error. Something like:
RangeError(text(i, " > ", j))
Comment #4 by eco — 2013-02-27T16:34:43Z
(In reply to comment #3)
> (In reply to comment #2)
> > https://github.com/D-Programming-Language/phobos/pull/1183
> >
> > should take care of this.
>
> Maybe that RangeError() should have a message that tells what's the error.
> Something like:
>
> RangeError(text(i, " > ", j))
Good idea. Done.
Comment #5 by github-bugzilla — 2013-02-27T20:02:38Z