From ac19d1e427747f91f287d5b6cca4eeac3a232493 Mon Sep 17 00:00:00 2001 From: Julien Falque Date: Tue, 8 Sep 2020 09:53:54 +0200 Subject: [PATCH] octal-values: Prevent detection of 8 and 9 as octal values --- tests/rules/test_octal_values.py | 4 ++++ yamllint/rules/octal_values.py | 12 ++++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/tests/rules/test_octal_values.py b/tests/rules/test_octal_values.py index f2d9a2b..87bdbe3 100644 --- a/tests/rules/test_octal_values.py +++ b/tests/rules/test_octal_values.py @@ -50,6 +50,8 @@ class OctalValuesTestCase(RuleTestCase): ' - 0.10\n' ' - .01\n' ' - 0e3\n', conf) + self.check('with-decimal-digits: 012345678', conf) + self.check('with-decimal-digits: 012345679', conf) def test_explicit_octal_values(self): conf = ('octal-values:\n' @@ -74,3 +76,5 @@ class OctalValuesTestCase(RuleTestCase): ' - .01\n' ' - 0e3\n', conf) self.check('user-city: "010"', conf) + self.check('with-decimal-digits: 0o012345678', conf) + self.check('with-decimal-digits: 0o012345679', conf) diff --git a/yamllint/rules/octal_values.py b/yamllint/rules/octal_values.py index faa38aa..c9a5574 100644 --- a/yamllint/rules/octal_values.py +++ b/yamllint/rules/octal_values.py @@ -71,6 +71,8 @@ converted to ``8``. city-code: 0o10 """ +import re + import yaml from yamllint.linter import LintProblem @@ -84,6 +86,10 @@ DEFAULT = {'forbid-implicit-octal': True, 'forbid-explicit-octal': True} +def _is_octal_number(string): + return re.match(r'^[0-7]+$', string) is not None + + def check(conf, token, prev, next, nextnext, context): if prev and isinstance(prev, yaml.tokens.TagToken): return @@ -92,7 +98,8 @@ def check(conf, token, prev, next, nextnext, context): if isinstance(token, yaml.tokens.ScalarToken): if not token.style: val = token.value - if val.isdigit() and len(val) > 1 and val[0] == '0': + if (val.isdigit() and len(val) > 1 and val[0] == '0' and + _is_octal_number(val[1:])): yield LintProblem( token.start_mark.line + 1, token.end_mark.column + 1, 'forbidden implicit octal value "%s"' % @@ -102,7 +109,8 @@ def check(conf, token, prev, next, nextnext, context): if isinstance(token, yaml.tokens.ScalarToken): if not token.style: val = token.value - if len(val) > 2 and val[:2] == '0o' and val[2:].isdigit(): + if (len(val) > 2 and val[:2] == '0o' and + _is_octal_number(val[2:])): yield LintProblem( token.start_mark.line + 1, token.end_mark.column + 1, 'forbidden explicit octal value "%s"' %