0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-12-01 09:32:32 +01:00

fix $inc overflow on int SERVER-2005

This commit is contained in:
Eliot Horowitz 2010-11-01 23:38:17 -04:00
parent b0140357af
commit 2dd5f90b5d
2 changed files with 16 additions and 3 deletions

View File

@ -67,8 +67,16 @@ namespace mongo {
ms.inclong = elt.numberLong() + in.numberLong();
}
else {
ms.incType = NumberInt;
ms.incint = elt.numberInt() + in.numberInt();
int x = elt.numberInt() + in.numberInt();
if ( x < 0 && elt.numberInt() > 0 && in.numberInt() > 0 ){
// overflow
ms.incType = NumberLong;
ms.inclong = elt.numberLong() + in.numberLong();
}
else {
ms.incType = NumberInt;
ms.incint = elt.numberInt() + in.numberInt();
}
}
ms.appendIncValue( bb , false );
@ -398,6 +406,11 @@ namespace mongo {
// if i'm incrememnting with a double, then the storage has to be a double
mss->amIInPlacePossible( m.elt.type() != NumberDouble );
}
// check for overflow
if ( e.type() == NumberInt && e.numberLong() + m.elt.numberLong() > numeric_limits<int>::max() ){
mss->amIInPlacePossible( false );
}
}
break;

View File

@ -759,7 +759,7 @@ namespace UpdateTests {
while ( start < max ){
update( BSON( "$inc" << BSON( "x" << 500000 ) ) );
start += 500000;
//ASSERT_EQUALS( start , findOne()["x"].numberLong() ); // SERVER-2005
ASSERT_EQUALS( start , findOne()["x"].numberLong() ); // SERVER-2005
}
}