From 3a9ca873c7fc16558506b1af341c73eb8371c56e Mon Sep 17 00:00:00 2001 From: KotlinIsland Date: Thu, 9 Feb 2023 15:59:49 +1000 Subject: [PATCH 1/2] blackd: fix issue for mishandling single character input --- CHANGES.md | 2 ++ src/blackd/__init__.py | 3 ++- tests/test_blackd.py | 6 ++++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index c7ecc396214..1264294a828 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -41,6 +41,8 @@ +- fix a bug where a single character would cause a 500 (#3558) + ### Integrations diff --git a/src/blackd/__init__.py b/src/blackd/__init__.py index ba4750b8298..e478c276a5f 100644 --- a/src/blackd/__init__.py +++ b/src/blackd/__init__.py @@ -146,7 +146,8 @@ async def handle(request: web.Request, executor: Executor) -> web.Response: ) # Preserve CRLF line endings - if req_str[req_str.find("\n") - 1] == "\r": + nl = req_str.find("\n") + if nl > 0 and req_str[nl - 1] == "\r": formatted_str = formatted_str.replace("\n", "\r\n") # If, after swapping line endings, nothing changed, then say so if formatted_str == req_str: diff --git a/tests/test_blackd.py b/tests/test_blackd.py index 5b6461f7685..85408326f80 100644 --- a/tests/test_blackd.py +++ b/tests/test_blackd.py @@ -240,3 +240,9 @@ async def test_normalizes_line_endings(self) -> None: response = await self.client.post("/", data=data) self.assertEqual(await response.text(), expected) self.assertEqual(response.status, 200) + + @unittest_run_loop + async def test_single_character(self) -> None: + response = await self.client.post("/", data="1") + self.assertEqual(await response.text(), "1\n") + self.assertEqual(response.status, 200) From 3629df9f1d99e9597b51558d0628f8d9de313cab Mon Sep 17 00:00:00 2001 From: Shantanu <12621235+hauntsaninja@users.noreply.github.com> Date: Thu, 7 Sep 2023 00:01:45 -0700 Subject: [PATCH 2/2] Update CHANGES.md --- CHANGES.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index ad803537040..af2364d5eb3 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -43,7 +43,7 @@ -- fix a bug where a single character would cause a 500 (#3558) +- Fix an issue in `blackd` with single character input (#3558) ### Integrations