Skip to content
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

fix: remove support for underscore (_) in number parsing (fixes #627) #629

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 8 additions & 22 deletions dist/js-yaml.js
Original file line number Diff line number Diff line change
Expand Up @@ -536,11 +536,10 @@

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 @@ -550,55 +549,42 @@

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 Expand Up @@ -646,10 +632,10 @@

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 @@ -671,7 +657,7 @@
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
2 changes: 1 addition & 1 deletion dist/js-yaml.min.js

Large diffs are not rendered by default.

30 changes: 8 additions & 22 deletions dist/js-yaml.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -530,11 +530,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 @@ -544,55 +543,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 Expand Up @@ -640,10 +626,10 @@ var int = new type('tag:yaml.org,2002:int', {

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 @@ -665,7 +651,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
6 changes: 3 additions & 3 deletions lib/type/float.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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