Skip to content

gh-145968: fix base64.b64decode altchars translation in specific cases - #145969

Merged
serhiy-storchaka merged 3 commits into
python:mainfrom
mayeut:base64-bad-translation
Mar 15, 2026
Merged

gh-145968: fix base64.b64decode altchars translation in specific cases#145969
serhiy-storchaka merged 3 commits into
python:mainfrom
mayeut:base64-bad-translation

Conversation

@mayeut

@mayeutmayeut commented Mar 15, 2026

Copy link
Copy Markdown
Contributor

When altchars overlaps with the standard ones, the translation does not always yield to the expected outcome.
This updates bytes.maketrans arguments to take those overlap cases into account.

…c cases
When `altchars` overlaps with the standard ones, the translation does not always yield to the expected outcome.
This commit updates `bytes.maketrans` arguments to take those overlap cases into account.
@python-cla-bot

python-cla-botBot commented Mar 15, 2026

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@mayeut

Copy link
Copy Markdown
ContributorAuthor

cc @serhiy-storchaka

Comment threadLib/base64.py Outdated
Comment on lines +103 to +113
altchars_out = (
altchars[0] if altchars[0] not in b'+/' else altchars[1],
altchars[1] if altchars[1] not in b'+/' else altchars[0],
)
trans_in = bytearray(altchars)
trans_out = bytearray(b'+/')
for b, b_out in zip(b'+/', altchars_out):
if b not in altchars:
trans_in.append(b)
trans_out.append(b_out)
trans = bytes.maketrans(trans_in, trans_out)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can be simply:

Suggested change
altchars_out= (
altchars[0] ifaltchars[0] notinb'+/'elsealtchars[1],
altchars[1] ifaltchars[1] notinb'+/'elsealtchars[0],
)
trans_in=bytearray(altchars)
trans_out=bytearray(b'+/')
forb, b_outinzip(b'+/', altchars_out):
ifbnotinaltchars:
trans_in.append(b)
trans_out.append(b_out)
trans=bytes.maketrans(trans_in, trans_out)
trans=bytes.maketrans(altchars+bytes(set(b'+/') -set(altchars)),
b'+/'+bytes(set(altchars) -set(b'+/')))

@mayeutmayeutMar 15, 2026

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I commited the suggestion but after further reflection, the sets are unordered and thus the translation might not be correct when altchars and standard ones do not overlap (although this might be hard to catch in tests ?).
I'll fix that later

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. What about the following?

trans_in=altcharstrans_out=b'+/'ifb'+'notinaltcharsandb'-'notinaltchars:
trans_in+=b'+/'trans_out+=altcharselifb'+'notinaltcharsorb'-'notinaltchars:
trans_in+=bytes(set(b'+/') -set(altchars))
trans_out+=bytes(set(altchars) -set(b'+/'))

You can also return your original code. #145981 also fixes this issue, so this code can be replaced, but your tests will remain.

mayeutand others added 2 commits March 15, 2026 11:36

@serhiy-storchakaserhiy-storchaka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. 👍

@serhiy-storchaka
serhiy-storchaka merged commit ec5e3a5 into python:mainMar 15, 2026
51 checks passed
@mayeut
mayeut deleted the base64-bad-translation branch March 29, 2026 17:21
ljfp pushed a commit to ljfp/cpython that referenced this pull request Apr 25, 2026
…c cases (pythonGH-145969)
When altchars overlaps with the standard ones, the translation does not
always yield to the expected outcome.
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@mayeut@serhiy-storchaka