PHP Driver
  1. PHP Driver
  2. PHP-138

64-bit int gets truncated by PHP-MongoDB driver

    Details

    • Type: Bug Bug
    • Status: Closed Closed
    • Priority: Major - P3 Major - P3
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 1.0.10
    • Component/s: None
    • Labels:
      None
    • Environment:
      64-bit Ubuntu PHP 5.3.2 (originally reported to me from one of my team-members having this issue on OSX)
    • Operating System:
      ALL
    • # Replies:
      9
    • Last comment by Customer:
      true

      Description

      echo PHP_INT_MAX;
      // 9223372036854775807
      // confirms a 64-bit environment

      $id = array('_id' => 100000154115727);

      echo $id['_id'];
      // 100000154115727
      echo gettype($id['_id']);
      // int

      $mongo->test->insert($id);
      $id = $mongo->test->findOne();

      echo $id['_id'];
      // 430562959
      echo gettype($id['_id']);
      // int

      From the Mongo CLI everything works correctly and confirms that the 64-bit int is indeed truncated within Mongo:

      > db.test.findOne()

      { "_id" : 430562959 }

      > db.test.save(

      { "_id" : 100000154115727 }

      )

      > db.test.find()

      { "_id" : 430562959 } { "_id" : 100000154115727 }

      > db.test.find(

      { "_id" : 430562959 }

      )

      { "_id" : 430562959 }

      > db.test.find(

      { "_id" : 100000154115727 }

      )

      { "_id" : 100000154115727 }

        Activity

        Hide
        Derick Rethans
        added a comment -

        I've done some work towards this. I still need to do some wrapping up, but the changes are at:

        http://github.com/derickr/mongo-php-driver

        Show
        Derick Rethans
        added a comment - I've done some work towards this. I still need to do some wrapping up, but the changes are at: http://github.com/derickr/mongo-php-driver
        Hide
        phpMoAdmin
        added a comment -
        Show
        phpMoAdmin
        added a comment - Fix is documented at http://derickrethans.nl/64bit-ints-in-mongodb.html
        Hide
        Kristina Chodorow (Inactive)
        added a comment -

        I'll resolve once there's documentation at php.net

        Show
        Kristina Chodorow (Inactive)
        added a comment - I'll resolve once there's documentation at php.net
        Hide
        Jay Paroline
        added a comment -

        I don't think this issue can safely be called resolved as long as the default behavior is truncating to 32 bits on 64 bit platforms. I only stumbled across the aforementioned article by accident.

        It scares the crap out of me thinking about the possibility that we might have launched in production at a larger scale with truncation silently happening in the background. How long would it have taken us to discover that it was happening? That could have been a lot of corrupted data with no way to fix it after the fact.

        I understand the reasoning in the article for making you turn support for 64 bit ints on as it might somehow break old apps that rely on that behavior, but your choice is:
        1. Potentially break old apps
        2. Potentially break all new apps that expect 64 bit ints to behave as expected, leading to data loss/corruption.

        It's either that or add blinking marquee text in 48 point font on every page of the PHP driver documentation warning about this.

        Show
        Jay Paroline
        added a comment - I don't think this issue can safely be called resolved as long as the default behavior is truncating to 32 bits on 64 bit platforms. I only stumbled across the aforementioned article by accident. It scares the crap out of me thinking about the possibility that we might have launched in production at a larger scale with truncation silently happening in the background. How long would it have taken us to discover that it was happening? That could have been a lot of corrupted data with no way to fix it after the fact. I understand the reasoning in the article for making you turn support for 64 bit ints on as it might somehow break old apps that rely on that behavior, but your choice is: 1. Potentially break old apps 2. Potentially break all new apps that expect 64 bit ints to behave as expected, leading to data loss/corruption. It's either that or add blinking marquee text in 48 point font on every page of the PHP driver documentation warning about this.
        Hide
        Kristina Chodorow (Inactive)
        added a comment -

        Valid point. I will flip the default in 1.1.0.

        Show
        Kristina Chodorow (Inactive)
        added a comment - Valid point. I will flip the default in 1.1.0.
        Hide
        Kristina Chodorow (Inactive)
        added a comment -

        I've put a first pass at documenting this into the PHP manual... very bare-bones, if you want to fill it out.

        Show
        Kristina Chodorow (Inactive)
        added a comment - I've put a first pass at documenting this into the PHP manual... very bare-bones, if you want to fill it out.
        Hide
        John Greenall
        added a comment -

        I just encountered this issue. As Jay states above, the problem only became apparent in latter stages of testing and it does feel like we only narrowly avoided disaster.

        The issue was fixed by explicitly enabling native_long. The reason I am reporting this here is that I am using version 1.4.1 so based on this thread would expect it to be on by default. Any suggestions as to why it hasn't been?
        output of phpinfo:
        ***************************************
        MongoDB Support enabled
        Version 1.4.1
        SSL Support enabled

        Directive Local Value Master Value
        mongo.allow_empty_keys 0 0
        mongo.chunk_size 262144 262144
        mongo.cmd $ $
        mongo.default_host localhost localhost
        mongo.default_port 27017 27017
        mongo.is_master_interval 15 15
        mongo.long_as_object 0 0
        mongo.native_long 0 0
        mongo.ping_interval 5 5
        ************************************

        Show
        John Greenall
        added a comment - I just encountered this issue. As Jay states above, the problem only became apparent in latter stages of testing and it does feel like we only narrowly avoided disaster. The issue was fixed by explicitly enabling native_long. The reason I am reporting this here is that I am using version 1.4.1 so based on this thread would expect it to be on by default. Any suggestions as to why it hasn't been? output of phpinfo: *************************************** MongoDB Support enabled Version 1.4.1 SSL Support enabled Directive Local Value Master Value mongo.allow_empty_keys 0 0 mongo.chunk_size 262144 262144 mongo.cmd $ $ mongo.default_host localhost localhost mongo.default_port 27017 27017 mongo.is_master_interval 15 15 mongo.long_as_object 0 0 mongo.native_long 0 0 mongo.ping_interval 5 5 ************************************
        Hide
        Derick Rethans
        added a comment -

        John Greenall, so far, we didn't change this default as this breaks BC. But I think the time is ready to do this in driver version 1.5.0 which is on the way to have a release candidate.

        Show
        Derick Rethans
        added a comment - John Greenall , so far, we didn't change this default as this breaks BC. But I think the time is ready to do this in driver version 1.5.0 which is on the way to have a release candidate.
        Hide
        John Greenall
        added a comment -

        Derick Rethans, thanks for your response. Wasn't sure from this thread whether this had already been done and it was just something peculiar to my install.
        Best,
        John

        Show
        John Greenall
        added a comment - Derick Rethans , thanks for your response. Wasn't sure from this thread whether this had already been done and it was just something peculiar to my install. Best, John

          People

          • Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:
              Days since reply:
              14 weeks, 1 day ago
              Date of 1st Reply: