From owner-freebsd-hackers Sun Dec 29 2:32:48 2002 Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id D58A337B401 for ; Sun, 29 Dec 2002 02:32:46 -0800 (PST) Received: from holly.dyndns.org (adsl-208-191-149-232.dsl.hstntx.swbell.net [208.191.149.232]) by mx1.FreeBSD.org (Postfix) with ESMTP id E5E2443E4A for ; Sun, 29 Dec 2002 02:32:45 -0800 (PST) (envelope-from chris@holly.dyndns.org) Received: (from chris@localhost) by holly.dyndns.org (8.12.6/8.11.6) id gBTAWt5k051974; Sun, 29 Dec 2002 04:32:55 -0600 (CST) (envelope-from chris) Date: Sun, 29 Dec 2002 04:32:54 -0600 From: Chris Costello To: Jordan Hubbard Cc: FreeBSD Hackers Subject: Re: Anyone like obscure stdio problems? Message-ID: <20021229103254.GB48772@holly.machined.net> Reply-To: chris@FreeBSD.ORG References: <7D91340A-1B13-11D7-A2B1-000393BB9222@apple.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <7D91340A-1B13-11D7-A2B1-000393BB9222@apple.com> User-Agent: Mutt/1.4i Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG 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 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