From 7e0b5236af319edbf0ad4661440432a016f5055e Mon Sep 17 00:00:00 2001 From: Moritz Raabe Date: Fri, 19 Mar 2021 09:19:50 +0100 Subject: [PATCH] better deal with CRLF/LF issues --- capa/rules.py | 2 ++ scripts/capafmt.py | 2 ++ scripts/lint.py | 7 ++++++- 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/capa/rules.py b/capa/rules.py index b9a42056..aeb2754a 100644 --- a/capa/rules.py +++ b/capa/rules.py @@ -736,6 +736,8 @@ class Rule(object): # the below regex makes these adjustments and while ugly, we don't have to explore the ruamel.yaml insides doc = re.sub(r"!!int '0x-([0-9a-fA-F]+)'", r"-0x\1", doc) + # normalize CRLF to LF + doc = doc.replace("\r\n", "\n") return doc diff --git a/scripts/capafmt.py b/scripts/capafmt.py index a0b2a7c6..1f110074 100644 --- a/scripts/capafmt.py +++ b/scripts/capafmt.py @@ -65,6 +65,8 @@ def main(argv=None): return 0 else: logger.info("rule requires reformatting (%s)", rule.name) + if "\r\n" in rule.definition: + logger.info("please make sure that the file uses LF (\\n) line endings only") return 1 if args.in_place: diff --git a/scripts/lint.py b/scripts/lint.py index 26702c3f..0c2c8087 100644 --- a/scripts/lint.py +++ b/scripts/lint.py @@ -331,7 +331,12 @@ class FormatIncorrect(Lint): if actual != expected: diff = difflib.ndiff(actual.splitlines(1), expected.splitlines(True)) - self.recommendation = self.recommendation_template.format("".join(diff)) + recommendation_template = self.recommendation_template + if "\r\n" in actual: + recommendation_template = ( + self.recommendation_template + "\nplease make sure that the file uses LF (\\n) line endings only" + ) + self.recommendation = recommendation_template.format("".join(diff)) return True return False