Skip to content

Fix crash in logging checker on a non-UTF-8 bytes format string#11111

Open
sarathfrancis90 wants to merge 1 commit into
pylint-dev:mainfrom
sarathfrancis90:fix-logging-bytes-format-string-crash
Open

Fix crash in logging checker on a non-UTF-8 bytes format string#11111
sarathfrancis90 wants to merge 1 commit into
pylint-dev:mainfrom
sarathfrancis90:fix-logging-bytes-format-string-crash

Conversation

@sarathfrancis90

Copy link
Copy Markdown

Type of Changes

Type
🐛 Bug fix

Description

The logging checker crashed with a fatal F0002 error on a logging call whose format string is bytes that isn't valid UTF-8, e.g.:

import logging

logging.critical(b"\xc0\xc0")

_check_format_string ran format_string.decode(), which raises UnicodeDecodeError on non-UTF-8 bytes.

At runtime logging applies str() to the message before interpolation (str(b"\xc0\xc0") -> "b'\\xc0\\xc0'"), so I switched the bytes handling to str(format_string). That never raises and matches the string logging actually formats, so argument-count checks on valid bytes format strings still work.

Testing: added a functional test (logging_bytes_format_string) covering the crashing case plus valid bytes format strings with too-few/too-many args. It crashes with astroid-error before the fix and passes after. All logging functional tests pass, and ruff/black/isort are clean.

Closes #10813

The logging checker called ``bytes.decode()`` on a bytes format string,
which raises ``UnicodeDecodeError`` (and crashes pylint with a fatal
error) when the bytes are not valid UTF-8, e.g. ``logging.critical(b"\xc0\xc0")``.

At runtime ``logging`` applies ``str()`` to the message before
interpolation, so use ``str()`` here as well. This never raises and
matches the string that ``logging`` actually formats.

Closes pylint-dev#10813
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Crash 💥 A bug that makes pylint crash

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improper handling of log message format bytestrings

2 participants