D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 14292 - writing to file opened for reading - error report is nonsense
Summary: writing to file opened for reading - error report is nonsense
Status: RESOLVED WORKSFORME
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All All
: P1 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-03-16 15:19 UTC by Ivan Kazmenko
Modified: 2020-03-21 03:56 UTC (History)
0 users

See Also:


Attachments
the example code (94 bytes, text/plain)
2015-03-16 15:19 UTC, Ivan Kazmenko
Details

Note You need to log in before you can comment on or make changes to this issue.
Description Ivan Kazmenko 2015-03-16 15:19:36 UTC
Created attachment 1492 [details]
the example code

Say we want to overwrite a file "test.txt".
Say we instead have a typo and so open it for reading.
After trying to write to the file, a hilarious message appears:

-----
std.exception.ErrnoException@<somepath>\std\stdio.d(2113):  (No error)
-----

The culprit (line 2337 of stdio.d at master currently) is:

-----
if (result != writeme.length) errnoEnforce(0);
-----

That errnoEnforce(0) produces a nonsense message "...ErrnoException...: (No error)".
Which I believe is wrong code regardless of the context.
But I don't know which way of reporting the error is right here.

Below is the complete example code.
The file "test.txt" must be present for it to work.
Its contents may be arbitrary.

-----
import std.stdio;
void main ()
{
	auto f = File ("test.txt", "rt"); // file must exist
	f.writeln ("!");
}
-----
Comment 1 basile-z 2017-12-26 09:59:32 UTC
It gives: 
std.exception.ErrnoException@/usr/include/dmd/phobos/std/stdio.d(2847):  (Bad file descriptor)

nowadays