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 #2 by eco — 2013-02-27T16:14:00Z
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
Commit pushed to master at https://github.com/D-Programming-Language/phobos https://github.com/D-Programming-Language/phobos/commit/bc4031066ab7d4c2221d1241800d95c85910007b Merge pull request #1183 from eco/patch-1 Issue 9612: Cycle opSlice should throw when finish > start
Comment #6 by github-bugzilla — 2013-03-11T03:17:27Z
Commit pushed to master at https://github.com/D-Programming-Language/phobos https://github.com/D-Programming-Language/phobos/commit/dd1a80c56fb848003fb8c3028bc4555ce8388e44 Fixup for cycle RangeError invocation From: #1183 : Issue 9612: Cycle opSlice should throw when finish > start Because a RangeError is not actually customizable, and the first argument is actually the file name. The error was producing: ``` core.exception.RangeError@2 > 1(3836): Range violation ``` Now it produces: ``` core.exception.RangeError@std\range.d(3835): Range violation ```