Skip to content

Commit 3571f57

Browse files
authored
fix: scientific notation circumvented bounds check (#110)
1 parent c9bd79c commit 3571f57

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,9 @@ function parse (args, opts) {
425425

426426
function maybeCoerceNumber (key, value) {
427427
if (!checkAllAliases(key, flags.strings) && !checkAllAliases(key, flags.coercions)) {
428-
const shouldCoerceNumber = isNumber(value) && configuration['parse-numbers'] && (Number.isSafeInteger(parseInt(value)))
428+
const shouldCoerceNumber = isNumber(value) && configuration['parse-numbers'] && (
429+
Number.isSafeInteger(Math.floor(value))
430+
)
429431
if (shouldCoerceNumber || (!isUndefined(value) && checkAllAliases(key, flags.numbers))) value = Number(value)
430432
}
431433
return value

test/yargs-parser.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2479,6 +2479,11 @@ describe('yargs-parser', function () {
24792479
argv.foo.should.equal('93940495950949399948393')
24802480
})
24812481

2482+
it('does not magically convert scientific notation larger than Number.MAX_SAFE_INTEGER', () => {
2483+
const argv = parser([ '--foo', '33e99999' ])
2484+
argv.foo.should.equal('33e99999')
2485+
})
2486+
24822487
it('converts numeric options larger than Number.MAX_SAFE_INTEGER to number', () => {
24832488
const argv = parser([ '--foo', '93940495950949399948393' ], {
24842489
number: ['foo']

0 commit comments

Comments
 (0)