[Egdy] gcc has a strange behaviour -2 > 0 ?
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://
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_
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_
assert(b <= resource_
assert(
int sum = a + b;
std::cout << "a: " << a << '\n';
std::cout << "b: " << b << '\n';
std::cout << "(a+b) sum: " << sum << '\n';
std::cout << "resource_
if (sum < 0){
std::cout << "sum < 0\n";
sum = resource_
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_
sum >= 0
sum >= a: 0
sum >= b: 0
qbittorrent: allocate_
How can -2>=0 ??? strange, right? My machine is standard: intel centrino 32bits.
I'm compiling with this gcc:
chris@chris-
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_
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.
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';
std:: cout << "sum >= 0" << '\n';
else
}
$ 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)