14

I had just taken the decision to change as many variables from unsigned to int and upon recompiling the code in question, was greeted by this warning message:

freespace_state.c:203: warning: assuming that the loop is not infinite

The line in question:

for (x = startx; x <= endx; ++x, ++xptr)

This loop is 60 lines of code (inc white space/brackets etc), and has a goto within it, and at least one occurrence of continue.

In this case, I think I am appreciative that GCC is assuming this loop is not infinite, because, it should never loop indefinitely.

What is GCC trying to tell me here?

The grammar of the warning is almost suggestive that the warning should be taken within the context of some other warning, but there are none within that context.

3
  • What a strange message... I have been trying to understand if it's related to the blog post at blog.regehr.org/archives/140 , but it would be odd to emit a user-visible warning. That blog post deals with silent (and of arguable validity) optimizations. Jun 5, 2010 at 23:27
  • Are you sure you are not mixing int and unsigned in this loop? Are all these variables ints now? Jun 5, 2010 at 23:37
  • @AndreyT: all are ints except xptr which is a pointer to a uint64_t. Jun 5, 2010 at 23:58

3 Answers 3

14

According to this GCC patch from 2005, it appears that GCC is performing an "unsafe loop optimization" (and you are being warned because -funsafe-loop-optimizations is set). If the loop is infinite, this particular optimization will fail somehow.

Since you said it is a terminating loop, it sounds as though you have nothing to worry about.

Another relevant part of the patch:

@opindex Wunsafe-loop-optimizations
Warn if the loop cannot be optimized because the compiler could not
assume anything on the bounds of the loop indices.  With
@option{-funsafe-loop-optimizations} warn if the compiler made
+such assumptions.
0
2

The reason GCC warns you is because it have pulled of an unsafe optimization. Instead of

for (x = startx; x <= endx; ++x, ++xptr)

it basically uses:

for( x = startx; x < (endx+1); ++x, ++xptr)

which is correct only if endx+1 doesn't overflow, but this happens when endx is the largest possible value which means that x <= endx is always true. The compiler assumes that this doesn't happen.

The warning is sometimes a bit confusing because it's not actually the finiteness of the loop that is the point here. I don't know if there's a better candidate for a message that would be short enough for a compiler warning.

One example is the case where for example x and endx are integers the optimization could actually be interpreted as being allowed by the standard, if endx==MAX_INT you would have the condition being true which will lead to that x eventually overflows which is undefined behavior, this mean that the compiler may assume that this doesn't happen. To skip the loop entirely is a standard conforming behavior according to this interpretations.

Another case is if the program doesn't terminate during the loop or alters volatile memory (ie have observable behavior) which means that an infinite loop means undefined behavior (IIRC, at least the compiler is allowed to assume that this doesn't happen).

0

I think that GCC is telling you that it can't determine that the loop is not infinite and is carrying on with compilation regardless. It's a warning, not an error, something you may want to think about.

1
  • 2
    A run time error will never result from a compile time error, but may be a result of unheeded warnings. Jun 6, 2010 at 0:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.