[prev in list] [next in list] [prev in thread] [next in thread] 

List:       freebsd-hackers
Subject:    Re: Anyone like obscure stdio problems?
From:       Chris Costello <chris () FreeBSD ! ORG>
Date:       2002-12-29 10:32:54
[Download RAW message or body]

On Sunday, December 29, 2002, Jordan Hubbard wrote:
> I have no problem admitting that I've traced through the innards of 
> _fseeko a few times now and am no closer to finding out exactly where 
> the problem is, though I have a suspicion it has to do with when the 
> file pointer's buffer is allocated and initially populated.  In any 
> case, here's a particularly weird one from the Apple Files that also 
> occurs on FreeBSD
> 
> The following program, also available from 
> http://narcissus.queasyweasel.com/fump.c, demonstrates the problem.
> 
> If you change the top #define of FIRST_SEEK to 0 instead of 1, the 
> program will work.  If you leave it at 1, the initial (and 
> theoretically redundant and no-op) fseek on readMidFP will bugger up 
> stdio's internal state somehow.
> 
> If anyone with more stdio-fu than me would like to poke at it, I'd be 
> interested in hearing what you find out.  If this is breaking some 
> undocumented rule of stdio I'd like to know that too so that I can 
> document it both in FreeBSD and Mac OS X.  Thanks.

   As we discussed on IRC, the problem lies in line 254 of
libc/stdio/fseek.c.  Since in your case the buffer is not
modified (no __SMOD flag, see a few lines back in fseek.c), it
assumes that the buffer that it filled after the first fseek but
before the write is still OK, and simply copies out from it when
you read it looking for your B's.

   Placing a pointless fgetln() after the first fseek() will make
this obvious:

        if (fseek(rp, (long)(i / 2), SEEK_SET) != 0)
                err(1, "fseek on rp");
        (void)fgetln(rp, throwaway);

   fgetln() will set __SMOD for rp's buffer and so stdio is
forced to discard it and refill it from the file (which contains
the B's).

-- 
Chris Costello                                <chris@FreeBSD.org>
FreeBSD Project                           http://www.FreeBSD.org/
TrustedBSD Project                     http://www.TrustedBSD.org/

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic