Skip to content

base64.b85decode thread safety issue #141141

@beneltayar

Description

@beneltayar

Bug report

Bug description:

The b85decode implementation starts like this:

def b85decode(b):
    """Decode the base85-encoded bytes-like object or ASCII string b

    The result is returned as a bytes object.
    """
    global _b85dec
    # Delay the initialization of tables to not waste memory
    # if the function is never called
    if _b85dec is None:
        _b85dec = [None] * 256
        for i, c in enumerate(_b85alphabet):
            _b85dec[c] = i
    ...  # decode logic

This can result in a thread safety issue if a thread switch happens after _b85dec = [None] * 256 but before _b85dec was fully populated
If that happens, and a different thread calls b85decode, it will proceed to the decode logic with an invalid value in the _b85dec global variable.
I was able to reproduce this issue locally on my machine with some mock trickery to make the thread sleep at the right time

CPython versions tested on:

CPython main branch, 3.11

Operating systems tested on:

Windows

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytopic-free-threadingtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions