From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.3 required=5.0 tests=MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 13722 invoked from network); 25 Oct 2021 17:26:15 -0000 Received: from mother.openwall.net (195.42.179.200) by inbox.vuxu.org with ESMTPUTF8; 25 Oct 2021 17:26:15 -0000 Received: (qmail 11848 invoked by uid 550); 25 Oct 2021 17:26:12 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Reply-To: musl@lists.openwall.com Received: (qmail 11814 invoked from network); 25 Oct 2021 17:26:12 -0000 Date: Mon, 25 Oct 2021 13:25:57 -0400 From: Rich Felker To: Lorenzo Beretta Cc: musl@lists.openwall.com Message-ID: <20211025172557.GT7074@brightrain.aerifal.cx> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) Subject: Re: [musl] Re: request: please detect reads from stdin with unflushed On Mon, Oct 25, 2021 at 03:17:13PM +0200, Lorenzo Beretta wrote: > > Suggestion: fix those broken programs! > > I know, I know... the problem is that they work with glibc and they fail > silently with musl, and on top of that I've just discovered that at least > netbsd and openbsd do the same as glibc, ie a broken program like > #include > int main(){ > printf("not flushed: "); > char line[123]; > return fgets(line,sizeof line,stdin) ? 0 : 123; > } > > happens to work!!! > > What I'm asking is that musl (while technically correct!) helps > __detecting__ those programs, possibly as an option ("#ifdef > HUMOR_BROKEN_PROGRAMS") > > PS > I'm not subscribed to this mailing list, sorry for not mentioning it the > first time There's nothing detectable here because there's nothing wrong with the program; the bug is in the programmer's *expectation* that the output be visible. It's possible to implement the behavior the programmer here desired, the optional flushing of line-buffered output streams before reading input. This would not help detect the bug in expectaions though; it would just help mask it. The reason this behavior is not present in musl is because it does not scale with significant numbers of stdio streams open, and can even produce deadlock conditions in multithreaded programs where there is no semantic deadlock but the additional flushing produces an extraneous operation on a stream in a way that causes deadlock. If you hit a program with an issue like this, it should be fairly easy to fix by adding fflush(stdout) or fflush(0) immediately before the relevant input operations. Rich