From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/13762 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: Ponnuvel Palaniyappan Newsgroups: gmane.linux.lib.musl.general Subject: Re: Bug in gets function? Date: Tue, 12 Feb 2019 14:55:19 +0000 Message-ID: References: <20190212034838.GH23599@brightrain.aerifal.cx> <20190212035106.GI23599@brightrain.aerifal.cx> Reply-To: musl@lists.openwall.com Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="000000000000e7b7580581b39d5a" Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="253717"; mail-complaints-to="usenet@blaine.gmane.org" To: musl@lists.openwall.com Original-X-From: musl-return-13778-gllmg-musl=m.gmane.org@lists.openwall.com Tue Feb 12 15:55:47 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 1gtZTP-0013uC-1l for gllmg-musl@m.gmane.org; Tue, 12 Feb 2019 15:55:47 +0100 Original-Received: (qmail 7872 invoked by uid 550); 12 Feb 2019 14:55: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 7853 invoked from network); 12 Feb 2019 14:55:43 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to; bh=nlQQaHBYF/9h93biga9/w41C7znjiBhV05mbVqbotbw=; b=mYgFC0DtuS3KqvfPPXifI5zf6/nyMAGEA7kJqZKupPBQWnu5Jj3HzG6lmShmmUGq29 vfEepS45J5ltHMdWunNTDe94y1Hnia3XEoSprRC5YW7k5EqLBcMuvdAj4CAwAZMe/PW8 YxzCbYLnjo6pcUWGQHqk88CmCaBoBHqoD9mUn8cDW4pL/30s4WmU1FMxuAn+ZpoeLIHC ETXjPOw/DThHouy0E1Q1/OYmRAx/uBPeQ7Ew9N5CmPH9/Ww1F3XAhbGc89GZ/WL6EAji wkJxYSOWAFVYzZ98/FcM+cxXxpdthI7S2jXey+Rhfca2lrKGQwS51Tyv79J8q8spZTC2 vRVA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to; bh=nlQQaHBYF/9h93biga9/w41C7znjiBhV05mbVqbotbw=; b=bPuOkp43VM5rW6MKYGqieSSgvQplGpcG2kooFjczliIgs+rU4LC5gq0Q+YlEWihYk6 joRnURetu7vJpXMiW2s6oHxmRj61UD4DBW0RjpEJsvPnQPRFWDlsgNYCqEXvrmcqfn8L Vqmx2j3JzIbqFvvBT4QR9TuigAM20vUB0lYk9Aut8jz5H0oviGy081nnBMGhVJ91X42n jYiCBiEu7FNZMK8Lf7JXuYwgukc2Z+GhhBm5SJAjVvmViObEWhv3hKjVJ3gPIoZqxHbR HcY+Rc2jndOSlZuDdsYDsj282Q11h6sF1abVTIHPl22fOmTiX4o6awppMNe1fLuJTJly MICQ== X-Gm-Message-State: AHQUAuYmJQaQdXWx0Eglq4vBaLR33zgVslGRXWulPwz6K00bGoksbQtY TcZQL7jop8iMGfhR3uW9X1QsCe/RFeY6/HaasygEPcffSAs= X-Google-Smtp-Source: AHgI3Iae71AhyeI9A1kw8YNTj5fvniQXgfrbAlKv+4Z2ZCkxMd0wJZ3BNnuziPslD6vBwFdCiqusBrlLX0YqxAj1O/k= X-Received: by 2002:a5d:51cd:: with SMTP id n13mr3077007wrv.310.1549983331889; Tue, 12 Feb 2019 06:55:31 -0800 (PST) In-Reply-To: Xref: news.gmane.org gmane.linux.lib.musl.general:13762 Archived-At: --000000000000e7b7580581b39d5a Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable > Is gets(s) equivalent to scanf("%[^\n]%*1[\n]",s)? I think it has at least one minor issue: it doesn't null-terminate the buffer on empty input i.e., just a newline as input. Regards, Ponnuvel On Tue, Feb 12, 2019 at 2:42 PM James Larrowe wrote: > I could probably try patching it. That C99 specification seems descriptiv= e > enough. > > On Mon, Feb 11, 2019 at 10:51 PM Rich Felker wrote: > >> On Mon, Feb 11, 2019 at 10:48:38PM -0500, Rich Felker wrote: >> > On Mon, Feb 11, 2019 at 06:55:24PM -0800, Keyhan Vakil wrote: >> > > Hi. It seems that the gets function does not follow the C99 spec. In >> > > particular, if the input contains a null byte in the middle of the >> > > input, then the new-line character is not discarded. >> > > >> > > For reference, here's the relevant part in the C99 standard >> > > (7.19.7.7): >> > > >> > > > The gets function reads characters from the input stream pointed t= o >> > > > by stdin, into the array pointed to by s, until end-of-=EF=AC=81le= is >> > > > encountered or a new-line character is read. Any new-line characte= r >> > > > is discarded, and a null character is written immediately after th= e >> > > > last character read into the array. >> > > >> > > Here is an example: >> > > >> > > #include >> > > char s[8]; >> > > int main() { >> > > gets(s); >> > > for (int i =3D 0; i < sizeof s; i++) { >> > > printf("%02x ", s[i]); >> > > } >> > > printf("\n"); >> > > return 0; >> > > } >> > > >> > > When compiled against gcc: >> > > >> > > $ echo -e 'A\x00B' | ./a.out >> > > 41 00 42 00 00 00 00 00 >> > > >> > > When compiled against musl: >> > > >> > > $ echo -e 'A\x00B' | ./a.out >> > > 41 00 42 0a 00 00 00 00 >> > > >> > > Note the terminating newline, which contradicts the spec. >> > >> > I think this bug report is correct; however the gets function is >> > awful, removed in C11, and should never be used. :-) >> > >> > I will see what can be done to fix it though. >> >> Is gets(s) equivalent to scanf("%[^\n]%*1[\n]",s)? If so that would be >> an appropriately hideous way to implement it that avoids the current >> bug? :-) >> >> Rich >> > --=20 Regards, Ponnuvel P --000000000000e7b7580581b39d5a Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
>=C2=A0 =C2=A0Is gets(s) equivalent to= scanf("%[^\n]%*1[\n]",s)?

