From 8daa64a2e10907539094df51f4c51306bb426f07 Mon Sep 17 00:00:00 2001 From: KotlinIsland <65446343+KotlinIsland@users.noreply.github.com> Date: Thu, 7 Sep 2023 17:11:50 +1000 Subject: [PATCH] blackd: fix mishandling of single character input (#3558) --- 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 2168c1b..af9fc49 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -45,6 +45,8 @@ +- Fix an issue in `blackd` with single character input (#3558) + ### Integrations diff --git a/src/blackd/__init__.py b/src/blackd/__init__.py index 4f2d87d..6b0f3d3 100644 --- a/src/blackd/__init__.py +++ b/src/blackd/__init__.py @@ -152,7 +152,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 325bd7d..dd2126e 100644 --- a/tests/test_blackd.py +++ b/tests/test_blackd.py @@ -240,3 +240,9 @@ class BlackDTestCase(AioHTTPTestCase): # type: ignore[misc] 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) -- 2.39.5