Skip to content

Unnecessary substring in UTF-7 BOM list #716

@PalmtopTiger

Description

@PalmtopTiger

Hi. While analyzing the code, I discovered an unnecessary line in UTF-7 BOM substring list: b"\x2b\x2f\x76\x38\x2d"

ENCODING_MARKS: dict[str, bytes | list[bytes]] = {
	...
    "utf_7": [
        b"\x2b\x2f\x76\x38",
        b"\x2b\x2f\x76\x39",
        b"\x2b\x2f\x76\x2b",
        b"\x2b\x2f\x76\x2f",
        b"\x2b\x2f\x76\x38\x2d", # < this line
    ],
    ...
}

If you look at the code here

for mark in marks:
    if sequence.startswith(mark):
        return iana_encoding, mark

You can see that this list is processed in a loop, with the function exiting on the first startswith match. This means that if the first element of the list, b"\x2b\x2f\x76\x38", matches, the loop doesn't reach the last one, b"\x2b\x2f\x76\x38\x2d". If the first doesn't match, the last one doesn't match either, since it starts with the same four bytes. Therefore, the fifth substring does nothing but add an additional delay during execution.

Also, I don't understand where the fifth substring of five bytes came from. Wikipedia only mentions four-byte substrings:

UTF-7: 2B 2F 76
...
Followed by 38, 39, 2B, or 2F (ASCII 8, 9, + or /), depending on what the next character is.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions