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 6940 invoked from network); 26 Jun 2020 16:06:44 -0000 Received: from mother.openwall.net (195.42.179.200) by inbox.vuxu.org with ESMTPUTF8; 26 Jun 2020 16:06:44 -0000 Received: (qmail 1894 invoked by uid 550); 26 Jun 2020 16:06:40 -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 1876 invoked from network); 26 Jun 2020 16:06:39 -0000 Date: Fri, 26 Jun 2020 12:06:24 -0400 From: Rich Felker To: musl@lists.openwall.com Message-ID: <20200626160624.GZ6430@brightrain.aerifal.cx> References: <20200625153803.GO6430@brightrain.aerifal.cx> <20200626051400.GB13001@voyager> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200626051400.GB13001@voyager> User-Agent: Mutt/1.5.21 (2010-09-15) Subject: Re: [musl] errno not set to EBADF when reading from invalid descriptor On Fri, Jun 26, 2020 at 07:14:00AM +0200, Markus Wichmann wrote: > On Thu, Jun 25, 2020 at 11:38:04AM -0400, Rich Felker wrote: > > Note that there are lots of other reasons you can't safely use stdio > > read functions on a stream you don't know is suitable for it. > > Switching from writing to reading without a successful flush produces > > UB, and switching the other direction without a successful seek does, > > even if the FILE stream is open for both. > > So I can't actually use a FILE open for reading and writing that refers > to something non-seekable, like a serial line, to both read and write to > it? I mean, I suppose I could write something and then flush and stay in > read mode forever, but that is not really the nature of my application. > I suppose I'll just open the serial line twice. Indeed, you basically always need 2 FILEs for bidirectional io. The only case where you don't is when it's a normal seekable file and you're willing to follow the arcane rules for switching modes (which may make sense if it's a trivial case like writing whole file, rewinding, then only reading from that point forward, which is a fairly canonical use case for tmpfile()). Rich