Date: Sun, 29 Dec 2002 04:32:54 -0600 From: Chris Costello <chris@FreeBSD.ORG> To: Jordan Hubbard <jkh@apple.com> Cc: FreeBSD Hackers <hackers@FreeBSD.ORG> Subject: Re: Anyone like obscure stdio problems? Message-ID: <20021229103254.GB48772@holly.machined.net> In-Reply-To: <7D91340A-1B13-11D7-A2B1-000393BB9222@apple.com> References: <7D91340A-1B13-11D7-A2B1-000393BB9222@apple.com>
next in thread | previous in thread | raw e-mail | index | archive | help
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
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20021229103254.GB48772>