[Issue 13856] New: std.stdio.readln stomps arrays
via Digitalmars-d-bugs
digitalmars-d-bugs at puremagic.com
Thu Dec 11 14:47:47 PST 2014
https://issues.dlang.org/show_bug.cgi?id=13856
Issue ID: 13856
Summary: std.stdio.readln stomps arrays
Product: D
Version: D2
Hardware: All
OS: All
Status: NEW
Severity: normal
Priority: P1
Component: Phobos
Assignee: nobody at puremagic.com
Reporter: r.sagitario at gmx.de
This program causes two asserts on Win32:
import std.stdio;
import std.file;
import core.memory;
void main()
{
std.file.write("testread", "abcd\n0123456789abcde\n");
File f = File("testread", "rb");
char[] ln = new char[2];
f.readln(ln);
assert(ln == "abcd\n");
char[] t = ln[0..2];
t ~= 't'; // stomps ln
assert(t == "abt");
assert(ln == "abcd\n"); // fails
// it can get also stomp the array length
ln = new char[4];
f.readln(ln);
assert(ln == "0123456789abcde\n");
auto bi = GC.query(ln.ptr);
assert(bi.base == ln.ptr);
assert(bi.attr & GC.BlkAttr.APPENDABLE);
int len = ln.ptr[bi.size - 1]; // last byte should be allocated array
size
assert(ln.length <= len && len < bi.size); // fails: len = 10
}
shows that readln fills the memory of arrays with data without respecting the
"allocation length" also stored for arrays.
There are different implementations for DigitalMars, Microsoft and GCC runtime,
so the behaviour might be slightly different, but all assume that the passed
array is GC.malloced, starts at the allocation block and has size
GC.sizeOf(arr.ptr).
--
More information about the Digitalmars-d-bugs
mailing list