[3.8] bpo-39068: Fix race condition in base64 (GH-17627) (GH-24022)

There was a race condition in base64 in lazy initialization of multiple globals.
(cherry picked from commit 9655434cca)

Co-authored-by: Brandon Stansbury <brandonrstansbury@gmail.com>
This commit is contained in:
Serhiy Storchaka 2021-01-01 19:41:49 +02:00 committed by GitHub
parent 187785e2fa
commit b863607d30
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 5 additions and 2 deletions

View File

@ -320,7 +320,7 @@ def a85encode(b, *, foldspaces=False, wrapcol=0, pad=False, adobe=False):
global _a85chars, _a85chars2 global _a85chars, _a85chars2
# Delay the initialization of tables to not waste memory # Delay the initialization of tables to not waste memory
# if the function is never called # if the function is never called
if _a85chars is None: if _a85chars2 is None:
_a85chars = [bytes((i,)) for i in range(33, 118)] _a85chars = [bytes((i,)) for i in range(33, 118)]
_a85chars2 = [(a + b) for a in _a85chars for b in _a85chars] _a85chars2 = [(a + b) for a in _a85chars for b in _a85chars]
@ -428,7 +428,7 @@ def b85encode(b, pad=False):
global _b85chars, _b85chars2 global _b85chars, _b85chars2
# Delay the initialization of tables to not waste memory # Delay the initialization of tables to not waste memory
# if the function is never called # if the function is never called
if _b85chars is None: if _b85chars2 is None:
_b85chars = [bytes((i,)) for i in _b85alphabet] _b85chars = [bytes((i,)) for i in _b85alphabet]
_b85chars2 = [(a + b) for a in _b85chars for b in _b85chars] _b85chars2 = [(a + b) for a in _b85chars for b in _b85chars]
return _85encode(b, _b85chars, _b85chars2, pad) return _85encode(b, _b85chars, _b85chars2, pad)

View File

@ -1607,6 +1607,7 @@ Tage Stabell-Kulo
Quentin Stafford-Fraser Quentin Stafford-Fraser
Frank Stajano Frank Stajano
Joel Stanley Joel Stanley
Brandon Stansbury
Anthony Starks Anthony Starks
David Steele David Steele
Oliver Steele Oliver Steele

View File

@ -0,0 +1,2 @@
Fix initialization race condition in :func:`a85encode` and :func:`b85encode`
in :mod:`base64`. Patch by Brandon Stansbury.