Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date: Fri, 2 Feb 2018 14:54:27 +0100
From: Jens Gustedt <jens.gustedt@...ia.fr>
To: CodingMarkus <CodingMarkus@...auska.name>
Cc: musl@...ts.openwall.com
Subject: Re: Why are stdin/stdout/stderr `FILE *const` in musl?

Hello CodingMarkus,

On Fri, 2 Feb 2018 14:24:28 +0100 CodingMarkus
<CodingMarkus@...auska.name> 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,
> 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.

I don't think so. The C standard says

 > stderr
 > stdin
 > stdout
 > which are expressions of type "pointer to FILE" that point to the FILE objects associated, respectively,

which clearly states that these are "expressions" not lvalues, and so
your code

> 	void * getOutputPtr ( void ) {
> 		if (/* whatever */) {
> 			return &stdout;
> 		}
> 		return &stderr;
> 	}

is not conforming. In is undefined (by omission of defining it) to
take the address of such an expression.

(This is different from errno, e.g, which is defined as a modifiable
lvalue.)

> and it is correct by POSIX
> standard but it won’t compile with strict compiler settings when
> using musl as the compiler complains that the `const` is lost and
> there is no explicitly cast for that. 

> 	error: cannot initialize return object of type 'void *' with
> an rvalue of type 'FILE *const *'

and this is a feature, because it points to where your program has
undefined behavior.

The C standard has freopen for the cases that you want to change the
association or the access mode of one of the standard streams.

Jens

-- 
:: INRIA Nancy Grand Est ::: Camus ::::::: ICube/ICPS :::
:: ::::::::::::::: office Strasbourg : +33 368854536   ::
:: :::::::::::::::::::::: gsm France : +33 651400183   ::
:: ::::::::::::::: gsm international : +49 15737185122 ::
:: http://icube-icps.unistra.fr/index.php/Jens_Gustedt ::

Content of type "application/pgp-signature" skipped

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.