Skip to content

Commit

Permalink
fix: scientific notation circumvented bounds check (yargs#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoe committed Dec 20, 2017
1 parent c9bd79c commit 3571f57
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
4 changes: 3 additions & 1 deletion index.js
Expand Up @@ -425,7 +425,9 @@ function parse (args, opts) {

function maybeCoerceNumber (key, value) {
if (!checkAllAliases(key, flags.strings) && !checkAllAliases(key, flags.coercions)) {
const shouldCoerceNumber = isNumber(value) && configuration['parse-numbers'] && (Number.isSafeInteger(parseInt(value)))
const shouldCoerceNumber = isNumber(value) && configuration['parse-numbers'] && (
Number.isSafeInteger(Math.floor(value))
)
if (shouldCoerceNumber || (!isUndefined(value) && checkAllAliases(key, flags.numbers))) value = Number(value)
}
return value
Expand Down
5 changes: 5 additions & 0 deletions test/yargs-parser.js
Expand Up @@ -2479,6 +2479,11 @@ describe('yargs-parser', function () {
argv.foo.should.equal('93940495950949399948393')
})

it('does not magically convert scientific notation larger than Number.MAX_SAFE_INTEGER', () => {
const argv = parser([ '--foo', '33e99999' ])
argv.foo.should.equal('33e99999')
})

it('converts numeric options larger than Number.MAX_SAFE_INTEGER to number', () => {
const argv = parser([ '--foo', '93940495950949399948393' ], {
number: ['foo']
Expand Down

0 comments on commit 3571f57

Please sign in to comment.