www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 13856] New: std.stdio.readln stomps arrays


          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 puremagic.com
          Reporter: r.sagitario 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];

    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];
    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
    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

Dec 11 2014