This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Optimisations and undefined behaviour


On 2015-11-09 14:29:25 +0000, Andrew Haley wrote:
> Here it is again:
> 
> int foo(int x) {
> 	if (x > 1290) {
> 		printf("X is wrong here %d, but we don't care\n", x);
> 	}
> 	return x*x*x;
> 
> Here, the printf writes to a stream then the UB happens.  But the
> stream is buffered and the UB kills the process before the stream is
> flushed.  There is nothing in the C specification to prevent this, and
> neither should there be.  I don't think it's even possible.

But it may happen that the buffer is full, so that the stream will
be flushed and the printf can block forever (e.g. is the user pipes
the output to a process that doesn't read anything) so that the
"return x*x*x;" is not reacheable for this particular instance.
So, in this case, the UB never happens.

-- 
Vincent Lefèvre <vincent@vinc17.net> - Web: <https://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <https://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]