Bug 19042 – Chunking a padRight'ed range leads to range violations

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2018-06-29T18:24:39Z
Last change time
2019-02-18T14:09:31Z
Assigned to
No Owner
Creator
asumface

Comments

Comment #0 by asumface — 2018-06-29T18:24:39Z
The following code exhibits a range violation: import std.stdio; import std.range; import std.conv; void main() { pragma(msg, [2,5,13].padRight(42, 10).chunks(5).to!string); } Compiler output: /dlang/dmd/linux/bin64/../../src/phobos/std/range/package.d(12811): Error: slice [5..3] exceeds array bounds [0..3] /dlang/dmd/linux/bin64/../../src/phobos/std/range/package.d(12811): called from here: Result(null, 0, 0LU, 0LU, 0LU).this(b <= this.data.length ? this.data[a..b] : this.data[a..this.data.length], this.element, b - a) /dlang/dmd/linux/bin64/../../src/phobos/std/range/primitives.d(1743): called from here: r.opSlice(n, __dollar) /dlang/dmd/linux/bin64/../../src/phobos/std/range/package.d(7704): called from here: popFrontN(this._source, this._chunkSize) /dlang/dmd/linux/bin64/../../src/phobos/std/format.d(3299): called from here: val.popFront() /dlang/dmd/linux/bin64/../../src/phobos/std/format.d(4127): called from here: formatRange(w, val, f) /dlang/dmd/linux/bin64/../../src/phobos/std/format.d(1845): called from here: formatValueImpl(w, val, f) /dlang/dmd/linux/bin64/../../src/phobos/std/conv.d(151): called from here: formatValue(w, src, f) /dlang/dmd/linux/bin64/../../src/phobos/std/conv.d(1018): called from here: toStr(value) /dlang/dmd/linux/bin64/../../src/phobos/std/conv.d(222): called from here: toImpl(_param_0) onlineapp.d(7): called from here: to(chunks(padRight([2, 5, 13], 42, 10LU), 5LU)) onlineapp.d(7): while evaluating pragma(msg, chunks(padRight([2, 5, 13], 42, 10LU), 5LU).to!string) The problem occurs almost indepentently from the supplied arguments and is not restricted to compile-time evaluation, the only requirement that I could determine is that padRight needs to extend its input by at least one element. Replacing chunks with evenChunks results in the same error. padLeft does not appear to be affected by this issue. It can be reproduced in all versions since the introduction of padRight (2.072).
Comment #1 by jrdemail2000-dlang — 2019-02-17T06:53:41Z
This bug is triggered when a slice is taken entirely from the pad section at the end of the range. Here's another variation: ---- issue_19042.d ---- import std.range; import std.stdio; void main(string[] args) { auto r = [1, 2, 3, 4].padRight(0, 10); writeln("r[7 .. 9] : ", r[7 .. 9]); } ---- Run ------- $ dmd issue_19042.d $ ./issue_19042 core.exception.RangeError@INSTALLDIR/dmd2-2.084.0/osx/bin/../../src/phobos/std/range/package.d(12868): Range violation ---------------- ??:? _d_arrayboundsp [0x73c2b71] ??:? pure nothrow @nogc @safe std.range.padRight!(int[], int).padRight(int[], int, ulong).Result std.range.padRight!(int[], int).padRight(int[], int, ulong).Result.opSlice(ulong, ulong) [0x73aab65] ??:? _Dmain [0x73aa596]
Comment #2 by jrdemail2000-dlang — 2019-02-17T09:24:12Z
Comment #3 by dlang-bot — 2019-02-18T14:09:31Z
dlang/phobos pull request #6865 "Fix issues 19042, 19681 - std.range.padRight bugs" was merged: https://github.com/dlang/phobos/pull/6865