This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Crazy compiler optimization
- From: Jonathan Wakely <jwakely dot gcc at gmail dot com>
- To: vijay nag <vijunag at gmail dot com>
- Cc: "gcc-help at gcc dot gnu dot org" <gcc-help at gcc dot gnu dot org>
- Date: Wed, 9 Oct 2013 10:54:44 +0100
- Subject: Re: Crazy compiler optimization
- Authentication-results: sourceware.org; auth=none
- References: <CAKhyrx_1m0K868TVXg8kwo+EhbB+VFV0RkPqDHJ+FA8BV4w4Gw at mail dot gmail dot com>
On 9 October 2013 10:36, vijay nag wrote:
> Hello GCC,
>
> I'm facing a wierd compiler optimization problem. Consider the code
> snippet below
>
> #include <stdio.h>
>
> int printChar(unsigned long cur_col, unsigned char c)
> {
> char buf[256];
> char* bufp = buf;
> char cnt = sizeof(buf) - 2; /* overflow in implicit type conversion */
> unsigned long terminal_width = 500;
>
> while ((cur_col++ < terminal_width) && cnt) {
> *bufp++ = c;
> cnt--;
> }
> Basically the crash here is because of elimination of the check in the
> if-clause "&& cnt" which is causing stack overrun and thereby SIGSEGV.
> While standards may say that the behaviour is
> undefined when an unsigned value is stored in a signed value,
Standards do not say that. 254 cannot be presented in a char if char
is a signed type, so it's an overflow, which is undefined behaviour.
Storing an unsigned value that doesn't overflow is OK.
> can a
> language lawyer explain to me why GCC chose to eliminate code
> pertaining to cnt considering it as dead-code ?
cnt is initialized to -2 (after an overflow) and then you decrement it
so it gets more negative. The "&& cnt" condition will never be false,
because cnt starts non-zero and gets further from zero, so will never
reach zero.