Skip to content

Commit

Permalink
fix: remove support for underscore (_) in number parsing (fixes nod…
Browse files Browse the repository at this point in the history
  • Loading branch information
david-mohr committed Mar 8, 2022
1 parent 49baadd commit 9a1726e
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 30 deletions.
6 changes: 3 additions & 3 deletions lib/type/float.js
Expand Up @@ -5,10 +5,10 @@ var Type = require('../type');

var YAML_FLOAT_PATTERN = new RegExp(
// 2.5e4, 2.5 and integers
'^(?:[-+]?(?:[0-9][0-9_]*)(?:\\.[0-9_]*)?(?:[eE][-+]?[0-9]+)?' +
'^(?:[-+]?(?:[0-9][0-9]*)(?:\\.[0-9]*)?(?:[eE][-+]?[0-9]+)?' +
// .2e4, .2
// special case, seems not from spec
'|\\.[0-9_]+(?:[eE][-+]?[0-9]+)?' +
'|\\.[0-9]+(?:[eE][-+]?[0-9]+)?' +
// .inf
'|[-+]?\\.(?:inf|Inf|INF)' +
// .nan
Expand All @@ -30,7 +30,7 @@ function resolveYamlFloat(data) {
function constructYamlFloat(data) {
var value, sign;

value = data.replace(/_/g, '').toLowerCase();
value = data.toLowerCase();
sign = value[0] === '-' ? -1 : 1;

if ('+-'.indexOf(value[0]) >= 0) {
Expand Down
24 changes: 5 additions & 19 deletions lib/type/int.js
Expand Up @@ -47,11 +47,10 @@ function resolveYamlInteger(data) {

for (; index < max; index++) {
ch = data[index];
if (ch === '_') continue;
if (ch !== '0' && ch !== '1') return false;
hasDigits = true;
}
return hasDigits && ch !== '_';
return hasDigits;
}


Expand All @@ -61,55 +60,42 @@ function resolveYamlInteger(data) {

for (; index < max; index++) {
ch = data[index];
if (ch === '_') continue;
if (!isHexCode(data.charCodeAt(index))) return false;
hasDigits = true;
}
return hasDigits && ch !== '_';
return hasDigits;
}


if (ch === 'o') {
// base 8
index++;

for (; index < max; index++) {
ch = data[index];
if (ch === '_') continue;
if (!isOctCode(data.charCodeAt(index))) return false;
hasDigits = true;
}
return hasDigits && ch !== '_';
return hasDigits;
}
}

// base 10 (except 0)

// value should not start with `_`;
if (ch === '_') return false;

for (; index < max; index++) {
ch = data[index];
if (ch === '_') continue;
if (!isDecCode(data.charCodeAt(index))) {
return false;
}
hasDigits = true;
}

// Should have digits and should not end with `_`
if (!hasDigits || ch === '_') return false;

return true;
// Should have digits
return hasDigits;
}

function constructYamlInteger(data) {
var value = data, sign = 1, ch;

if (value.indexOf('_') !== -1) {
value = value.replace(/_/g, '');
}

ch = value[0];

if (ch === '-' || ch === '+') {
Expand Down
7 changes: 7 additions & 0 deletions test/issues/0027.js
Expand Up @@ -28,6 +28,13 @@ describe('Should load numbers in YAML 1.2 format', function () {
// not valid octal
assert.strictEqual(yaml.load('0o1289'), '0o1289');
});

it('should not allow underscore', function () {
// previously parsed as int
assert.strictEqual(yaml.load('1_23'), '1_23');
// previously parsed as float
assert.strictEqual(yaml.load('1_23.45'), '1_23.45');
});
});


Expand Down
6 changes: 3 additions & 3 deletions test/issues/0614.js
Expand Up @@ -34,9 +34,9 @@ it('Should allow int override', function () {
const SCHEMA = yaml.DEFAULT_SCHEMA.extend({ implicit: [ BigIntType ] });

const data = `
int: -123_456_789
bigint: -12_345_678_901_234_567_890
float: -12_345_678_901_234_567_890.1234
int: -123456789
bigint: -12345678901234567890
float: -12345678901234567890.1234
`;

assert.deepStrictEqual(yaml.load(data, { schema: SCHEMA }), {
Expand Down
4 changes: 2 additions & 2 deletions test/samples-common/construct-float.yml
@@ -1,5 +1,5 @@
canonical: 6.8523015e+5
exponential: 685.230_15e+03
fixed: 685_230.15
exponential: 685.23015e+03
fixed: 685230.15
negative infinity: -.inf
not a number: .NaN
6 changes: 3 additions & 3 deletions test/samples-common/construct-int.yml
@@ -1,5 +1,5 @@
canonical: 685230
decimal: +685_230
decimal: +685230
octal: 0o2472256
hexadecimal: 0x_0A_74_AE
binary: 0b1010_0111_0100_1010_1110
hexadecimal: 0x0A74AE
binary: 0b10100111010010101110

0 comments on commit 9a1726e

Please sign in to comment.