From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/13786 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: Rich Felker Newsgroups: gmane.linux.lib.musl.general Subject: Re: Bug in gets function? Date: Wed, 13 Feb 2019 17:13:30 -0500 Message-ID: <20190213221330.GR23599@brightrain.aerifal.cx> References: <20190212034838.GH23599@brightrain.aerifal.cx> <20190212035106.GI23599@brightrain.aerifal.cx> <20190212163027.GK23599@brightrain.aerifal.cx> <419e95bb3a69fa0990e56996e198355f@ispras.ru> Reply-To: musl@lists.openwall.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="238505"; mail-complaints-to="usenet@blaine.gmane.org" User-Agent: Mutt/1.5.21 (2010-09-15) To: musl@lists.openwall.com Original-X-From: musl-return-13802-gllmg-musl=m.gmane.org@lists.openwall.com Wed Feb 13 23:13:48 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 1gu2mo-000zvF-EP for gllmg-musl@m.gmane.org; Wed, 13 Feb 2019 23:13:46 +0100 Original-Received: (qmail 24016 invoked by uid 550); 13 Feb 2019 22:13:44 -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 23995 invoked from network); 13 Feb 2019 22:13:43 -0000 Content-Disposition: inline In-Reply-To: <419e95bb3a69fa0990e56996e198355f@ispras.ru> Xref: news.gmane.org gmane.linux.lib.musl.general:13786 Archived-At: On Thu, Feb 14, 2019 at 12:39:07AM +0300, Alexey Izbyshev wrote: > On 2019-02-12 19:30, Rich Felker wrote: > >Here's an alternate proposal via direct implementation: > > > >char *gets(char *s) > >{ > > size_t i=0; > > int c; > > FLOCK(stdin); > > while ((c=getc_unlocked(stdin)) != EOF && c != '\n') s[i++] = c; > > s[i] = 0; > > if (c != '\n' && !feof(stdin)) s = 0; > > FUNLOCK(stdin); > > return s; > >} > > > >Does this look ok? Of course it's slow compared to a fgets-like > >operation on the buffer, but gets is not a usable interface and I > >don't see any reason to care whether it's fast. > > > gets() must also return NULL if EOF is reached and no bytes were read. So if (c != '\n' && (!feof(stdin) || !i)) ? Rich