octal-values: Prevent detection of 8 and 9 as octal values

This commit is contained in:
Julien Falque 2020-09-08 09:53:54 +02:00 committed by GitHub
parent 597e88bb7b
commit ac19d1e427
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 2 deletions

View File

@ -50,6 +50,8 @@ class OctalValuesTestCase(RuleTestCase):
' - 0.10\n' ' - 0.10\n'
' - .01\n' ' - .01\n'
' - 0e3\n', conf) ' - 0e3\n', conf)
self.check('with-decimal-digits: 012345678', conf)
self.check('with-decimal-digits: 012345679', conf)
def test_explicit_octal_values(self): def test_explicit_octal_values(self):
conf = ('octal-values:\n' conf = ('octal-values:\n'
@ -74,3 +76,5 @@ class OctalValuesTestCase(RuleTestCase):
' - .01\n' ' - .01\n'
' - 0e3\n', conf) ' - 0e3\n', conf)
self.check('user-city: "010"', conf) self.check('user-city: "010"', conf)
self.check('with-decimal-digits: 0o012345678', conf)
self.check('with-decimal-digits: 0o012345679', conf)

View File

@ -71,6 +71,8 @@ converted to ``8``.
city-code: 0o10 city-code: 0o10
""" """
import re
import yaml import yaml
from yamllint.linter import LintProblem from yamllint.linter import LintProblem
@ -84,6 +86,10 @@ DEFAULT = {'forbid-implicit-octal': True,
'forbid-explicit-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): def check(conf, token, prev, next, nextnext, context):
if prev and isinstance(prev, yaml.tokens.TagToken): if prev and isinstance(prev, yaml.tokens.TagToken):
return return
@ -92,7 +98,8 @@ def check(conf, token, prev, next, nextnext, context):
if isinstance(token, yaml.tokens.ScalarToken): if isinstance(token, yaml.tokens.ScalarToken):
if not token.style: if not token.style:
val = token.value 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( yield LintProblem(
token.start_mark.line + 1, token.end_mark.column + 1, token.start_mark.line + 1, token.end_mark.column + 1,
'forbidden implicit octal value "%s"' % 'forbidden implicit octal value "%s"' %
@ -102,7 +109,8 @@ def check(conf, token, prev, next, nextnext, context):
if isinstance(token, yaml.tokens.ScalarToken): if isinstance(token, yaml.tokens.ScalarToken):
if not token.style: if not token.style:
val = token.value 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( yield LintProblem(
token.start_mark.line + 1, token.end_mark.column + 1, token.start_mark.line + 1, token.end_mark.column + 1,
'forbidden explicit octal value "%s"' % 'forbidden explicit octal value "%s"' %