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 9650 invoked from network); 8 Oct 2020 16:21:31 -0000 Received: from mother.openwall.net (195.42.179.200) by inbox.vuxu.org with ESMTPUTF8; 8 Oct 2020 16:21:31 -0000 Received: (qmail 3604 invoked by uid 550); 8 Oct 2020 16:21:28 -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 3586 invoked from network); 8 Oct 2020 16:21:28 -0000 Date: Thu, 8 Oct 2020 12:21:16 -0400 From: Rich Felker To: musl@lists.openwall.com Cc: John Scott Message-ID: <20201008162115.GZ17637@brightrain.aerifal.cx> References: <20475045.GjpEljjT90@t450> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20475045.GjpEljjT90@t450> User-Agent: Mutt/1.5.21 (2010-09-15) Subject: Re: [musl] fputs/fputc doesn't set errno on failure On Thu, Oct 08, 2020 at 10:55:47AM -0400, John Scott wrote: > Hi, > > I'm not subscribed, please keep me CC'd. > > In this corner case, errno is unset despite fputs giving an error. > I'm on Debian testing using musl-gcc 1.2.1. > > #define _POSIX_C_SOURCE 200809L > #include > #include > #include > int main(void) { > FILE *f = fopen("/", "r"); > if(f == NULL) { > perror("fopen()"); > } > if(fputs("Hello world\n", f) == EOF) { > assert(errno); > perror("fputs()"); > } > } > > With glibc this prints EBADF for fputs. > > The wiki page about writing tests [1], which I thought this would be > a good candidate for, has the 404 link > http://nsz.repo.hu/git/?p=libc-test > > Please let me know if I can help debugging. > > [1] https://wiki.musl-libc.org/writing-tests.html The test is invoking undefined behavior by calling an output function on a stream not opened for write or update mode. If you instead opened "/" with O_RDONLY but then passed it to fdopen with mode "r+" or similar, the POSIX-specified "shall fail" for "The file descriptor underlying stream is not a valid file descriptor open for writing" would apply and EBADF would be set (assuming fdopen didn't take the liberty to fail here, which POSIX allows but does not require and musl does not do since it would make fdopen more expensive). Rich