[Issue 15293] [REG2.069.0] std.stdio.readln(buffer) messes up buffer's capacity

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Thu Nov 12 07:56:59 PST 2015


https://issues.dlang.org/show_bug.cgi?id=15293

--- Comment #8 from ag0aep6g at gmail.com ---
(In reply to Steven Schveighoffer from comment #7)
> Since passing a buffer into readln disowns the data (readln now owns and can
> resize the used portion down if necessary), the error is continuing to use
> 'buffer' as the buffer -- buffer contains 2 bytes, the second byte is
> essentially garbage after assumeSafeAppend is called since it's not part of
> the capacity. readln is meant to be passed in the same buffer over and over,
> this is why it uses assumeSafeAppend.

Sorry, but that's ridiculous. It's unnecessarily unsafe, overly complicated,
and not documented. The other readlnImpl variants don't work like that, and it
apparently wasn't thought of when byLine was implemented.

If readln wants to take the same buffer over and over, then it should not
shrink it, but return a slice of it. But breaking the API is probably not an
option now. So we're stuck with either 1) fundamentally flawed, surprising
behavior, or 2) more allocations when readln is used naively.

In my opinion, 2 is the way to go.

Also, byLine is nicer anyway, and doesn't have the problem. And if std.io ever
gets done, we have an opportunity not to fall in the same trap.

--


More information about the Digitalmars-d-bugs mailing list