Path: csiph.com!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: Buffer contents well-defined after fgets() reaches EOF ? Date: Tue, 11 Feb 2025 13:59:33 -0800 Organization: None to speak of Lines: 27 Message-ID: <87msesnnwa.fsf@nosuchdomain.example.com> References: MIME-Version: 1.0 Content-Type: text/plain Injection-Date: Tue, 11 Feb 2025 22:59:34 +0100 (CET) Injection-Info: dont-email.me; posting-host="4801b0c70125e12e38b1effcec89a0a8"; logging-data="2072317"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+h+EKDv7vPnIgXjhlH9QWQ" User-Agent: Gnus/5.13 (Gnus v5.13) Cancel-Lock: sha1:NAZCe31rjGSN4/8dnx5Ele3fdos= sha1:LK3dpDduHPd9xWqGvpyr8Z3Fo7g= Xref: csiph.com comp.lang.c:390307 James Kuyper writes: > On 2/10/25 23:54, Lawrence D'Oliveiro wrote: >> On Mon, 10 Feb 2025 22:33:03 -0500, James Kuyper wrote: > ... >>> "The fgets function reads at most one less than the number of characters >>> specified by n from the stream pointed to by stream into the array >>> pointed to by s." (7.32.7.2p2) >>> >>> If the buffer length is 1, "at most one less than the number ... >>> specified" is 0. Therefore, fgets() cannot read any characters into the >>> buffer, no matter what the contents of the input stream are. Again, >>> since there is no "last byte read into the array", there is no location >>> where a null byte should be written. >> >> Have you tried it? I have. > > I just tried it, using gcc and found that fgets() does set the first > byte of the buffer to a null character. Therefore, it doesn't conform to > the requirements of the standard. That's not particularly surprising - > calling fgets with useless arguments isn't something that I'd expect to > be a high priority on their pre-delivery tests. As you know, gcc doesn't implement fgets(). Were you using GNU libc? -- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com void Void(void) { Void(); } /* The recursive call of the void */