Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork34.3k
gh-145968: fix base64.b64decode altchars translation in specific cases#145969
gh-145968: fix base64.b64decode altchars translation in specific cases#145969serhiy-storchaka merged 3 commits intopython:mainfrom
Conversation
…c casesWhen `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-botbot commentedMar 15, 2026 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
mayeut commentedMar 15, 2026
Lib/base64.py Outdated
| 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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
It can be simply:
| 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'+/'))) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
serhiy-storchaka left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
LGTM. 👍
ec5e3a5 intopython:mainUh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
When
altcharsoverlaps with the standard ones, the translation does not always yield to the expected outcome.This updates
bytes.maketransarguments to take those overlap cases into account.