D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 6944 - stdio.File.byLine can't handle an empty file
Summary: stdio.File.byLine can't handle an empty file
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All All
: P2 normal
Assignee: No Owner
URL:
Keywords:
: 6993 (view as issue list)
Depends on:
Blocks:
 
Reported: 2011-11-13 19:56 UTC by Jonathan M Davis
Modified: 2015-06-09 01:31 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Jonathan M Davis 2011-11-13 19:56:59 UTC
This program

import std.file;
import std.stdio;

void main()
{
    std.file.write("empty.txt", "");
    auto file = File("empty.txt");

    foreach(line; file.byLine())
        writeln(line);
}


results in this output:

core.exception.AssertError@std/stdio.d(946): Bug in File.readln
----------------
./q(_d_assert_msg+0x1f) [0x4595cb]
./q(void std.stdio.File.ByLine!(char, char).ByLine.popFront()+0xa6) [0x4560a2]
./q(char[] std.stdio.File.ByLine!(char, char).ByLine.front()+0x4c) [0x455fec]
./q(_Dmain+0x87) [0x455e13]
./q(extern (C) int rt.dmain2.main(int, char**).void runMain()+0x17) [0x459bd7]
./q(extern (C) int rt.dmain2.main(int, char**).void tryExec(scope void delegate())+0x2a) [0x45977e]
./q(extern (C) int rt.dmain2.main(int, char**).void runAll()+0x42) [0x459c2a]
./q(extern (C) int rt.dmain2.main(int, char**).void tryExec(scope void delegate())+0x2a) [0x45977e]
./q(main+0xd3) [0x45970f]
/lib/libc.so.6(__libc_start_main+0xed) [0x7ff97772817d]d


The foreach should just not run, since it's effectively iterating over an empty range, but apparently ByLine's empty doesn't check the possibility that EOF has already been reached - just whether the file is open or not.
Comment 1 Jonathan M Davis 2011-11-13 20:27:38 UTC
The simplest workaround that I've found is to use check whether std.file.getSize returns a value greater than 0, but I don't know if that's technically sufficient, and even if it is, I'm not sure that that's what should be done internall to ByLine. It _does_ allow you to avoid AssertErrors until the problem is fixed though.
Comment 2 Jonathan M Davis 2011-11-22 21:49:07 UTC
*** Issue 6993 has been marked as a duplicate of this issue. ***
Comment 3 Brad Roberts 2012-01-07 14:30:19 UTC
Andrei addressed in:
  https://github.com/D-Programming-Language/phobos/pull/375