I think it has at l= east one minor issue: it doesn't null-terminate the buffer on empty inp= ut i.e., just a newline as input.

Regards,
Ponnuvel

On Tue, Feb 12, 2019 at 2:42 PM James Larrowe <larrowe.semaj11@gmail.com> wro= te:
I could probably try patching it. That C99 specification seems descrip= tive enough.

On Mon, Feb 11, 2019 at 10:51 PM Rich Felker <dalias@libc.org> wrote:
On Mon, Feb 11, 2019 = at 10:48:38PM -0500, Rich Felker wrote:
> On Mon, Feb 11, 2019 at 06:55:24PM -0800, Keyhan Vakil wrote:
> > Hi. It seems that the gets function does not follow the C99 spec.= In
> > particular, if the input contains a null byte in the middle of th= e
> > input, then the new-line character is not discarded.
> >
> > For reference, here's the relevant part in the C99 standard > > (7.19.7.7):
> >
> > > The gets function reads characters from the input stream poi= nted to
> > > by stdin, into the array pointed to by s, until end-of-=EF= =AC=81le is
> > > encountered or a new-line character is read. Any new-line ch= aracter
> > > is discarded, and a null character is written immediately af= ter the
> > > last character read into the array.
> >
> > Here is an example:
> >
> >=C2=A0 =C2=A0 =C2=A0#include <stdio.h>
> >=C2=A0 =C2=A0 =C2=A0char s[8];
> >=C2=A0 =C2=A0 =C2=A0int main() {
> >=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0gets(s);
> >=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0for (int i =3D 0; i < sizeof = s; i++) {
> >=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0printf("%02x = ", s[i]);
> >=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0}
> >=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0printf("\n");
> >=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0return 0;
> >=C2=A0 =C2=A0 =C2=A0}
> >
> > When compiled against gcc:
> >
> >=C2=A0 =C2=A0 =C2=A0$ echo -e 'A\x00B' | ./a.out
> >=C2=A0 =C2=A0 =C2=A041 00 42 00 00 00 00 00
> >
> > When compiled against musl:
> >
> >=C2=A0 =C2=A0 =C2=A0$ echo -e 'A\x00B' | ./a.out
> >=C2=A0 =C2=A0 =C2=A041 00 42 0a 00 00 00 00
> >
> > Note the terminating newline, which contradicts the spec.
>
> I think this bug report is correct; however the gets function is
> awful, removed in C11, and should never be used. :-)
>
> I will see what can be done to fix it though.

Is gets(s) equivalent to scanf("%[^\n]%*1[\n]",s)? If so that wou= ld be
an appropriately hideous way to implement it that avoids the current
bug? :-)

Rich


--
Regards,
Ponnuvel P
--000000000000e7b7580581b39d5a--