Skip to content

fix(NODE-6764): incorrect negative bigint handling #752

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@
"build:bundle": "rollup -c rollup.config.mjs",
"build": "npm run build:dts && npm run build:bundle",
"check:lint": "ESLINT_USE_FLAT_CONFIG=false eslint -v && ESLINT_USE_FLAT_CONFIG=false eslint --ext '.js,.ts' --max-warnings=0 src test && npm run build:dts && npm run check:tsd",
"format": "eslint --ext '.js,.ts' src test --fix",
"format": "ESLINT_USE_FLAT_CONFIG=false eslint --ext '.js,.ts' src test --fix",
"check:coverage": "nyc --check-coverage npm run check:node",
"prepare": "node etc/prepare.js",
"release": "standard-version -i HISTORY.md"
Expand Down
6 changes: 4 additions & 2 deletions src/utils/number_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,12 @@ export const NumberUtils: NumberUtils = {
const hi = NumberUtils.getUint32LE(source, offset + 4);

/*
eslint-disable-next-line no-restricted-globals
eslint-disable no-restricted-globals
-- This is allowed since this helper should not be called unless bigint features are enabled
*/
return (BigInt(hi) << BigInt(32)) + BigInt(lo);
const intermediate = (BigInt(hi) << BigInt(32)) + BigInt(lo);
return BigInt.asIntN(64, intermediate);
/* eslint-enable no-restricted-globals */
},

/** Reads a little-endian 64-bit float from source */
Expand Down
33 changes: 33 additions & 0 deletions test/node/bigint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,39 @@ describe('BSON BigInt support', function () {

it(description, test);
}

describe('edge case tests', function () {
Copy link
Member

@durran durran Feb 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a little nit. Instead of just calling these edge case tests can we better describe what we are testing here? I'd personally prefer individual it blocks with specific descriptions than the generic describe and loop.

const tests = [
{
expectedResult: { a: -(2n ** 63n) },
input: Buffer.from('10000000126100000000000000008000', 'hex')
},
{
expectedResult: { a: -1n },
input: Buffer.from('10000000126100FFFFFFFFFFFFFFFF00', 'hex')
},
{
expectedResult: { a: 0n },
input: Buffer.from('10000000126100000000000000000000', 'hex')
},
{
expectedResult: { a: 1n },
input: Buffer.from('10000000126100010000000000000000', 'hex')
},
{
expectedResult: { a: 2n ** 63n - 1n },
input: Buffer.from('10000000126100FFFFFFFFFFFFFF7F00', 'hex')
}
];

for (const test of tests) {
it(`correctly deserializes the bson document encoded in ${test.input.toString('hex')}`, function () {
expect(BSON.deserialize(test.input, { useBigInt64: true })).to.deep.equal(
test.expectedResult
);
});
}
});
});

describe('BSON.serialize()', function () {
Expand Down
Loading