Skip to content

corner case: base64.b32decode ignorechars=b'=' miscounts padding #162353

Description

@gpshead

Bug report

>>> import base64 import *
>>> b32decode(b'======ME', ignorechars=b'=')
b'a'
>>> b64decode(b'=YQ=', ignorechars=b'=')
Traceback (most recent call last):
...
binascii.Error: Incorrect padding

The above b32decode call should raise an incorrect padding error.

binascii.a2b_base32 doesn't reset its pads counter after a data character. Ignored earlier '=' characters are counted towards the final padding check.

esoteric. hit in practice? i'd hope not. who would actually use = as padding? but b64decode handles this error case, b32 should as well. a fix is likely just:

--- a/Modules/binascii.c
+++ b/Modules/binascii.c
@@ -1773,6 +1773,7 @@
             }
             goto error;
         }
+        pads = 0;

         switch (octa_pos) {
             case 0:
--- a/Lib/test/test_binascii.py
+++ b/Lib/test/test_binascii.py
@@ -1255,6 +1255,14 @@
         eq(a2b_base32(b'MFRGGZD=F', ignorechars=b'='), b'abcde')
         eq(a2b_base32(b'MFRGGZDF=', ignorechars=b'='), b'abcde')
         eq(a2b_base32(b'MFRA======', ignorechars=b'='), b'ab')
+        # Ignored pad characters do not count as trailing padding.
+        eq(a2b_base32(b'=MF=RA====', ignorechars=b'='), b'ab')
+        for data in (b'======ME', b'=ME=====', b'M======E', b'ME==FR==',
+                     b'=MFRGG43'):
+            with self.subTest(data=data):
+                with self.assertRaisesRegex(binascii.Error,
+                                            'Incorrect padding'):
+                    a2b_base32(data, ignorechars=b'=')

         eq(a2b_base32(b'A1B3C5W7Z9', ignorechars=b'19WZ'), b'\x00v.\xdb\xf9')
         eq(a2b_base32(b'A1B3C5W7Z9', ignorechars=b'19WZ',

CPython versions tested on:

CPython main branch, 3.15

Linked PRs

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    3.15bugs and security fixes
    on Nov 21, 2026
  2. omkar-334 commented on Nov 21, 2026

    @omkar-334
    Contributor

    hey @gpshead id like to work on this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.15bugs and security fixeseasytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions