File tree Expand file tree Collapse file tree 2 files changed +23
-1
lines changed Expand file tree Collapse file tree 2 files changed +23
-1
lines changed Original file line number Diff line number Diff line change @@ -117,7 +117,7 @@ async def journal_logs_reader(
117117 continue
118118
119119 # strip \n for simple fields before decoding
120- entries [field_name ] = data [:- 1 ].decode ("utf-8" )
120+ entries [field_name ] = data [:- 1 ].decode ("utf-8" , errors = "replace" )
121121
122122
123123def _parse_boot_json (boot_json_bytes : bytes ) -> tuple [int , str ]:
Original file line number Diff line number Diff line change @@ -275,3 +275,25 @@ async def test_parsing_boots_none():
275275 boots .append ((index , boot_id ))
276276
277277 assert boots == []
278+
279+
280+ async def test_parsing_non_utf8_message ():
281+ """Test that non-UTF-8 bytes in message are replaced with replacement character."""
282+ journal_logs , stream = _journal_logs_mock ()
283+ # Include invalid UTF-8 sequence (0xff is not valid UTF-8)
284+ stream .feed_data (b"MESSAGE=Hello, \xff world!\n \n " )
285+ _ , line = await anext (journal_logs_reader (journal_logs ))
286+ assert line == "Hello, \ufffd world!"
287+
288+
289+ async def test_parsing_non_utf8_in_binary_message ():
290+ """Test that non-UTF-8 bytes in binary format message are replaced."""
291+ journal_logs , stream = _journal_logs_mock ()
292+ # Binary format with invalid UTF-8 sequence
293+ stream .feed_data (
294+ b"ID=1\n "
295+ b"MESSAGE\n \x0f \x00 \x00 \x00 \x00 \x00 \x00 \x00 Hello, \xff world!\n "
296+ b"AFTER=after\n \n "
297+ )
298+ _ , line = await anext (journal_logs_reader (journal_logs ))
299+ assert line == "Hello, \ufffd world!"
You can’t perform that action at this time.
0 commit comments