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

HexData(0, "000000000000000000000005") will throw

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Minor - P4 Minor - P4
    • 2.4.5, 2.5.1
    • Affects Version/s: 2.4.1
    • Component/s: JavaScript
    • Labels:
      None
    • Environment:
      AWS m2.2xlarge (AMI: ami-8e27adbe) Production Amazon Linux. RAID10 8 drives on provisioned IOPS data partition. logs partition separate from data
    • Fully Compatible
    • ALL
    • Hide
      mongos> var hid = db.User.findOne({Fn:"Huy"})._id
      mongos> hid
      BinData(0,"AAAAAAAAAAAAAAAF")
      mongos> hid.hex()
      000000000000000000000005
      mongos> hid.subtype()
      0
      mongos> parseInt(hid.hex(),16)
      5
      mongos>
      

      //So then the following should work!

      mongos> HexData(hid.subtype(),hid.hex())
      Tue May  7 08:35:34.387   Assertion failure false src/mongo/util/hex.h 34
      0x747301 0x70ff5d 0x7024b0 0x702600 0x6e9fc0 0x82cac2 0x7413a06362
       mongo(_ZN5mongo15printStackTraceERSo+0x21) [0x747301]
       mongo(_ZN5mongo12verifyFailedEPKcS1_j+0xfd) [0x70ff5d]
       mongo() [0x7024b0]
       mongo(_ZN5mongo11hexDataInitEPNS_7V8ScopeERKN2v89ArgumentsE+0xc0) [0x702600]
       mongo(_ZN5mongo7V8Scope10v8CallbackERKN2v89ArgumentsE+0xb0) [0x6e9fc0]
       mongo() [0x82cac2]
       [0x7413a06362]
      Tue May  7 08:35:34.391 JavaScript execution failed: Error: assertion src/mongo/util/hex.h:34
      mongos>
      

      //But it throws...

      Show
      mongos> var hid = db.User.findOne({Fn: "Huy" })._id mongos> hid BinData(0, "AAAAAAAAAAAAAAAF" ) mongos> hid.hex() 000000000000000000000005 mongos> hid.subtype() 0 mongos> parseInt(hid.hex(),16) 5 mongos> //So then the following should work! mongos> HexData(hid.subtype(),hid.hex()) Tue May 7 08:35:34.387 Assertion failure false src/mongo/util/hex.h 34 0x747301 0x70ff5d 0x7024b0 0x702600 0x6e9fc0 0x82cac2 0x7413a06362 mongo(_ZN5mongo15printStackTraceERSo+0x21) [0x747301] mongo(_ZN5mongo12verifyFailedEPKcS1_j+0xfd) [0x70ff5d] mongo() [0x7024b0] mongo(_ZN5mongo11hexDataInitEPNS_7V8ScopeERKN2v89ArgumentsE+0xc0) [0x702600] mongo(_ZN5mongo7V8Scope10v8CallbackERKN2v89ArgumentsE+0xb0) [0x6e9fc0] mongo() [0x82cac2] [0x7413a06362] Tue May 7 08:35:34.391 JavaScript execution failed: Error: assertion src/mongo/util/hex.h:34 mongos> //But it throws...

      Our .net client stores a long id of 5. Before saving the id however, we convert it to big endian byte[] of 12 in length (to be the same size of mongo object id) for future proofing. This however should not matter as the following is clearly self explanatory.

      See "steps to reproduce"

      I actually use these JS functions as a work-around which allows me to call:

      mongos> BinData(0, hexToBase64(pad((5).toString(16), 24, 0)))
      BinData(0,"AAAAAAAAAAAAAAAF")
      
      //custom JS function definition begins:
      
      function pad(n, width, z) {
        z = z || '0';
        n = n + '';
        return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
      }
      
        function btoa(bin) {
      	var tableStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
      	var table = tableStr.split("");
          for (var i = 0, j = 0, len = bin.length / 3, base64 = []; i < len; ++i) {
            var a = bin.charCodeAt(j++), b = bin.charCodeAt(j++), c = bin.charCodeAt(j++);
            if ((a | b | c) > 255) throw new Error("String contains an invalid character");
            base64[base64.length] = table[a >> 2] + table[((a << 4) & 63) | (b >> 4)] +
                                    (isNaN(b) ? "=" : table[((b << 2) & 63) | (c >> 6)]) +
                                    (isNaN(b + c) ? "=" : table[c & 63]);
          }
          return base64.join("");
        };
      
      function hexToBase64(str) {
        return btoa(String.fromCharCode.apply(null,
          str.replace(/\r|\n/g, "").replace(/([\da-fA-F]{2}) ?/g, "0x$1 ").replace(/ +$/, "").split(" "))
        );
      }
      

            Assignee:
            tad Tad Marshall
            Reporter:
            huy@overnear.com Huy Nguyen
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: