Bug 10517 – readln(Char)(Char[] buf) accepts non-mutable buffers

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D1 (retired)
Platform
All
OS
All
Creation time
2013-06-30T23:35:00Z
Last change time
2013-10-30T15:44:43Z
Keywords
accepts-invalid
Assigned to
monarchdodra
Creator
monarchdodra

Comments

Comment #0 by monarchdodra — 2013-06-30T23:35:42Z
As said in title, and explained in this thread: http://forum.dlang.org/thread/[email protected] -------------------------------- The global readln has a signature that looks like this: size_t readln(ref char[] buf, dchar terminator = '\n') File.readln's is: size_t readln(C)(ref C[] buf, dchar terminator = '\n') if (isSomeChar!C && !is(C == enum)) You might think "Oh: Global readline isn't templated, it should". Yes it should, but that's minor and trivial to fix. The problem is that "C[]" can mean things like "const(char)[]", which means, basically, "string". This means that the following code is legal: string reuseableBuffer; (!) myFile.readln(reuseableBuffer); //Okey Dokey. Note: This works perfectly fine, there is no illegal mutation or anything. It's just that a brand new value is always assigned to the (not so reuseable) reuseableBuffer slice. The code handles this, but: a) It Accepts code this makes little sense, and is most probably an error that silently passes. b) While the *code* accepts this, *reading it*, it feels more like luck then explicitly handled. c) This can be replaced just as well by: c.1) "s = myFile.readln();" c.2) "(s = myFile.readln()).size;" if you want the return value -------------------------------- Solution(s): 1) File.readln signature must be fixed to accept only mutable buffers. 2) .readln should be templatize to accept [wd]string.
Comment #1 by monarchdodra — 2013-07-01T10:23:22Z
Comment #2 by github-bugzilla — 2013-07-02T20:55:54Z
Commits pushed to master at https://github.com/D-Programming-Language/phobos https://github.com/D-Programming-Language/phobos/commit/97cec336a14c5d433970b930906bfeb68fdccaf3 Fix Issue 10517 - readln(Char)(Char[] buf) accepts non-mutable buffers Also templatizes a global readln https://github.com/D-Programming-Language/phobos/commit/3be7a03168c8763a4fef9d62210899d8a9be518c Merge pull request #1381 from monarchdodra/readln Fix Issue 10517 - readln(Char)(Char[] buf) accepts non-mutable buffers
Comment #3 by mk — 2013-10-30T15:44:43Z
*** Issue 11374 has been marked as a duplicate of this issue. ***