Bug 6944 – stdio.File.byLine can't handle an empty file

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2011-11-13T19:56:00Z
Last change time
2015-06-09T01:31:17Z
Assigned to
nobody
Creator
issues.dlang

Comments

Comment #0 by issues.dlang — 2011-11-13T19:56:59Z
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 by issues.dlang — 2011-11-13T20:27:38Z
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 by issues.dlang — 2011-11-22T21:49:07Z
*** Issue 6993 has been marked as a duplicate of this issue. ***
Comment #3 by braddr — 2012-01-07T14:30:19Z