From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/9275 Path: news.gmane.org!not-for-mail From: Rich Felker Newsgroups: gmane.linux.lib.musl.general Subject: Re: bug in fwrite/__towrite Date: Fri, 5 Feb 2016 10:40:21 -0500 Message-ID: <20160205154021.GW9349@brightrain.aerifal.cx> References: <56B4C0AA.3020705@gmx.at> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: ger.gmane.org 1454686839 19045 80.91.229.3 (5 Feb 2016 15:40:39 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Fri, 5 Feb 2016 15:40:39 +0000 (UTC) To: musl@lists.openwall.com Original-X-From: musl-return-9288-gllmg-musl=m.gmane.org@lists.openwall.com Fri Feb 05 16:40:39 2016 Return-path: Envelope-to: gllmg-musl@m.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by plane.gmane.org with smtp (Exim 4.69) (envelope-from ) id 1aRiUw-0001zs-FM for gllmg-musl@m.gmane.org; Fri, 05 Feb 2016 16:40:38 +0100 Original-Received: (qmail 21679 invoked by uid 550); 5 Feb 2016 15:40:36 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Original-Received: (qmail 21660 invoked from network); 5 Feb 2016 15:40:35 -0000 Content-Disposition: inline In-Reply-To: <56B4C0AA.3020705@gmx.at> User-Agent: Mutt/1.5.21 (2010-09-15) Original-Sender: Rich Felker Xref: news.gmane.org gmane.linux.lib.musl.general:9275 Archived-At: On Fri, Feb 05, 2016 at 04:32:58PM +0100, hombre wrote: > Hello, > > I think there is a bug in fwrite/__towrite. > > This is my unittest that fails: > static void test_write_read2(const char *fname) > { > char wbuf[3]; > char c; > > FILE *file = fopen(fname, "wb"); > assert(file != NULL); > wbuf[0] = 'a'; > wbuf[1] = 'b'; > wbuf[2] = 'c'; > size_t written = fwrite(wbuf, 1, 3, file); > assert(written == 3); > fclose(file); > > file = fopen(fname, "rb+"); > size_t nread = fread(&c, 1, 1, file); > assert(nread == 1); > assert(c == 'a'); > c = 'B'; > written = fwrite(&c, 1, 1, file); This line caused undefined behavior. You cannot switch between writing and reading on a stream without an intervening successful fseek or fflush (the latter only in the write->read direction). See 7.21.5.3 The fopen function, paragraph 7: "When a file is opened with update mode ('+' as the second or third character in the above list of mode argument values), both input and output may be performed on the associated stream. However, output shall not be directly followed by input without an intervening call to the fflush function or to a file positioning function (fseek, fsetpos, or rewind), and input shall not be directly followed by output without an intervening call to a file positioning function, unless the input operation encounters end- of-file. Opening (or creating) a text file with update mode may instead open (or create) a binary stream in some implementations." > assert(written == 1); > nread = fread(&c, 1, 1, file); And again. > assert(nread == 1); /* <================== nread is 0 here ! */ > assert(c == 'c'); > fclose(file); > } Rich