[Bug libstdc++/71431] New: ifstream::rdbuf directory
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71431 Bug ID: 71431 Summary: ifstream::rdbuf directory Product: gcc Version: 6.1.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: libstdc++ Assignee: unassigned at gcc dot gnu.org Reporter: dimaqq at gmail dot com Target Milestone: --- Pardon my primitive code: ``` auto utf8 = std::locale(std::locale(), new std::codecvt_utf8); std::wifstream expression(argv[1]); if (!expression) { /* ... */ return 99; } expression.imbue(utf8); std::wstringstream buf; buf << expression.rdbuf(); ``` This works fine for files; fails correctly for files that won't open (doesn't exist, wrong permissions, etc). It doesn't fail for directories though (result is same as empty file). None of the error bits are set either. strace shows a failed read call: ``` open(".", O_RDONLY) = 4 read(4, 0x10b0250, 8191)= -1 EISDIR (Is a directory) ``` But no error is reported. That can't be right, can it? gcc version 6.1.1 20160501 (GCC); x86_64-pc-linux-gnu
[Bug libstdc++/71431] ifstream::rdbuf directory
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71431 Dima Tisnek changed: What|Removed |Added Status|RESOLVED|NEW Resolution|INVALID |--- --- Comment #3 from Dima Tisnek --- @Jonathan, arguably, `std::ifstream f("/");` should not through, as underlying `open()` succeeds. No read call is made at that point. That was the nature of bug 36564 They site ""current standard" at the time. (Personally I'd prefer ifstream creation to fail, but I can live with that). Successive read operations really ought to set some error bit.
[Bug libstdc++/71431] ifstream::rdbuf directory
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71431 --- Comment #6 from Dima Tisnek --- Perhaps in my case, it's a combination of directory, `imbue` and `rdbuf` ?
[Bug libstdc++/71431] ifstream::rdbuf directory
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71431 --- Comment #7 from Dima Tisnek --- Ugh, it never occurred to me to check error on target of buffer copy. `buf.rdstate()` does report an error for me. A bit unexpected and seems like black magic, but the error is there :)
[Bug libstdc++/71431] ifstream::rdbuf directory
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71431 --- Comment #8 from Dima Tisnek --- Looks like I cannot distinguish between a directory and an empty file: ``` buf << expression.rdbuf(); /* check buf.rdstate() */ ``` underlying file name was a directory -- rdstate() == 4 underlying file name is empty file -- rdstate() == 4 It's good enough for me (file shouldn't be empty), but may be a gotcha for someone else. Personally I find it weird that nothing is set on `expression`, but hey, I didn't read the standard, did I?