This is the mail archive of the gcc@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: GCC optimizes integer overflow: bug or feature?



      int j;
      for (j = 1; 0 < j; j *= 2)
	if (! bigtime_test (j))
	  return 1;

Here it is obvious to a programmer that the comparison is
intended to do overflow checking, even though the test
controls the loop.

Well, it's not to me. :-)


Another question for the GCC experts: would it fix the bug
if we replaced "j *= 2" with "j <<= 1" in this sample code?

Yes, it will actually compile the code as this:


  int i, j;
  for (i = 0, j = 1; i < 31; i++)
    j <<= 1;

Or you can do, since elsewhere in the code you compute time_t_max:

for (j = 1; j <= time_t_max / 2 + 1; j *= 2)

Which is IMO more intention revealing.

Paolo


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