Bug 79979 - For loop fails to exit
Summary: For loop fails to exit
Status: CLOSED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 4.8.3
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2017-03-09 15:43 UTC by jdowdells
Modified: 2017-03-09 16:21 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments
Test code which shows this issue (11.53 KB, application/x-compressed)
2017-03-09 15:43 UTC, jdowdells
Details

Note You need to log in before you can comment on or make changes to this bug.
Description jdowdells 2017-03-09 15:43:21 UTC
Created attachment 40938 [details]
Test code which shows this issue

When I compile this code:


#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>

typedef enum
{
	e_0,
	e_1,
	e_2,
	e_3,
	e_4,
	e_5,
	e_6,
	e_7,
	e_8,
	e_9
}EEEE;

int dummy_array[8];

int main(void)
{
	int x = dummy_array[8];
	EEEE e;


	printf("X = %d\n", x);
	printf("address of dummy_array[0] = %p\n", &dummy_array[0]);
	printf("address of dummy_array[100] = %p\n", &dummy_array[100]);

	for (e = e_0; e<e_9; e++)
	{
		printf("TEST: %d\n", dummy_array[e]);
	}

	return 0;
}



using:

    gcc -O3 -o test test.c

then the for loop never exits ( eventually resulting in seg fault )

If I compile un-opitmised or change:

   EEEE e;

to 

   volatile EEEE e;

or to

   int e;

or the array to:

    int dummy_array[10];

then the loop exits as expected.

I know that the array access in the original code is out of bounds and such an access is undefined but we'd expect the for loop to exit even in this circumstance.

I tried MSVC and it behaves in an intuitive manner.

I've attached a tgz file of my test build with:

      gcc -v -save-temps -O3 -o test test.c

Thanks
Comment 1 Marek Polacek 2017-03-09 15:54:50 UTC
Given the UB you're invoking, all bets are off and the compiler is free to do anything.  I don't think we want to change anything here.  Even -Warray-bounds warns here.
Comment 2 jdowdells 2017-03-09 16:03:57 UTC
Thanks for your quick reply.

However, this is not intuitive, the compiler should be "free to do anything" in a sensible intuitive manner.

The for loop is well defined and there is no conceivable circumstance that should allow it to loop forever.

The optimized/unoptimized code should work the same but it does not.

If you want to make the unoptimized code loop for ever I'll be more than happy

Thanks again.
Comment 3 Jakub Jelinek 2017-03-09 16:12:39 UTC
No, undefined behavior means undefined behavior, anything can happen once you trigger it, it can format your disk, launch missiles, do nothing, do what you expect, do somethng different.  The compiler optimizes on the assumption that the code is valid and does not invoke undefined behavior.
Comment 4 Jakub Jelinek 2017-03-09 16:13:47 UTC
Please read e.g. http://blog.regehr.org/archives/213 or other articles about UB and just fix your code.
Comment 5 jdowdells 2017-03-09 16:21:42 UTC
Thanks for your feedback.