[Python-checkins] cpython (3.3): Issue #17812: Fixed quadratic complexity of base64.b32encode().

serhiy.storchaka python-checkins at python.org
Sun May 19 10:50:02 CEST 2013


http://hg.python.org/cpython/rev/4b5467d997f1
changeset:   83836:4b5467d997f1
branch:      3.3
parent:      83833:c430bea30457
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Sun May 19 11:41:15 2013 +0300
summary:
  Issue #17812: Fixed quadratic complexity of base64.b32encode().

files:
  Lib/base64.py |  12 ++++++------
  Misc/NEWS     |   2 ++
  2 files changed, 8 insertions(+), 6 deletions(-)


diff --git a/Lib/base64.py b/Lib/base64.py
--- a/Lib/base64.py
+++ b/Lib/base64.py
@@ -166,7 +166,7 @@
     if leftover:
         s = s + bytes(5 - leftover)  # Don't use += !
         quanta += 1
-    encoded = bytes()
+    encoded = bytearray()
     for i in range(quanta):
         # c1 and c2 are 16 bits wide, c3 is 8 bits wide.  The intent of this
         # code is to process the 40 bits in units of 5 bits.  So we take the 1
@@ -187,14 +187,14 @@
                           ])
     # Adjust for any leftover partial quanta
     if leftover == 1:
-        return encoded[:-6] + b'======'
+        encoded[-6:] = b'======'
     elif leftover == 2:
-        return encoded[:-4] + b'===='
+        encoded[-4:] = b'===='
     elif leftover == 3:
-        return encoded[:-3] + b'==='
+        encoded[-3:] = b'==='
     elif leftover == 4:
-        return encoded[:-1] + b'='
-    return encoded
+        encoded[-1:] = b'='
+    return bytes(encoded)
 
 
 def b32decode(s, casefold=False, map01=None):
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -24,6 +24,8 @@
 Library
 -------
 
+- Issue #17812: Fixed quadratic complexity of base64.b32encode().
+
 - Issue #17980: Fix possible abuse of ssl.match_hostname() for denial of
   service using certificates with many wildcards (CVE-2013-2099).
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list