bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: GCC optimizes integer overflow: bug or feature?


From: Paolo Bonzini
Subject: Re: GCC optimizes integer overflow: bug or feature?
Date: Fri, 22 Dec 2006 08:59:52 +0100
User-agent: Thunderbird 1.5.0.9 (Macintosh/20061207)


      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




reply via email to

[Prev in Thread] Current Thread [Next in Thread]