[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Nasty consequence of fclose followed by ferror
From: |
Andreas Jaeger |
Subject: |
Re: Nasty consequence of fclose followed by ferror |
Date: |
23 Nov 2000 09:07:40 +0100 |
User-agent: |
Gnus/5.0808 (Gnus v5.8.8) XEmacs/21.1 (Capitol Reef) |
>>>>> Garance A Drosihn writes:
> Hi.
> I was porting code which is used on several OS's (freebsd, sunOS,
> solaris, aix, irix) to redhat linux 7.0, and ran into a number
> of odd bugs which were time-consuming to track down.
> I eventually found that the basic cause for all those bugs was
> in the code I was porting. It has:
> fclose(fp);
> if (ferror(fp)) { ... do stuff ... }
> Please note I am quite happy to admit this code is wrong. Also
> note I did not write this code, I am just porting it from other
> places. I do not need any lecture on why this code is bad. I
> can read the "Single Unix Specification" as well as anyone else.
> I would be perfectly happy if that call to ferror would cause a
> SEGV or some other disaster -- as long as the disaster is at
> THAT call and not far-far-away from it.
> The trick is that the call to ferror with the freed FILE pointer
> causes random memory to be overwritten. When tracking down
> this problem, I first had calls to inet_aton() which would
> fail. I also had some calls to syslog which were generating
> garbage messages. When I commented out the call to inet_aton(),
> I had failures in some other system routine (which I forget
> now). Debugging was complicated by the fact that sometimes
> everything would work fine, and other times the process in
> question would just disappear on me (in the middle of one
> of these system routines).
> Eventually I commented out enough things that the failing
> system call was at another fopen, which at least got me on
> the right track. Eventually I used that to trace the
> problem back to the bad code above. As I sit here writing
> this message, I have already fixed this code I am porting,
> and it is working fine (and on all the above platforms).
> However, for the sanity of other people who might run into
> this problem, I was wondering if there was some simple and
> inexpensive change which could be made to fclose or ferror
> which would make debugging this much less painful.
No such change is possible - it would lead to memory leaks.
Andreas
--
Andreas Jaeger
SuSE Labs address@hidden
private address@hidden
http://www.suse.de/~aj