Bug 14005 – core.exception.InvalidMemoryOperationError@(0) during byLine

Status
RESOLVED
Resolution
DUPLICATE
Severity
blocker
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2015-01-18T23:51:00Z
Last change time
2015-06-09T05:13:47Z
Keywords
pull
Assigned to
nobody
Creator
acehreli

Comments

Comment #0 by acehreli — 2015-01-18T23:51:05Z
1) Start with git head dmd: DMD64 D Compiler v2.067-devel-e9e7c53 2) Create file "deneme.txt" and put the following two lines in it (The problem is data-dependent; I could not reduce it further): D'deki tanıma göre, dönüş değerini üretirken veya olası yan etkilerini oluştururken değerlere erişmeyen bir işlev saftır. da değişebilen evrensel durum olarak kabul edildiklerinden saf işlevler giriş ve çıkış işlemleri de içeremezler. Bazı saf işlevler parametrelerinde değişiklik de yapmazlar. Dolayısıyla, bu gibi işlevlerin programdaki tek etkileri dönüş değerleridir. Bu açıdan bakıldığında, parametrelerinde değişiklik yapmayan saf işlevlerin belirli parametre değerlerine karşılık hep aynı değeri döndürecekleri belli demektir. Dolayısıyla, böyle bir işlevin dönüş değeri eniyileştirme amacıyla sonradan kullanılmak üzere saklanabilir. (Bu gözlem hem derleyici hem de programcı için yararlıdır.) 3) Compile and run the following program: import std.stdio; void main() { foreach (line; File("deneme.txt", "r").byLine) { writeln(line); } } (writeln line is optional.) The compiled program fails with core.exception.InvalidMemoryOperationError@(0) after printing the two lines.
Comment #1 by ag0aep6g — 2015-01-19T04:31:26Z
Reduced: ---- import core.memory: GC; void main() { /* First line. Exact length doesn't matter. */ auto firstLineLength = 260; auto buf = new char[firstLineLength]; /* Second line. Must not be about as long as the block allocated for the first line. */ auto firstBlockSize = GC.sizeOf(buf.ptr); buf = buf.ptr[0 .. firstBlockSize]; auto secondLineLength = firstBlockSize; /* no good */ version(none) auto secondLineLength = firstBlockSize - 1; /* no good */ version(none) auto secondLineLength = firstBlockSize - 2; /* ok */ buf = buf.ptr[0 .. secondLineLength]; /* popFront the second line. */ assumeSafeAppend(buf); /* core.exception.InvalidMemoryOperationError@(0) */ } ---- I guess the size returned by GC.sizeOf includes some internal data. Then std.stdio.readlnImpl is wrong to assume that it can use all of it.
Comment #2 by ag0aep6g — 2015-01-19T15:45:53Z
(In reply to ag0aep6g from comment #1) > I guess the size returned by GC.sizeOf includes some internal data. Then > std.stdio.readlnImpl is wrong to assume that it can use all of it. Actually, std.stdio.readlnImpl shouldn't write beyond the given buf at all. That space may be in use! https://github.com/D-Programming-Language/phobos/pull/2880
Comment #3 by schveiguy — 2015-01-19T18:29:25Z
I think this is a duplicate of issue 13856 Can you check that PR to see if it helps?
Comment #4 by ag0aep6g — 2015-01-19T21:16:37Z
(In reply to Steven Schveighoffer from comment #3) > I think this is a duplicate of issue 13856 > > Can you check that PR to see if it helps? Yup, it's a duplicate. The PR for 13856 fixes Ali's test case as well, as far as I can tell. The changes are more involved than mine, though, fixing/optimizing other things as well. It may be worthwile to split it up, if there's anything blocking the merge of the whole thing. *** This issue has been marked as a duplicate of issue 13856 ***