D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 10517 - readln(Char)(Char[] buf) accepts non-mutable buffers
Summary: readln(Char)(Char[] buf) accepts non-mutable buffers
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D1 (retired)
Hardware: All All
: P2 normal
Assignee: monarchdodra
URL:
Keywords: accepts-invalid
: 11374 (view as issue list)
Depends on:
Blocks:
 
Reported: 2013-06-30 23:35 UTC by monarchdodra
Modified: 2013-10-30 15:44 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description monarchdodra 2013-06-30 23:35:42 UTC
As said in title, and explained in this thread:
http://forum.dlang.org/thread/odkkhkgnahhpklvumghv@forum.dlang.org

--------------------------------
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 2 github-bugzilla 2013-07-02 20:55:54 UTC
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 Martin Krejcirik 2013-10-30 15:44:43 UTC
*** Issue 11374 has been marked as a duplicate of this issue. ***