[Egdy] gcc has a strange behaviour -2 > 0 ?

Bug #57194 reported by Christophe Dumez
4
Affects Status Importance Assigned to Milestone
gcc-defaults (Ubuntu)
Invalid
Undecided
Matthias Klose

Bug Description

Binary package hint: gcc

Hi,

I recompiled libtorrent library (http://libtorrent.sf.net) with
gcc (GCC) 4.1.2 20060817 (prerelease) (Ubuntu 4.1.1-11ubuntu1) and I experience a problem I didn't have with previous versions of gcc.

"allocate_resources.cpp:80: int libtorrent::saturated_add(int, int): assert « sum >= a && sum >= b » failed."

I added some debug messages to the function, as follows:

int saturated_add(int a, int b)
{
  assert(a >= 0);
  assert(b >= 0);
  assert(a <= resource_request::inf);
  assert(b <= resource_request::inf);
  assert(resource_request::inf + resource_request::inf < 0);
  int sum = a + b;
  std::cout << "a: " << a << '\n';
  std::cout << "b: " << b << '\n';
  std::cout << "(a+b) sum: " << sum << '\n';
  std::cout << "resource_request::inf: " << resource_request::inf << '\n';
  if (sum < 0){
    std::cout << "sum < 0\n";
    sum = resource_request::inf;
    std::cout << "new sum: " << sum << '\n';
  }else{
    std::cout << "sum >= 0\n";
  }
  std::cout << "sum >= a: " << (sum>=a) << '\n';
  std::cout << "sum >= b: " << (sum>=b) << '\n';
  assert(sum >= a && sum >= b);
  return sum;
}

Here is the output:

a: 2147483647
b: 2147483647
(a+b) sum: -2
resource_request::inf: 2147483647
sum >= 0
sum >= a: 0
sum >= b: 0
qbittorrent: allocate_resources.cpp:87: int libtorrent::saturated_add(int, int): assert « sum >= a && sum >= b » failed.

How can -2>=0 ??? strange, right? My machine is standard: intel centrino 32bits.
I'm compiling with this gcc:
chris@chris-laptop:~/qbittorrent_svn/src$ gcc --version
gcc (GCC) 4.1.2 20060817 (prerelease) (Ubuntu 4.1.1-11ubuntu1)

I tried again with an older gcc:
chris-laptop# gcc --version
gcc (GCC) 4.0.4 20060630 (prerelease) (Ubuntu 4.0.3-4)

and this gcc worked fine... see:
a: 2147483647
b: 2147483647
sum: -2
resource_request::inf: 2147483647
sum < 0
new sum: 2147483647
sum >= a: 1
sum >= b: 1

I don't think the problem comes from libtorrent code. With the lastest gcc the statement "-2<0" is false is this is simply wrong.

Revision history for this message
Andreas Simon (andreas-w-simon) wrote :

It just wanted to add that when extracting the arithmetic and comparision code from above it works as expected:

$ cat test.cpp
#include <iostream>

int main() {
        int a = 2147483647;
        int b = 2147483647;
        int sum = a + b;
        std::cout << "sum = " << sum << '\n';

        if (sum < 0)
                std::cout << "sum < 0" << '\n';
        else
                std::cout << "sum >= 0" << '\n';
}

$ g++ test.cpp -o test
$ ./test
sum = -2
sum < 0
$ g++ --version|grep rel
g++ (GCC) 4.1.2 20060817 (prerelease) (Ubuntu 4.1.1-11ubuntu1)

Revision history for this message
Christophe Dumez (hydr0g3n) wrote :

Yes I tried the same test as you on my computer and it passed successfuly too. I don't know what is different in the code of libtorrent...

Revision history for this message
Matthias Klose (doko) wrote :

No bug. Signed int have no defined overflow behaviour. You have to use
unsigned int.

Changed in gcc-defaults:
assignee: nobody → doko
status: Unconfirmed → Rejected
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.