From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/7821 Path: news.gmane.org!not-for-mail From: Rich Felker Newsgroups: gmane.linux.lib.musl.general Subject: Re: Making stdio writes robust/recoverable under errors Date: Fri, 29 May 2015 10:36:05 -0400 Message-ID: <20150529143605.GI17573@brightrain.aerifal.cx> References: <20150529135300.GD17573@brightrain.aerifal.cx> 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 1432910180 22043 80.91.229.3 (29 May 2015 14:36:20 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Fri, 29 May 2015 14:36:20 +0000 (UTC) To: musl@lists.openwall.com Original-X-From: musl-return-7833-gllmg-musl=m.gmane.org@lists.openwall.com Fri May 29 16:36:19 2015 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 1YyLOV-0005nD-O1 for gllmg-musl@m.gmane.org; Fri, 29 May 2015 16:36:19 +0200 Original-Received: (qmail 13363 invoked by uid 550); 29 May 2015 14:36:18 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: Original-Received: (qmail 13345 invoked from network); 29 May 2015 14:36:18 -0000 Content-Disposition: inline In-Reply-To: <20150529135300.GD17573@brightrain.aerifal.cx> User-Agent: Mutt/1.5.21 (2010-09-15) Original-Sender: Rich Felker Xref: news.gmane.org gmane.linux.lib.musl.general:7821 Archived-At: On Fri, May 29, 2015 at 09:53:00AM -0400, Rich Felker wrote: > Currently, musl's stdio write operations take the liberty of leaving > the current position and actual contents written rather unpredictable > if a write error occurs. Originally the motivation was probably a mix > of uncertainty about what to do and lack of code (or a desire to avoid > complexity) for tracking what part of the buffer was unwritten if a > write error occurred before the buffer was written out. But commit > 58165923890865a6ac042fafce13f440ee986fd9, as part of adding > cancellation support, added the code to track what part of the buffer > remains unwritten, and therefore, from what I can see, avoiding data > loss when stdio writes fail transiently (like EINTR, for instance) is > simply a matter of removing line 31 in the error path of > __stdio_write: > > f->wpos = f->wbase = f->wend = 0; > > so that the buffer is not thrown away. fflush is also doing its down discarding on line 15 with the same assignment, but in the 'success' path, which is taken with the above line in __stdio_write removed -- in other words, fflush fails to detect failure with the above change. I think the code on lines 6-9 of fflush.c should be changed from: if (f->wpos > f->wbase) { f->write(f, 0, 0); if (!f->wpos) return EOF; } to explicitly check for non-empty buffer: if (f->wpos > f->wbase) { f->write(f, 0, 0); if (f->wpos > f->wbase) return EOF; } Then the subsequent zeroing of the buffer pointers in the success case is not discarding anything, but just disabling writes through the buffer. Rich