1 # This Source Code Form is subject to the terms of the Mozilla Public
2 # License, v. 2.0. If a copy of the MPL was not distributed with this
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 from mozlint
import result
8 from mozlint
.pathutils
import expand_exclusions
10 # Code inspired by Red Hat
11 # https://github.com/siddhesh/find-unicode-control/
12 # published under the 'BSD 3-Clause' license
13 # https://access.redhat.com/security/vulnerabilities/RHSB-2021-007
18 chr(c
) for c
in range(sys
.maxunicode
) if unicodedata
.category(chr(c
)) == "Cf"
22 def getfiletext(config
, filename
):
23 # Make a text string from a file, attempting to decode from latin1 if necessary.
24 # Other non-utf-8 locales are not supported at the moment.
25 with
open(filename
, "rb") as infile
:
27 return infile
.read().decode("utf-8")
28 except Exception as e
:
31 "message": "Could not open file as utf-8 - maybe an encoding error: %s"
35 results
.append(result
.from_config(config
, **res
))
41 def analyze_text(filename
, text
, disallowed
):
43 for t
in text
.splitlines():
45 subset
= [c
for c
in t
if chr(ord(c
)) in disallowed
]
52 def lint(paths
, config
, **lintargs
):
53 files
= list(expand_exclusions(paths
, config
, lintargs
["root"]))
55 text
= getfiletext(config
, f
)
57 (subset
, line
) = analyze_text(f
, text
, disallowed
)
62 "message": "disallowed characters: %s" % subset
,
65 results
.append(result
.from_config(config
, **res
))
67 return {"results": results
, "fixed": 0}