Bug 7659 – std.stdio.File.close() erases file.name

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-03-07T09:09:00Z
Last change time
2013-01-07T14:04:51Z
Assigned to
nobody
Creator
bioinfornatics
Depends on
7022

Comments

Comment #0 by bioinfornatics — 2012-03-07T09:09:05Z
Dear, when you want to reopen a closed file from a File instance the programm segfault, i.e above code _________________________________ import std.stdio; import std.string; void main(){ File f = File("text.txt", "w"); f.writeln( "blah" ); f.close(); if( ! f.isOpen ) f = File( f.name, "a+" ); f.writeln( "blah" ); f.close(); } _________________________________ this tniny code segfault because when you close a file you can't after get the filer name closed by using f.name it will really helpfull to know at any moment the name from a File instance. And method isOpen will get more sense to use. Because currently it said if isopen or not but can't do many thing in more.
Comment #1 by lovelydear — 2012-04-21T01:40:31Z
The issue here is f.close() erases f.name, making it impossible to reopen f by name; unless it's stored in a separate variable.
Comment #2 by lovelydear — 2012-04-21T03:01:55Z
Renamed the title.
Comment #3 by 10equals2 — 2012-06-21T18:39:15Z
I encountered this bug, or a similar variant. I can't decide if I just won the lottery, or if it's reproducable... I couldn't reproduce it, but I might be missing something important. All of my attempts simply segfaulted. This code immediately exits the function with no errors. It returns the correct value even though I assume the return statement is never reached since nothing around the return statement will execute. try { auto f = File(fileName, "w"); f.write("success"); f.detach; if(true) { f = File(fileName, "w"); if(f.readln() == "success") success = true; } f.close; ///All of these cause it to immediately exit the current func. f.name; writeln(f.name); f.name.remove; } The catch statement also catches no Exceptions.
Comment #4 by 10equals2 — 2012-06-21T18:41:39Z
I encountered this bug, or a similar variant. I can't decide if I just won the lottery, or if it's reproducable... I couldn't reproduce it, but I might be missing something important. All of my attempts simply segfaulted. This code immediately exits the function with no errors. It returns the correct value even though I assume the return statement is never reached since nothing around the return statement will execute. try { auto f = File(fileName, "w"); f.write("success"); f.detach; if(true) { f = File(fileName, "r"); if(startsWith(f.readln(), "success")) success = true; } f.close; ///All of these cause it to immediately exit the current func. f.name; writeln(f.name); f.name.remove; } The catch statement also catches no Exceptions.
Comment #5 by 10equals2 — 2012-06-21T18:43:01Z
I encountered this bug, or a similar variant. I can't decide if I just won the lottery, or if it's reproducable... I couldn't reproduce it, but I might be missing something important. All of my attempts simply segfaulted. This code immediately exits the function with no errors. It returns the correct value even though I assume the return statement is never reached since nothing around the return statement will execute. try { auto f = File(fileName, "w"); f.write("success"); f.detach; if(true) { f = File(fileName, "r"); if(startsWith(f.readln(), "success")) success = true; } f.close; ///All of these cause it to immediately exit the current func. f.name; writeln(f.name); f.name.remove; } The catch statement also catches no Exceptions.
Comment #6 by github-bugzilla — 2012-10-04T09:51:22Z
Commit pushed to master at https://github.com/D-Programming-Language/phobos https://github.com/D-Programming-Language/phobos/commit/84a6468ef86ebdb94d1430d95ea8d9964f8e85bf Fix Issue 7659 - std.stdio.File.close() erases file.name * also improve documentation a bit