[pypy-svn] pypy default: Fix the malloc/free pair: if we hit "unknown hash function", then

arigo commits-noreply at bitbucket.org
Sun Mar 13 11:50:01 CET 2011


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r42548:ab70ac82034f
Date: 2011-03-13 06:42 -0400
http://bitbucket.org/pypy/pypy/changeset/ab70ac82034f/

Log:	Fix the malloc/free pair: if we hit "unknown hash function", then
	self.ctx is left in an uninitialized state, which will crash
	EVP_MD_CTX_cleanup().

diff --git a/pypy/module/_hashlib/interp_hashlib.py b/pypy/module/_hashlib/interp_hashlib.py
--- a/pypy/module/_hashlib/interp_hashlib.py
+++ b/pypy/module/_hashlib/interp_hashlib.py
@@ -13,9 +13,10 @@
 algorithms = ('md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512')
 
 class W_Hash(Wrappable):
+    ctx = lltype.nullptr(ropenssl.EVP_MD_CTX.TO)
+
     def __init__(self, space, name):
         self.name = name
-        self.ctx = lltype.malloc(ropenssl.EVP_MD_CTX.TO, flavor='raw')
 
         # Allocate a lock for each HASH object.
         # An optimization would be to not release the GIL on small requests,
@@ -26,12 +27,15 @@
         if not digest:
             raise OperationError(space.w_ValueError,
                                  space.wrap("unknown hash function"))
-        ropenssl.EVP_DigestInit(self.ctx, digest)
+        ctx = lltype.malloc(ropenssl.EVP_MD_CTX.TO, flavor='raw')
+        ropenssl.EVP_DigestInit(ctx, digest)
+        self.ctx = ctx
 
     def __del__(self):
         # self.lock.free()
-        ropenssl.EVP_MD_CTX_cleanup(self.ctx)
-        lltype.free(self.ctx, flavor='raw')
+        if self.ctx:
+            ropenssl.EVP_MD_CTX_cleanup(self.ctx)
+            lltype.free(self.ctx, flavor='raw')
 
     def descr_repr(self, space):
         addrstring = self.getaddrstring(space)


More information about the Pypy-commit mailing list