From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/15045 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: Rich Felker Newsgroups: gmane.linux.lib.musl.general Subject: Re: =?utf-8?B?562U5aSNOiBbbXVzbF0gW1BBVENI?= =?utf-8?Q?=5D?= ftello: Need adjust file offset before switching to write Date: Wed, 18 Dec 2019 22:03:25 -0500 Message-ID: <20191219030325.GR1666@brightrain.aerifal.cx> References: <07082d7e15024b06ba261fbb4e29eac5@huawei.com> <20191218134636.GM1666@brightrain.aerifal.cx> Reply-To: musl@lists.openwall.com Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="184513"; mail-complaints-to="usenet@blaine.gmane.org" User-Agent: Mutt/1.5.21 (2010-09-15) Cc: "musl@lists.openwall.com" , "Songyunlong (Euler)" To: "wangjianjian (C)" Original-X-From: musl-return-15061-gllmg-musl=m.gmane.org@lists.openwall.com Thu Dec 19 04:06:07 2019 Return-path: Envelope-to: gllmg-musl@m.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by blaine.gmane.org with smtp (Exim 4.89) (envelope-from ) id 1ihm8a-000low-Rn for gllmg-musl@m.gmane.org; Thu, 19 Dec 2019 04:06:04 +0100 Original-Received: (qmail 20164 invoked by uid 550); 19 Dec 2019 03:06:02 -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 20132 invoked from network); 19 Dec 2019 03:06:02 -0000 Content-Disposition: inline In-Reply-To: Original-Sender: Rich Felker Xref: news.gmane.org gmane.linux.lib.musl.general:15045 Archived-At: On Thu, Dec 19, 2019 at 02:47:46AM +0000, wangjianjian (C) wrote: > I don't think this has something to do with ungetc or UB. > If don't call ungetc and replace fputs with others, like fwrite or > fputc, the result of ftell or final file content is not correct. You cannot switch between reading and writing on a stream that's open for both without a successful seek in between, unless the read ended with hitting EOF. You're right that if you remove the ungetc, the UB is still there because fgetc is still there. Removing both, or putting a seek after them but before the fputs, will make things work as expected. For details see 7.21.5.3 The fopen function, ¶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." http://port70.net/~nsz/c/c11/n1570.html#7.21.5.3 Rich -----邮件原件----- > 发件人: Rich Felker [mailto:dalias@aerifal.cx] 代表 Rich Felker > 发送时间: 2019年12月18日 21:47 > 收件人: wangjianjian (C) > 抄送: musl@lists.openwall.com; Songyunlong (Euler) > 主题: Re: [musl] [PATCH] ftello: Need adjust file offset before switching to write > > On Wed, Dec 18, 2019 at 10:18:16AM +0000, wangjianjian (C) wrote: > > Consider below code flow: > > > > FILE *fp = fopen("foobar", "w+"); > > fputs("hello", fp); > > rewind(fp); > > fgetc(fp); > > ungetc('x', fp); > > fputs("world", fp); > ^^^^^^^^^^^^^^^^^^^ > > This line produces UB. You can't perform a write after ungetc without an intervening successful seek (or hitting EOF). > > Rich