beneltayar · GitHub

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

Read the original on github.com ↗