From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/12454 Path: news.gmane.org!.POSTED!not-for-mail From: Rich Felker Newsgroups: gmane.linux.lib.musl.general Subject: Re: Why are stdin/stdout/stderr `FILE *const` in musl? Date: Fri, 2 Feb 2018 12:35:01 -0500 Message-ID: <20180202173501.GS1627@brightrain.aerifal.cx> References: <1E109BA9-57C0-42DB-9B43-8ADE27F9E76C@hanauska.name> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Trace: blaine.gmane.org 1517592810 10532 195.159.176.226 (2 Feb 2018 17:33:30 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Fri, 2 Feb 2018 17:33:30 +0000 (UTC) User-Agent: Mutt/1.5.21 (2010-09-15) To: musl@lists.openwall.com Original-X-From: musl-return-12470-gllmg-musl=m.gmane.org@lists.openwall.com Fri Feb 02 18:33:26 2018 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.84_2) (envelope-from ) id 1ehfD5-0001av-39 for gllmg-musl@m.gmane.org; Fri, 02 Feb 2018 18:33:11 +0100 Original-Received: (qmail 26087 invoked by uid 550); 2 Feb 2018 17:35:14 -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 26066 invoked from network); 2 Feb 2018 17:35:13 -0000 Content-Disposition: inline In-Reply-To: <1E109BA9-57C0-42DB-9B43-8ADE27F9E76C@hanauska.name> Original-Sender: Rich Felker Xref: news.gmane.org gmane.linux.lib.musl.general:12454 Archived-At: On Fri, Feb 02, 2018 at 02:24:28PM +0100, CodingMarkus wrote: > Hello everyone! > > Just a quick question: > Why does musl define stdin, stdout, and stderr to be of type `FILE > *const`? Neither the C standard, nor the POSIX standard require, They don't require it, but they allow it. musl's implementation _does_ require it, because behind the macros (all the C standard requires to exist) are const-qualified objects (living inside libc.so/libc.a), and declaring them with a type that mismatches their definitions would result in undefined behavior. > recommend or even imply that it would be allowed that this is a > `const` pointer. That’s why other C libraries define it as `FILE *` > only because that matches the examples given by POSIX and that > matches the description found in any ISO-C standard. Making them > const only break compatibility with other C libraries, e.g. > considered this code: > > void * getOutputPtr ( void ) { > if (/* whatever */) { > return &stdout; > } > return &stderr; > } > > This code is correct by C standard and it is correct by POSIX It's not. As others have noted, stderr is not specified as an object; it's a macro that expands to an expression with type FILE *. You cannot take the address of an expression in general. Rich