summaryrefslogtreecommitdiff
path: root/jstests/core/numberlong.js
diff options
context:
space:
mode:
authorMatt Cotter <matt.cotter@mongodb.com>2016-08-30 14:43:31 -0400
committerMatt Cotter <matt.cotter@mongodb.com>2016-09-01 16:44:57 -0400
commitebf5539dac43ccc2289fabdd943ff6479a8d3920 (patch)
tree59850026339c54de7a04eb0d11b29bfbfc0a2513 /jstests/core/numberlong.js
parentbb6ae8c4cbde96aeb1a8681a4af8ccd8afee43f4 (diff)
downloadmongo-ebf5539dac43ccc2289fabdd943ff6479a8d3920.tar.gz
SERVER-25827 fix UB related to NumberLong(NaN) in shell
Diffstat (limited to 'jstests/core/numberlong.js')
-rw-r--r--jstests/core/numberlong.js56
1 files changed, 54 insertions, 2 deletions
diff --git a/jstests/core/numberlong.js b/jstests/core/numberlong.js
index 884a301440e..4c3ce6a6455 100644
--- a/jstests/core/numberlong.js
+++ b/jstests/core/numberlong.js
@@ -33,13 +33,47 @@ assert.eq.automsg("4294967295", "n - 1");
assert.eq.automsg("'NumberLong(\"4294967296\")'", "n.toString()");
assert.eq.automsg("'NumberLong(\"4294967296\")'", "tojson( n )");
assert.eq.automsg("4294967296", "n.floatApprox");
-assert.eq.automsg("", "n.top");
-assert.eq.automsg("", "n.bottom");
+assert.eq.automsg("1", "n.top");
+assert.eq.automsg("0", "n.bottom");
a = {};
a.a = n;
p = tojson(a);
assert.eq.automsg("'{ \"a\" : NumberLong(\"4294967296\") }'", "p");
+var goodValues = [
+ 0,
+ "9223372036854775807", // int64_t max
+ "-9223372036854775808", // int64_t min
+ -9223372036854776000,
+ 9223372036854775000,
+];
+var badNum = "number passed to NumberLong must be representable as an int64_t";
+var badStr = "could not convert string to long long";
+var badValues = [
+ {val: NaN, msg: badNum},
+ {val: "9223372036854775808", msg: badStr}, // int64_t max + 1
+ {val: 9223372036854776000, msg: badNum},
+ {val: "-9223372036854775809", msg: badStr}, // int64_t min - 1
+ {val: -9223372036854778000, msg: badNum},
+];
+
+for (var i = 0; i < goodValues.length; i++) {
+ try {
+ NumberLong(goodValues[i]);
+ } catch (e) {
+ doassert("Error: NumberLong(" + goodValues[i] + ") should have worked, but got '" +
+ e.message + "'.");
+ }
+}
+for (var i = 0; i < badValues.length; i++) {
+ try {
+ NumberLong(badValues[i].val);
+ doassert("Error: NumberLong(" + badValues[i] + ") should have failed.");
+ } catch (e) {
+ assert.eq(e.message, badValues[i].msg);
+ }
+}
+
// too big to fit in double
n = new NumberLong("11111111111111111");
assert.eq.automsg("11111111111111112", "n.toNumber()");
@@ -73,11 +107,29 @@ assert.eq.automsg("9223372036854775807", "n.floatApprox");
assert.eq.automsg("2147483647", "n.top");
assert.eq.automsg("4294967295", "n.bottom");
+// From top and bottom
n = new NumberLong(9223372036854775807, 2147483647, 4294967295);
assert.eq.automsg("9223372036854775807", "n.floatApprox");
assert.eq.automsg("2147483647", "n.top");
assert.eq.automsg("4294967295", "n.bottom");
+n = new NumberLong(0, 1, 0); // Test that floatApprox argument is ignored.
+assert.eq.automsg("4294967296", "n.floatApprox");
+assert.eq.automsg("1", "n.top");
+assert.eq.automsg("0", "n.bottom");
+
+badValues = [
+ [0, 4294967296, 0],
+ [0, 0, 4294967296],
+ ['asdf', 0, 0],
+ [0, 1.5, 0],
+];
+for (var i = 0; i < badValues.length; i++) {
+ assert.throws(function() {
+ NumberLong.apply(null, badValues[i]);
+ }, [], "Bad arguments to NumberLong should have thrown: " + JSON.stringify(badValues[i]));
+}
+
// parsing
assert.throws.automsg(function() {
new NumberLong("");