Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-7446

$inc should handle negative overflow the same as positive overflow

    • Type: Icon: Improvement Improvement
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 2.5.2
    • Affects Version/s: None
    • Component/s: Write Ops
    • Labels:
      None
    • Fully Compatible

      Right now if $inc would cause two positive ints to overflow, the result is converted to a long to prevent overflow. However, if two negative ints are added the result may overflow to a negative int.

      Test

      c = db.c;
      
      c.drop();
      c.save( { a:NumberInt( 0x7fffffff ) } );
      c.update( {}, { $inc:{ a:NumberInt( 10000 ) } } );
      // The sum of two positive ints is converted to a positive long on overflow.                                                                                                         
      printjson( c.find().toArray() );
      
      c.drop();
      c.save( { a:NumberInt( 0xffffffff ) } );
      c.update( {}, { $inc:{ a:NumberInt( -10000 ) } } );
      // The sum of two negative ints overflows to a positive int.                                                                                                                         
      printjson( c.find().toArray() );
      

            Assignee:
            Unassigned Unassigned
            Reporter:
            aaron Aaron Staple
            Votes:
            5 Vote for this issue
            Watchers:
            8 Start watching this issue

              Created:
              Updated:
              Resolved: