Navigation Menu

Skip to content

Commit

Permalink
fix $inc overflow on int SERVER-2005
Browse files Browse the repository at this point in the history
  • Loading branch information
erh committed Nov 2, 2010
1 parent b014035 commit 2dd5f90
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
17 changes: 15 additions & 2 deletions db/update.cpp
Expand Up @@ -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 );
Expand Down Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion dbtests/updatetests.cpp
Expand Up @@ -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
}

}
Expand Down

0 comments on commit 2dd5f90

Please sign in to comment.